@@ -5,10 +5,9 @@ use crate::utils::sugg::Sugg;
5
5
use crate :: utils:: usage:: { is_unused, mutated_variables} ;
6
6
use crate :: utils:: {
7
7
get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
8
- is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, last_path_segment, match_path,
9
- match_trait_method, match_type, match_var, multispan_sugg, qpath_res, snippet, snippet_opt,
10
- snippet_with_applicability, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg,
11
- SpanlessEq ,
8
+ is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, last_path_segment, match_trait_method,
9
+ match_type, match_var, multispan_sugg, qpath_res, snippet, snippet_opt, snippet_with_applicability, span_lint,
10
+ span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, SpanlessEq ,
12
11
} ;
13
12
use if_chain:: if_chain;
14
13
use rustc_ast:: ast;
@@ -2514,16 +2513,16 @@ enum IterFunctionKind {
2514
2513
struct IterFunctionVisitor {
2515
2514
uses : Vec < IterFunction > ,
2516
2515
seen_other : bool ,
2517
- target : String ,
2516
+ target : Ident ,
2518
2517
}
2519
2518
impl < ' tcx > Visitor < ' tcx > for IterFunctionVisitor {
2520
2519
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
2520
+ // Check function calls on our collection
2523
2521
if_chain ! {
2524
2522
if let ExprKind :: MethodCall ( method_name, _, ref args, _) = & expr. kind;
2525
2523
if let Some ( Expr { kind: ExprKind :: Path ( QPath :: Resolved ( _, ref path) ) , .. } ) = args. get( 0 ) ;
2526
- if match_path( path, & [ & self . target] ) ;
2524
+ if let & [ name] = & path. segments;
2525
+ if name. ident == self . target;
2527
2526
then {
2528
2527
let into_iter = sym!( into_iter) ;
2529
2528
let len = sym!( len) ;
@@ -2544,8 +2543,17 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor {
2544
2543
) ,
2545
2544
_ => self . seen_other = true ,
2546
2545
}
2546
+ return
2547
2547
}
2548
- else {
2548
+ }
2549
+ // Check if the collection is used for anything else
2550
+ if_chain ! {
2551
+ if let Expr { kind: ExprKind :: Path ( QPath :: Resolved ( _, ref path) ) , .. } = expr;
2552
+ if let & [ name] = & path. segments;
2553
+ if name. ident == self . target;
2554
+ then {
2555
+ self . seen_other = true ;
2556
+ } else {
2549
2557
walk_expr( self , expr) ;
2550
2558
}
2551
2559
}
@@ -2562,7 +2570,7 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor {
2562
2570
fn detect_iter_and_into_iters < ' tcx > ( block : & ' tcx Block < ' tcx > , identifier : Ident ) -> Option < Vec < IterFunction > > {
2563
2571
let mut visitor = IterFunctionVisitor {
2564
2572
uses : Vec :: new ( ) ,
2565
- target : identifier. name . to_ident_string ( ) ,
2573
+ target : identifier,
2566
2574
seen_other : false ,
2567
2575
} ;
2568
2576
visitor. visit_block ( block) ;
0 commit comments