about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCameron Steffen <cam.steffen94@gmail.com>2020-11-27 17:13:19 -0600
committerCameron Steffen <cam.steffen94@gmail.com>2020-11-29 15:02:27 -0600
commit6e1fbfdb8fe9a6b543fa2b0e688f928d2ee354b8 (patch)
treec953c9da8ec16c1c5837ee312091274949f548df
parent5df286b636e0bf71a665599f099da18a2b90936c (diff)
downloadrust-6e1fbfdb8fe9a6b543fa2b0e688f928d2ee354b8.tar.gz
rust-6e1fbfdb8fe9a6b543fa2b0e688f928d2ee354b8.zip
Add LocalUseVisitor
-rw-r--r--clippy_lints/src/utils/visitors.rs55
1 files changed, 54 insertions, 1 deletions
diff --git a/clippy_lints/src/utils/visitors.rs b/clippy_lints/src/utils/visitors.rs
index b0837b6c43e..28b3e79d7a6 100644
--- a/clippy_lints/src/utils/visitors.rs
+++ b/clippy_lints/src/utils/visitors.rs
@@ -1,5 +1,7 @@
 use rustc_hir as hir;
-use rustc_hir::intravisit::{self, Visitor};
+use rustc_hir::def::Res;
+use rustc_hir::intravisit::{self, walk_expr, NestedVisitorMap, Visitor};
+use rustc_hir::{Arm, Expr, ExprKind, HirId, QPath, Stmt};
 use rustc_lint::LateContext;
 use rustc_middle::hir::map::Map;
 
@@ -123,3 +125,54 @@ where
         !ret_finder.failed
     }
 }
+
+pub struct LocalUsedVisitor {
+    pub local_hir_id: HirId,
+    pub used: bool,
+}
+
+impl LocalUsedVisitor {
+    pub fn new(local_hir_id: HirId) -> Self {
+        Self {
+            local_hir_id,
+            used: false,
+        }
+    }
+
+    fn check<T>(&mut self, t: T, visit: fn(&mut Self, T)) -> bool {
+        visit(self, t);
+        std::mem::replace(&mut self.used, false)
+    }
+
+    pub fn check_arm(&mut self, arm: &Arm<'_>) -> bool {
+        self.check(arm, Self::visit_arm)
+    }
+
+    pub fn check_expr(&mut self, expr: &Expr<'_>) -> bool {
+        self.check(expr, Self::visit_expr)
+    }
+
+    pub fn check_stmt(&mut self, stmt: &Stmt<'_>) -> bool {
+        self.check(stmt, Self::visit_stmt)
+    }
+}
+
+impl<'v> Visitor<'v> for LocalUsedVisitor {
+    type Map = Map<'v>;
+
+    fn visit_expr(&mut self, expr: &'v Expr<'v>) {
+        if let ExprKind::Path(QPath::Resolved(None, path)) = expr.kind {
+            if let Res::Local(id) = path.res {
+                if id == self.local_hir_id {
+                    self.used = true;
+                    return;
+                }
+            }
+        }
+        walk_expr(self, expr);
+    }
+
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
+        NestedVisitorMap::None
+    }
+}