@@ -2359,7 +2359,10 @@ impl<'a, 'tcx> Visitor<'tcx> for VarCollectorVisitor<'a, 'tcx> {
2359
2359
const NEEDLESS_COLLECT_MSG : & str = "avoid using `collect()` when not needed" ;
2360
2360
2361
2361
fn 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 > ) {
2363
2366
if_chain ! {
2364
2367
if let ExprKind :: MethodCall ( ref method, _, ref args, _) = expr. kind;
2365
2368
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>) {
2425
2428
}
2426
2429
}
2427
2430
}
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 > ) {
2429
2434
if let ExprKind :: Block ( ref block, _) = expr. kind {
2430
2435
for ref stmt in block. stmts {
2431
2436
if_chain ! {
@@ -2484,10 +2489,18 @@ impl IterFunction {
2484
2489
}
2485
2490
fn get_suggestion_text ( & self ) -> & ' static str {
2486
2491
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
+ } ,
2491
2504
}
2492
2505
}
2493
2506
}
@@ -2505,6 +2518,8 @@ struct IterFunctionVisitor {
2505
2518
}
2506
2519
impl < ' tcx > Visitor < ' tcx > for IterFunctionVisitor {
2507
2520
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
2508
2523
if_chain ! {
2509
2524
if let ExprKind :: MethodCall ( method_name, _, ref args, _) = & expr. kind;
2510
2525
if let Some ( Expr { kind: ExprKind :: Path ( QPath :: Resolved ( _, ref path) ) , .. } ) = args. get( 0 ) ;
@@ -2515,10 +2530,18 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor {
2515
2530
let is_empty = sym!( is_empty) ;
2516
2531
let contains = sym!( contains) ;
2517
2532
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
+ ) ,
2522
2545
_ => self . seen_other = true ,
2523
2546
}
2524
2547
}
0 commit comments