@@ -2359,7 +2359,10 @@ impl<'a, 'tcx> Visitor<'tcx> for VarCollectorVisitor<'a, 'tcx> {
23592359const NEEDLESS_COLLECT_MSG : & str = "avoid using `collect()` when not needed" ;
23602360
23612361fn check_needless_collect < ' tcx > ( expr : & ' tcx Expr < ' _ > , cx : & LateContext < ' tcx > ) {
2362- // Check for direct, immediate usage
2362+ check_needless_collect_direct_usage ( expr, cx) ;
2363+ check_needless_collect_indirect_usage ( expr, cx) ;
2364+ }
2365+ fn check_needless_collect_direct_usage < ' tcx > ( expr : & ' tcx Expr < ' _ > , cx : & LateContext < ' tcx > ) {
23632366 if_chain ! {
23642367 if let ExprKind :: MethodCall ( ref method, _, ref args, _) = expr. kind;
23652368 if let ExprKind :: MethodCall ( ref chain_method, _, _, _) = args[ 0 ] . kind;
@@ -2425,7 +2428,9 @@ fn check_needless_collect<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
24252428 }
24262429 }
24272430 }
2428- // Check for collecting it and then turning it back into an iterator later
2431+ }
2432+
2433+ fn check_needless_collect_indirect_usage < ' tcx > ( expr : & ' tcx Expr < ' _ > , cx : & LateContext < ' tcx > ) {
24292434 if let ExprKind :: Block ( ref block, _) = expr. kind {
24302435 for ref stmt in block. stmts {
24312436 if_chain ! {
@@ -2484,10 +2489,18 @@ impl IterFunction {
24842489 }
24852490 fn get_suggestion_text ( & self ) -> & ' static str {
24862491 match & self . func {
2487- IterFunctionKind :: IntoIter => "Use the original Iterator instead of collecting it and then producing a new one" ,
2488- IterFunctionKind :: Len => "Take the original Iterator's count instead of collecting it and finding the length" ,
2489- IterFunctionKind :: IsEmpty => "Check if the original Iterator has anything instead of collecting it and seeing if it's empty" ,
2490- IterFunctionKind :: Contains ( _) => "Check if the original Iterator contains an element instead of collecting then checking" ,
2492+ IterFunctionKind :: IntoIter => {
2493+ "Use the original Iterator instead of collecting it and then producing a new one"
2494+ } ,
2495+ IterFunctionKind :: Len => {
2496+ "Take the original Iterator's count instead of collecting it and finding the length"
2497+ } ,
2498+ IterFunctionKind :: IsEmpty => {
2499+ "Check if the original Iterator has anything instead of collecting it and seeing if it's empty"
2500+ } ,
2501+ IterFunctionKind :: Contains ( _) => {
2502+ "Check if the original Iterator contains an element instead of collecting then checking"
2503+ } ,
24912504 }
24922505 }
24932506}
@@ -2505,6 +2518,8 @@ struct IterFunctionVisitor {
25052518}
25062519impl < ' tcx > Visitor < ' tcx > for IterFunctionVisitor {
25072520 fn visit_expr ( & mut self , expr : & ' tcx Expr < ' tcx > ) {
2521+ // TODO Check if the target identifier is being used in something other
2522+ // than a function call
25082523 if_chain ! {
25092524 if let ExprKind :: MethodCall ( method_name, _, ref args, _) = & expr. kind;
25102525 if let Some ( Expr { kind: ExprKind :: Path ( QPath :: Resolved ( _, ref path) ) , .. } ) = args. get( 0 ) ;
@@ -2515,10 +2530,18 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor {
25152530 let is_empty = sym!( is_empty) ;
25162531 let contains = sym!( contains) ;
25172532 match method_name. ident. name {
2518- name if name == into_iter => self . uses. push( IterFunction { func: IterFunctionKind :: IntoIter , span: expr. span } ) ,
2519- name if name == len => self . uses. push( IterFunction { func: IterFunctionKind :: Len , span: expr. span } ) ,
2520- name if name == is_empty => self . uses. push( IterFunction { func: IterFunctionKind :: IsEmpty , span: expr. span } ) ,
2521- name if name == contains => self . uses. push( IterFunction { func: IterFunctionKind :: Contains ( args[ 1 ] . span) , span: expr. span } ) ,
2533+ name if name == into_iter => self . uses. push(
2534+ IterFunction { func: IterFunctionKind :: IntoIter , span: expr. span }
2535+ ) ,
2536+ name if name == len => self . uses. push(
2537+ IterFunction { func: IterFunctionKind :: Len , span: expr. span }
2538+ ) ,
2539+ name if name == is_empty => self . uses. push(
2540+ IterFunction { func: IterFunctionKind :: IsEmpty , span: expr. span }
2541+ ) ,
2542+ name if name == contains => self . uses. push(
2543+ IterFunction { func: IterFunctionKind :: Contains ( args[ 1 ] . span) , span: expr. span }
2544+ ) ,
25222545 _ => self . seen_other = true ,
25232546 }
25242547 }
0 commit comments