Skip to content

Commit ab1ba47

Browse files
committed
Make suggestion always verbose
1 parent 3e8211d commit ab1ba47

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

clippy_lints/src/methods/string_lit_chars_any.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::{
2-
diagnostics::span_lint_and_sugg,
2+
diagnostics::span_lint_and_then,
33
is_from_proc_macro, is_trait_method,
44
msrvs::{Msrv, MATCHES_MACRO},
55
path_to_local,
@@ -49,14 +49,19 @@ pub(super) fn check<'tcx>(
4949
// something like `r"\"` will become `'\'`, which is of course invalid
5050
let pat_snip = val.as_str().chars().map(|c| format!("{c:?}")).join(" | ");
5151

52-
span_lint_and_sugg(
52+
span_lint_and_then(
5353
cx,
5454
STRING_LIT_CHARS_ANY,
5555
expr.span,
5656
"usage of `.chars().any(...)` to check if a char matches any from a string literal",
57-
"use `matches!(...)` instead",
58-
format!("matches!({scrutinee_snip}, {pat_snip})"),
59-
Applicability::MachineApplicable,
57+
|diag| {
58+
diag.span_suggestion_verbose(
59+
expr.span,
60+
"use `matches!(...)` instead",
61+
format!("matches!({scrutinee_snip}, {pat_snip})"),
62+
Applicability::MachineApplicable,
63+
);
64+
}
6065
);
6166
}
6267
}
@@ -69,11 +74,7 @@ fn lhs_rhs_are_param_and_scrutinee<'tcx>(
6974
one: &'tcx Expr<'tcx>,
7075
two: &'tcx Expr<'tcx>,
7176
) -> Option<&'tcx Expr<'tcx>> {
72-
if path_to_local(one).is_some_and(|hir_id| bindings.contains(&hir_id))
73-
&& path_to_local(two).is_some_and(|hir_id| !bindings.contains(&hir_id))
74-
{
75-
return Some(two);
76-
}
77-
78-
None
77+
(path_to_local(one).is_some_and(|hir_id| bindings.contains(&hir_id))
78+
&& path_to_local(two).is_some_and(|hir_id| !bindings.contains(&hir_id)))
79+
.then_some(two)
7980
}

tests/ui/string_lit_chars_any.stderr

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,57 @@ error: usage of `.chars().any(...)` to check if a char matches any from a string
22
--> $DIR/string_lit_chars_any.rs:19:5
33
|
44
LL | "//.+*?()|[]{}^$#&-~".chars().any(|x| x == c);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `matches!(...)` instead: `matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~')`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::string-lit-chars-any` implied by `-D warnings`
8+
help: use `matches!(...)` instead
9+
|
10+
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~');
11+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
812

913
error: usage of `.chars().any(...)` to check if a char matches any from a string literal
1014
--> $DIR/string_lit_chars_any.rs:20:5
1115
|
1216
LL | r#"/.+*?()|[]{}^$#&-~"#.chars().any(|x| x == c);
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `matches!(...)` instead: `matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~')`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
help: use `matches!(...)` instead
20+
|
21+
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~');
22+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1423

1524
error: usage of `.chars().any(...)` to check if a char matches any from a string literal
1625
--> $DIR/string_lit_chars_any.rs:21:5
1726
|
1827
LL | "//.+*?()|[]{}^$#&-~".chars().any(|x| c == x);
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `matches!(...)` instead: `matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~')`
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
|
30+
help: use `matches!(...)` instead
31+
|
32+
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~');
33+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2034

2135
error: usage of `.chars().any(...)` to check if a char matches any from a string literal
2236
--> $DIR/string_lit_chars_any.rs:22:5
2337
|
2438
LL | r#"/.+*?()|[]{}^$#&-~"#.chars().any(|x| c == x);
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `matches!(...)` instead: `matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~')`
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
|
41+
help: use `matches!(...)` instead
42+
|
43+
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~');
44+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2645

2746
error: usage of `.chars().any(...)` to check if a char matches any from a string literal
2847
--> $DIR/string_lit_chars_any.rs:24:5
2948
|
3049
LL | "//.+*?()|[]{}^$#&-~".chars().any(|x| { x == c });
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `matches!(...)` instead: `matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~')`
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51+
|
52+
help: use `matches!(...)` instead
53+
|
54+
LL | matches!(c, '//' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$' | '#' | '&' | '-' | '~');
55+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3256

3357
error: aborting due to 5 previous errors
3458

0 commit comments

Comments
 (0)