-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #11483 - y21:path_ends_with, r=Alexendoo
new lint: `path_ends_with_ext` Closes #11479 Not sure if it needs more test cases. I couldn't come up with any other ones, but it is a pretty simple lint logic wise with not too many checks changelog: new lint: [`path_ends_with_ext`]
- Loading branch information
Showing
11 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::PATH_ENDS_WITH_EXT; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::msrvs; | ||
use clippy_utils::msrvs::Msrv; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_ast::{LitKind, StrStyle}; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
use std::fmt::Write; | ||
|
||
pub const DEFAULT_ALLOWED_DOTFILES: &[&str] = &[ | ||
"git", "svn", "gem", "npm", "vim", "env", "rnd", "ssh", "vnc", "smb", "nvm", "bin", | ||
]; | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
recv: &Expr<'_>, | ||
path: &Expr<'_>, | ||
expr: &Expr<'_>, | ||
msrv: &Msrv, | ||
allowed_dotfiles: &FxHashSet<String>, | ||
) { | ||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv).peel_refs(), sym::Path) | ||
&& !path.span.from_expansion() | ||
&& let ExprKind::Lit(lit) = path.kind | ||
&& let LitKind::Str(path, StrStyle::Cooked) = lit.node | ||
&& let Some(path) = path.as_str().strip_prefix('.') | ||
&& (1..=3).contains(&path.len()) | ||
&& !allowed_dotfiles.contains(path) | ||
&& path.chars().all(char::is_alphanumeric) | ||
{ | ||
let mut sugg = snippet(cx, recv.span, "..").into_owned(); | ||
if msrv.meets(msrvs::OPTION_IS_SOME_AND) { | ||
let _ = write!(sugg, r#".extension().is_some_and(|ext| ext == "{path}")"#); | ||
} else { | ||
let _ = write!(sugg, r#".extension().map_or(false, |ext| ext == "{path}")"#); | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
PATH_ENDS_WITH_EXT, | ||
expr.span, | ||
"this looks like a failed attempt at checking for the file extension", | ||
"try", | ||
sugg, | ||
Applicability::MaybeIncorrect | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#![warn(clippy::path_ends_with_ext)] | ||
use std::path::Path; | ||
|
||
macro_rules! arg { | ||
() => { | ||
".md" | ||
}; | ||
} | ||
|
||
fn test(path: &Path) { | ||
path.extension().is_some_and(|ext| ext == "md"); | ||
//~^ ERROR: this looks like a failed attempt at checking for the file extension | ||
|
||
// some "extensions" are allowed by default | ||
path.ends_with(".git"); | ||
|
||
// most legitimate "dotfiles" are longer than 3 chars, so we allow them as well | ||
path.ends_with(".bashrc"); | ||
|
||
// argument from expn shouldn't trigger | ||
path.ends_with(arg!()); | ||
|
||
path.ends_with(".."); | ||
path.ends_with("./a"); | ||
path.ends_with("."); | ||
path.ends_with(""); | ||
} | ||
|
||
// is_some_and was stabilized in 1.70, so suggest map_or(false, ..) if under that | ||
#[clippy::msrv = "1.69"] | ||
fn under_msv(path: &Path) -> bool { | ||
path.extension().map_or(false, |ext| ext == "md") | ||
//~^ ERROR: this looks like a failed attempt at checking for the file extension | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#![warn(clippy::path_ends_with_ext)] | ||
use std::path::Path; | ||
|
||
macro_rules! arg { | ||
() => { | ||
".md" | ||
}; | ||
} | ||
|
||
fn test(path: &Path) { | ||
path.ends_with(".md"); | ||
//~^ ERROR: this looks like a failed attempt at checking for the file extension | ||
|
||
// some "extensions" are allowed by default | ||
path.ends_with(".git"); | ||
|
||
// most legitimate "dotfiles" are longer than 3 chars, so we allow them as well | ||
path.ends_with(".bashrc"); | ||
|
||
// argument from expn shouldn't trigger | ||
path.ends_with(arg!()); | ||
|
||
path.ends_with(".."); | ||
path.ends_with("./a"); | ||
path.ends_with("."); | ||
path.ends_with(""); | ||
} | ||
|
||
// is_some_and was stabilized in 1.70, so suggest map_or(false, ..) if under that | ||
#[clippy::msrv = "1.69"] | ||
fn under_msv(path: &Path) -> bool { | ||
path.ends_with(".md") | ||
//~^ ERROR: this looks like a failed attempt at checking for the file extension | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: this looks like a failed attempt at checking for the file extension | ||
--> $DIR/path_ends_with_ext.rs:11:5 | ||
| | ||
LL | path.ends_with(".md"); | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `path.extension().is_some_and(|ext| ext == "md")` | ||
| | ||
= note: `-D clippy::path-ends-with-ext` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::path_ends_with_ext)]` | ||
|
||
error: this looks like a failed attempt at checking for the file extension | ||
--> $DIR/path_ends_with_ext.rs:32:5 | ||
| | ||
LL | path.ends_with(".md") | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `path.extension().map_or(false, |ext| ext == "md")` | ||
|
||
error: aborting due to 2 previous errors | ||
|