@@ -22,7 +22,7 @@ pub(super) fn check<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
2222fn check_needless_collect_direct_usage < ' tcx > ( expr : & ' tcx Expr < ' _ > , cx : & LateContext < ' tcx > ) {
2323 if_chain ! {
2424 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;
2626 if chain_method. ident. name == sym!( collect) && is_trait_method( cx, & args[ 0 ] , sym:: Iterator ) ;
2727 if let Some ( ref generic_args) = chain_method. args;
2828 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
3131 || is_type_diagnostic_item( cx, ty, sym:: vecdeque_type)
3232 || match_type( cx, ty, & paths:: BTREEMAP )
3333 || 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" => {
6038 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) )
8243 }
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+ ) ;
8356 }
8457 }
8558}
@@ -269,14 +242,3 @@ fn detect_iter_and_into_iters<'tcx>(block: &'tcx Block<'tcx>, identifier: Ident)
269242 visitor. visit_block ( block) ;
270243 if visitor. seen_other { None } else { Some ( visitor. uses ) }
271244}
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