1
- use std:: cmp:: Ordering ;
2
1
use std:: ops:: ControlFlow ;
3
2
4
3
use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_and_then} ;
@@ -18,19 +17,19 @@ use rustc_span::{sym, Span};
18
17
19
18
declare_clippy_lint ! {
20
19
/// ### What it does
21
- /// Checks for manual char comparison in string patterns
20
+ /// Checks for manual ` char` comparison in string patterns
22
21
///
23
22
/// ### Why is this bad?
24
23
/// This can be written more concisely using a `char` or an array of `char`.
25
24
/// This is more readable and more optimized when comparing to only one `char`.
26
25
///
27
26
/// ### Example
28
27
/// ```no_run
29
- /// "Hello World !".trim_end_matches(|c: char | c == '.' || c == ',' || c == '!' || c == '?');
28
+ /// "Hello World!".trim_end_matches(|c| c == '.' || c == ',' || c == '!' || c == '?');
30
29
/// ```
31
30
/// Use instead:
32
31
/// ```no_run
33
- /// "Hello World !".trim_end_matches(['.', ',', '!', '?']);
32
+ /// "Hello World!".trim_end_matches(['.', ',', '!', '?']);
34
33
/// ```
35
34
#[ clippy:: version = "1.80.0" ]
36
35
pub MANUAL_PATTERN_CHAR_COMPARISON ,
@@ -124,10 +123,9 @@ fn get_char_span<'tcx>(cx: &'_ LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Optio
124
123
}
125
124
126
125
fn check_manual_pattern_char_comparison ( cx : & LateContext < ' _ > , method_arg : & Expr < ' _ > ) {
127
- let applicability = Applicability :: MachineApplicable ;
128
126
if let ExprKind :: Closure ( closure) = method_arg. kind
129
127
&& let body = cx. tcx . hir ( ) . body ( closure. body )
130
- && let PatKind :: Binding ( _, binding, ..) = body. params [ 0 ] . pat . kind
128
+ && let Some ( PatKind :: Binding ( _, binding, ..) ) = body. params . first ( ) . map ( |p| p . pat . kind )
131
129
{
132
130
let mut set_char_spans: Vec < Span > = Vec :: new ( ) ;
133
131
@@ -185,27 +183,25 @@ fn check_manual_pattern_char_comparison(cx: &LateContext<'_>, method_arg: &Expr<
185
183
MANUAL_PATTERN_CHAR_COMPARISON ,
186
184
method_arg. span ,
187
185
"this manual char comparison can be written more succinctly" ,
188
- |diag| match set_char_spans . len ( ) . cmp ( & 1 ) {
189
- Ordering :: Equal => {
186
+ |diag| {
187
+ if let [ set_char_span ] = set_char_spans [ .. ] {
190
188
diag. span_suggestion (
191
189
method_arg. span ,
192
190
"consider using a `char`" ,
193
- snippet ( cx, set_char_spans [ 0 ] , "c" ) ,
194
- applicability ,
191
+ snippet ( cx, set_char_span , "c" ) ,
192
+ Applicability :: MachineApplicable ,
195
193
) ;
196
- } ,
197
- Ordering :: Greater => {
194
+ } else {
198
195
diag. span_suggestion (
199
196
method_arg. span ,
200
197
"consider using an array of `char`" ,
201
198
format ! (
202
199
"[{}]" ,
203
200
set_char_spans. into_iter( ) . map( |span| snippet( cx, span, "c" ) ) . join( ", " )
204
201
) ,
205
- applicability ,
202
+ Applicability :: MachineApplicable ,
206
203
) ;
207
- } ,
208
- Ordering :: Less => { } ,
204
+ }
209
205
} ,
210
206
) ;
211
207
}
0 commit comments