]> git.proxmox.com Git - rustc.git/blobdiff - src/tools/clippy/clippy_lints/src/unused_self.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / tools / clippy / clippy_lints / src / unused_self.rs
index 5349c4f7eb8a78251c51bd412f3171afdb46dd1a..812482cf5cfb6638bdbb7eeaa18461c6937a8467 100644 (file)
@@ -1,12 +1,10 @@
 use if_chain::if_chain;
-use rustc_hir::def::Res;
-use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
-use rustc_hir::{HirId, Impl, ImplItem, ImplItemKind, ItemKind, Path};
+use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind};
 use rustc_lint::{LateContext, LateLintPass};
-use rustc_middle::hir::map::Map;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
 use crate::utils::span_lint_and_help;
+use crate::utils::visitors::LocalUsedVisitor;
 
 declare_clippy_lint! {
     /// **What it does:** Checks methods that contain a `self` argument but don't use it
@@ -44,10 +42,9 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
         if impl_item.span.from_expansion() {
             return;
         }
-        let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
+        let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
         let parent_item = cx.tcx.hir().expect_item(parent);
-        let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
-        let assoc_item = cx.tcx.associated_item(def_id);
+        let assoc_item = cx.tcx.associated_item(impl_item.def_id);
         if_chain! {
             if let ItemKind::Impl(Impl { of_trait: None, .. }) = parent_item.kind;
             if assoc_item.fn_has_self_parameter;
@@ -57,13 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
             then {
                 let self_param = &body.params[0];
                 let self_hir_id = self_param.pat.hir_id;
-                let mut visitor = UnusedSelfVisitor {
-                    cx,
-                    uses_self: false,
-                    self_hir_id: &self_hir_id,
-                };
-                visitor.visit_body(body);
-                if !visitor.uses_self {
+                if !LocalUsedVisitor::new(cx, self_hir_id).check_body(body) {
                     span_lint_and_help(
                         cx,
                         UNUSED_SELF,
@@ -78,28 +69,3 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
         }
     }
 }
-
-struct UnusedSelfVisitor<'a, 'tcx> {
-    cx: &'a LateContext<'tcx>,
-    uses_self: bool,
-    self_hir_id: &'a HirId,
-}
-
-impl<'a, 'tcx> Visitor<'tcx> for UnusedSelfVisitor<'a, 'tcx> {
-    type Map = Map<'tcx>;
-
-    fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
-        if self.uses_self {
-            // This function already uses `self`
-            return;
-        }
-        if let Res::Local(hir_id) = &path.res {
-            self.uses_self = self.self_hir_id == hir_id
-        }
-        walk_path(self, path);
-    }
-
-    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
-        NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
-    }
-}