about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBoot0x7c00 <boot0x7c00@outlook.com>2025-06-09 03:05:28 +0000
committerBoot0x7c00 <boot0x7c00@outlook.com>2025-06-09 03:05:28 +0000
commit4389e177ebb36e6f7ea9dfa496cca46e0c6f3f8a (patch)
treee3f6bd5f968c55a973a0e1cfe531161f79488d69
parent29492da34bccad34ec1532e2825e43fba8aac4ff (diff)
downloadrust-4389e177ebb36e6f7ea9dfa496cca46e0c6f3f8a.tar.gz
rust-4389e177ebb36e6f7ea9dfa496cca46e0c6f3f8a.zip
optimize codes
-rw-r--r--clippy_lints/src/swap.rs32
1 files changed, 7 insertions, 25 deletions
diff --git a/clippy_lints/src/swap.rs b/clippy_lints/src/swap.rs
index dfc63ae7f6a..5ecbb56925e 100644
--- a/clippy_lints/src/swap.rs
+++ b/clippy_lints/src/swap.rs
@@ -3,14 +3,14 @@ use clippy_utils::source::{snippet_indent, snippet_with_context};
 use clippy_utils::sugg::Sugg;
 use clippy_utils::ty::is_type_diagnostic_item;
 
-use clippy_utils::{can_mut_borrow_both, eq_expr_value, is_in_const_context, std_or_core};
+use clippy_utils::{can_mut_borrow_both, eq_expr_value, is_in_const_context, path_to_local, std_or_core};
 use itertools::Itertools;
 
 use rustc_data_structures::fx::FxIndexSet;
 use rustc_hir::intravisit::{Visitor, walk_expr};
 
 use rustc_errors::Applicability;
-use rustc_hir::{AssignOpKind, Block, Expr, ExprKind, LetStmt, PatKind, Path, QPath, Stmt, StmtKind};
+use rustc_hir::{AssignOpKind, Block, Expr, ExprKind, LetStmt, PatKind, QPath, Stmt, StmtKind};
 use rustc_lint::{LateContext, LateLintPass, LintContext};
 use rustc_middle::ty;
 use rustc_session::declare_lint_pass;
@@ -357,16 +357,13 @@ impl<'tcx> IndexBinding<'_, 'tcx> {
                 let init = self.cx.expr_or_init(expr);
 
                 // We skip suggesting a variable binding in any of these cases:
-                // 1. Variable initialization is outside the suggestion span
-                // 2. Variable initialization is inside the suggestion span but the variable is not used as an index
-                //    or elsewhere later
-                // 3. Variable initialization is inside the suggestion span and the variable is used as an
-                //    index/elsewhere later, but its declaration is outside the suggestion span
+                // - Variable initialization is outside the suggestion span
+                // - Variable declaration is outside the suggestion span
+                // - Variable is not used as an index or elsewhere later
                 if !self.suggest_span.contains(init.span)
+                    || path_to_local(expr)
+                        .is_some_and(|hir_id| !self.suggest_span.contains(self.cx.tcx.hir_span(hir_id)))
                     || !self.is_used_other_than_swapping(first_segment.ident)
-                    || self
-                        .get_res_span(expr)
-                        .is_some_and(|span| !self.suggest_span.contains(span))
                 {
                     return String::new();
                 }
@@ -383,21 +380,6 @@ impl<'tcx> IndexBinding<'_, 'tcx> {
         }
     }
 
-    fn get_res_span(&self, expr: &'tcx Expr<'tcx>) -> Option<Span> {
-        if let ExprKind::Path(QPath::Resolved(
-            _,
-            Path {
-                res: rustc_hir::def::Res::Local(hir_id),
-                ..
-            },
-        )) = expr.kind
-        {
-            Some(self.cx.tcx.hir_span(*hir_id))
-        } else {
-            None
-        }
-    }
-
     fn is_used_other_than_swapping(&mut self, idx_ident: Ident) -> bool {
         if Self::is_used_slice_indexed(self.swap1_idx, idx_ident)
             || Self::is_used_slice_indexed(self.swap2_idx, idx_ident)