@@ -3,14 +3,14 @@ use clippy_utils::source::{snippet_indent, snippet_with_context};
3
3
use clippy_utils:: sugg:: Sugg ;
4
4
use clippy_utils:: ty:: is_type_diagnostic_item;
5
5
6
- use clippy_utils:: { can_mut_borrow_both, eq_expr_value, is_in_const_context, std_or_core} ;
6
+ use clippy_utils:: { can_mut_borrow_both, eq_expr_value, is_in_const_context, path_to_local , std_or_core} ;
7
7
use itertools:: Itertools ;
8
8
9
9
use rustc_data_structures:: fx:: FxIndexSet ;
10
10
use rustc_hir:: intravisit:: { Visitor , walk_expr} ;
11
11
12
12
use rustc_errors:: Applicability ;
13
- use rustc_hir:: { AssignOpKind , Block , Expr , ExprKind , LetStmt , PatKind , Path , QPath , Stmt , StmtKind } ;
13
+ use rustc_hir:: { AssignOpKind , Block , Expr , ExprKind , LetStmt , PatKind , QPath , Stmt , StmtKind } ;
14
14
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
15
15
use rustc_middle:: ty;
16
16
use rustc_session:: declare_lint_pass;
@@ -357,16 +357,13 @@ impl<'tcx> IndexBinding<'_, 'tcx> {
357
357
let init = self . cx . expr_or_init ( expr) ;
358
358
359
359
// We skip suggesting a variable binding in any of these cases:
360
- // 1. Variable initialization is outside the suggestion span
361
- // 2. Variable initialization is inside the suggestion span but the variable is not used as an index
362
- // or elsewhere later
363
- // 3. Variable initialization is inside the suggestion span and the variable is used as an
364
- // index/elsewhere later, but its declaration is outside the suggestion span
360
+ // - Variable initialization is outside the suggestion span
361
+ // - Variable declaration is outside the suggestion span
362
+ // - Variable is not used as an index or elsewhere later
365
363
if !self . suggest_span . contains ( init. span )
364
+ || path_to_local ( expr)
365
+ . is_some_and ( |hir_id| !self . suggest_span . contains ( self . cx . tcx . hir_span ( hir_id) ) )
366
366
|| !self . is_used_other_than_swapping ( first_segment. ident )
367
- || self
368
- . get_res_span ( expr)
369
- . is_some_and ( |span| !self . suggest_span . contains ( span) )
370
367
{
371
368
return String :: new ( ) ;
372
369
}
@@ -383,21 +380,6 @@ impl<'tcx> IndexBinding<'_, 'tcx> {
383
380
}
384
381
}
385
382
386
- fn get_res_span ( & self , expr : & ' tcx Expr < ' tcx > ) -> Option < Span > {
387
- if let ExprKind :: Path ( QPath :: Resolved (
388
- _,
389
- Path {
390
- res : rustc_hir:: def:: Res :: Local ( hir_id) ,
391
- ..
392
- } ,
393
- ) ) = expr. kind
394
- {
395
- Some ( self . cx . tcx . hir_span ( * hir_id) )
396
- } else {
397
- None
398
- }
399
- }
400
-
401
383
fn is_used_other_than_swapping ( & mut self , idx_ident : Ident ) -> bool {
402
384
if Self :: is_used_slice_indexed ( self . swap1_idx , idx_ident)
403
385
|| Self :: is_used_slice_indexed ( self . swap2_idx , idx_ident)
0 commit comments