-
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 #11052 - Centri3:string_lit_chars_any, r=Jarcho
New lint [`string_lit_chars_any`] Closes #10389 This lint can probably be deprecated if/when rustc optimizes `.chars().any(...)`. changelog: New lint [`string_lit_chars_any`]
- Loading branch information
Showing
7 changed files
with
256 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::msrvs::{Msrv, MATCHES_MACRO}; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::{is_from_proc_macro, is_trait_method, path_to_local}; | ||
use itertools::Itertools; | ||
use rustc_ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{BinOpKind, Expr, ExprKind, Param, PatKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::STRING_LIT_CHARS_ANY; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
recv: &Expr<'_>, | ||
param: &'tcx Param<'tcx>, | ||
body: &Expr<'_>, | ||
msrv: &Msrv, | ||
) { | ||
if msrv.meets(MATCHES_MACRO) | ||
&& is_trait_method(cx, expr, sym::Iterator) | ||
&& let PatKind::Binding(_, arg, _, _) = param.pat.kind | ||
&& let ExprKind::Lit(lit_kind) = recv.kind | ||
&& let LitKind::Str(val, _) = lit_kind.node | ||
&& let ExprKind::Binary(kind, lhs, rhs) = body.kind | ||
&& let BinOpKind::Eq = kind.node | ||
&& let Some(lhs_path) = path_to_local(lhs) | ||
&& let Some(rhs_path) = path_to_local(rhs) | ||
&& let scrutinee = match (lhs_path == arg, rhs_path == arg) { | ||
(true, false) => rhs, | ||
(false, true) => lhs, | ||
_ => return, | ||
} | ||
&& !is_from_proc_macro(cx, expr) | ||
&& let Some(scrutinee_snip) = snippet_opt(cx, scrutinee.span) | ||
{ | ||
// Normalize the char using `map` so `join` doesn't use `Display`, if we don't then | ||
// something like `r"\"` will become `'\'`, which is of course invalid | ||
let pat_snip = val.as_str().chars().map(|c| format!("{c:?}")).join(" | "); | ||
|
||
span_lint_and_then( | ||
cx, | ||
STRING_LIT_CHARS_ANY, | ||
expr.span, | ||
"usage of `.chars().any(...)` to check if a char matches any from a string literal", | ||
|diag| { | ||
diag.span_suggestion_verbose( | ||
expr.span, | ||
"use `matches!(...)` instead", | ||
format!("matches!({scrutinee_snip}, {pat_snip})"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
); | ||
} | ||
} |
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,50 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(clippy::eq_op, clippy::needless_raw_string_hashes, clippy::no_effect, unused)] | ||
#![warn(clippy::string_lit_chars_any)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
struct NotStringLit; | ||
|
||
impl NotStringLit { | ||
fn chars(&self) -> impl Iterator<Item = char> { | ||
"c".chars() | ||
} | ||
} | ||
|
||
fn main() { | ||
let c = 'c'; | ||
matches!(c, '\\' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
matches!(c, '\\' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
matches!(c, '\\' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
matches!(c, '\\' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
#[rustfmt::skip] | ||
matches!(c, '\\' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
// Do not lint | ||
NotStringLit.chars().any(|x| x == c); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| { | ||
let c = 'c'; | ||
x == c | ||
}); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| { | ||
1; | ||
x == c | ||
}); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == x); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|_x| c == c); | ||
matches!( | ||
c, | ||
'\\' | '.' | '+' | '*' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~' | ||
); | ||
external! { | ||
let c = 'c'; | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == c); | ||
} | ||
with_span! { | ||
span | ||
let c = 'c'; | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == c); | ||
} | ||
} |
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,50 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(clippy::eq_op, clippy::needless_raw_string_hashes, clippy::no_effect, unused)] | ||
#![warn(clippy::string_lit_chars_any)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
struct NotStringLit; | ||
|
||
impl NotStringLit { | ||
fn chars(&self) -> impl Iterator<Item = char> { | ||
"c".chars() | ||
} | ||
} | ||
|
||
fn main() { | ||
let c = 'c'; | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == c); | ||
r#"\.+*?()|[]{}^$#&-~"#.chars().any(|x| x == c); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| c == x); | ||
r#"\.+*?()|[]{}^$#&-~"#.chars().any(|x| c == x); | ||
#[rustfmt::skip] | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| { x == c }); | ||
// Do not lint | ||
NotStringLit.chars().any(|x| x == c); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| { | ||
let c = 'c'; | ||
x == c | ||
}); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| { | ||
1; | ||
x == c | ||
}); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == x); | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|_x| c == c); | ||
matches!( | ||
c, | ||
'\\' | '.' | '+' | '*' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~' | ||
); | ||
external! { | ||
let c = 'c'; | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == c); | ||
} | ||
with_span! { | ||
span | ||
let c = 'c'; | ||
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == c); | ||
} | ||
} |
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,58 @@ | ||
error: usage of `.chars().any(...)` to check if a char matches any from a string literal | ||
--> $DIR/string_lit_chars_any.rs:19:5 | ||
| | ||
LL | "//.+*?()|[]{}^$#&-~".chars().any(|x| x == c); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::string-lit-chars-any` implied by `-D warnings` | ||
help: use `matches!(...)` instead | ||
| | ||
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: usage of `.chars().any(...)` to check if a char matches any from a string literal | ||
--> $DIR/string_lit_chars_any.rs:20:5 | ||
| | ||
LL | r#"/.+*?()|[]{}^$#&-~"#.chars().any(|x| x == c); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: use `matches!(...)` instead | ||
| | ||
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: usage of `.chars().any(...)` to check if a char matches any from a string literal | ||
--> $DIR/string_lit_chars_any.rs:21:5 | ||
| | ||
LL | "//.+*?()|[]{}^$#&-~".chars().any(|x| c == x); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: use `matches!(...)` instead | ||
| | ||
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: usage of `.chars().any(...)` to check if a char matches any from a string literal | ||
--> $DIR/string_lit_chars_any.rs:22:5 | ||
| | ||
LL | r#"/.+*?()|[]{}^$#&-~"#.chars().any(|x| c == x); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: use `matches!(...)` instead | ||
| | ||
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: usage of `.chars().any(...)` to check if a char matches any from a string literal | ||
--> $DIR/string_lit_chars_any.rs:24:5 | ||
| | ||
LL | "//.+*?()|[]{}^$#&-~".chars().any(|x| { x == c }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: use `matches!(...)` instead | ||
| | ||
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~'); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 5 previous errors | ||
|