1- use std:: cmp:: Ordering ;
21use std:: ops:: ControlFlow ;
32
43use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_and_then} ;
@@ -18,19 +17,19 @@ use rustc_span::{sym, Span};
1817
1918declare_clippy_lint ! {
2019 /// ### What it does
21- /// Checks for manual char comparison in string patterns
20+ /// Checks for manual ` char` comparison in string patterns
2221 ///
2322 /// ### Why is this bad?
2423 /// This can be written more concisely using a `char` or an array of `char`.
2524 /// This is more readable and more optimized when comparing to only one `char`.
2625 ///
2726 /// ### Example
2827 /// ```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 == '?');
3029 /// ```
3130 /// Use instead:
3231 /// ```no_run
33- /// "Hello World !".trim_end_matches(['.', ',', '!', '?']);
32+ /// "Hello World!".trim_end_matches(['.', ',', '!', '?']);
3433 /// ```
3534 #[ clippy:: version = "1.80.0" ]
3635 pub MANUAL_PATTERN_CHAR_COMPARISON ,
@@ -124,10 +123,9 @@ fn get_char_span<'tcx>(cx: &'_ LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Optio
124123}
125124
126125fn check_manual_pattern_char_comparison ( cx : & LateContext < ' _ > , method_arg : & Expr < ' _ > ) {
127- let applicability = Applicability :: MachineApplicable ;
128126 if let ExprKind :: Closure ( closure) = method_arg. kind
129127 && 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 )
131129 {
132130 let mut set_char_spans: Vec < Span > = Vec :: new ( ) ;
133131
@@ -185,27 +183,25 @@ fn check_manual_pattern_char_comparison(cx: &LateContext<'_>, method_arg: &Expr<
185183 MANUAL_PATTERN_CHAR_COMPARISON ,
186184 method_arg. span ,
187185 "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 [ .. ] {
190188 diag. span_suggestion (
191189 method_arg. span ,
192190 "consider using a `char`" ,
193- snippet ( cx, set_char_spans [ 0 ] , "c" ) ,
194- applicability ,
191+ snippet ( cx, set_char_span , "c" ) ,
192+ Applicability :: MachineApplicable ,
195193 ) ;
196- } ,
197- Ordering :: Greater => {
194+ } else {
198195 diag. span_suggestion (
199196 method_arg. span ,
200197 "consider using an array of `char`" ,
201198 format ! (
202199 "[{}]" ,
203200 set_char_spans. into_iter( ) . map( |span| snippet( cx, span, "c" ) ) . join( ", " )
204201 ) ,
205- applicability ,
202+ Applicability :: MachineApplicable ,
206203 ) ;
207- } ,
208- Ordering :: Less => { } ,
204+ }
209205 } ,
210206 ) ;
211207 }
0 commit comments