@@ -22,7 +22,7 @@ pub(super) fn check<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
22
22
fn check_needless_collect_direct_usage < ' tcx > ( expr : & ' tcx Expr < ' _ > , cx : & LateContext < ' tcx > ) {
23
23
if_chain ! {
24
24
if let ExprKind :: MethodCall ( ref method, _, ref args, _) = expr. kind;
25
- if let ExprKind :: MethodCall ( ref chain_method, _ , _, _) = args[ 0 ] . kind;
25
+ if let ExprKind :: MethodCall ( ref chain_method, method0_span , _, _) = args[ 0 ] . kind;
26
26
if chain_method. ident. name == sym!( collect) && is_trait_method( cx, & args[ 0 ] , sym:: Iterator ) ;
27
27
if let Some ( ref generic_args) = chain_method. args;
28
28
if let Some ( GenericArg :: Type ( ref ty) ) = generic_args. args. get( 0 ) ;
@@ -31,55 +31,28 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
31
31
|| is_type_diagnostic_item( cx, ty, sym:: vecdeque_type)
32
32
|| match_type( cx, ty, & paths:: BTREEMAP )
33
33
|| is_type_diagnostic_item( cx, ty, sym:: hashmap_type) ;
34
- then {
35
- if method. ident. name == sym!( len) {
36
- let span = shorten_needless_collect_span( expr) ;
37
- span_lint_and_sugg(
38
- cx,
39
- NEEDLESS_COLLECT ,
40
- span,
41
- NEEDLESS_COLLECT_MSG ,
42
- "replace with" ,
43
- "count()" . to_string( ) ,
44
- Applicability :: MachineApplicable ,
45
- ) ;
46
- }
47
- if method. ident. name == sym!( is_empty) {
48
- let span = shorten_needless_collect_span( expr) ;
49
- span_lint_and_sugg(
50
- cx,
51
- NEEDLESS_COLLECT ,
52
- span,
53
- NEEDLESS_COLLECT_MSG ,
54
- "replace with" ,
55
- "next().is_none()" . to_string( ) ,
56
- Applicability :: MachineApplicable ,
57
- ) ;
58
- }
59
- if method. ident. name == sym!( contains) {
34
+ if let Some ( sugg) = match & * method. ident. name. as_str( ) {
35
+ "len" => Some ( "count()" . to_string( ) ) ,
36
+ "is_empty" => Some ( "next().is_none()" . to_string( ) ) ,
37
+ "contains" => {
60
38
let contains_arg = snippet( cx, args[ 1 ] . span, "??" ) ;
61
- let span = shorten_needless_collect_span( expr) ;
62
- span_lint_and_then(
63
- cx,
64
- NEEDLESS_COLLECT ,
65
- span,
66
- NEEDLESS_COLLECT_MSG ,
67
- |diag| {
68
- let ( arg, pred) = contains_arg
69
- . strip_prefix( '&' )
70
- . map_or( ( "&x" , & * contains_arg) , |s| ( "x" , s) ) ;
71
- diag. span_suggestion(
72
- span,
73
- "replace with" ,
74
- format!(
75
- "any(|{}| x == {})" ,
76
- arg, pred
77
- ) ,
78
- Applicability :: MachineApplicable ,
79
- ) ;
80
- }
81
- ) ;
39
+ let ( arg, pred) = contains_arg
40
+ . strip_prefix( '&' )
41
+ . map_or( ( "&x" , & * contains_arg) , |s| ( "x" , s) ) ;
42
+ Some ( format!( "any(|{}| x == {})" , arg, pred) )
82
43
}
44
+ _ => None ,
45
+ } ;
46
+ then {
47
+ span_lint_and_sugg(
48
+ cx,
49
+ NEEDLESS_COLLECT ,
50
+ method0_span. with_hi( expr. span. hi( ) ) ,
51
+ NEEDLESS_COLLECT_MSG ,
52
+ "replace with" ,
53
+ sugg,
54
+ Applicability :: MachineApplicable ,
55
+ ) ;
83
56
}
84
57
}
85
58
}
@@ -269,14 +242,3 @@ fn detect_iter_and_into_iters<'tcx>(block: &'tcx Block<'tcx>, identifier: Ident)
269
242
visitor. visit_block ( block) ;
270
243
if visitor. seen_other { None } else { Some ( visitor. uses ) }
271
244
}
272
-
273
- fn shorten_needless_collect_span ( expr : & Expr < ' _ > ) -> Span {
274
- if_chain ! {
275
- if let ExprKind :: MethodCall ( .., args, _) = & expr. kind;
276
- if let ExprKind :: MethodCall ( _, span, ..) = & args[ 0 ] . kind;
277
- then {
278
- return expr. span. with_lo( span. lo( ) ) ;
279
- }
280
- }
281
- unreachable ! ( ) ;
282
- }
0 commit comments