Skip to content

Commit 5db48a3

Browse files
committed
Refactor out UnusedSelfVisitor
1 parent 1d30422 commit 5db48a3

File tree

2 files changed

+8
-37
lines changed

2 files changed

+8
-37
lines changed

clippy_lints/src/unused_self.rs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use if_chain::if_chain;
2-
use rustc_hir::def::Res;
3-
use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
4-
use rustc_hir::{HirId, Impl, ImplItem, ImplItemKind, ItemKind, Path};
2+
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind};
53
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::hir::map::Map;
74
use rustc_session::{declare_lint_pass, declare_tool_lint};
85

96
use crate::utils::span_lint_and_help;
7+
use crate::utils::visitors::LocalUsedVisitor;
108

119
declare_clippy_lint! {
1210
/// **What it does:** Checks methods that contain a `self` argument but don't use it
@@ -57,13 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
5755
then {
5856
let self_param = &body.params[0];
5957
let self_hir_id = self_param.pat.hir_id;
60-
let mut visitor = UnusedSelfVisitor {
61-
cx,
62-
uses_self: false,
63-
self_hir_id: &self_hir_id,
64-
};
65-
visitor.visit_body(body);
66-
if !visitor.uses_self {
58+
if !LocalUsedVisitor::new(cx, self_hir_id).check_body(body) {
6759
span_lint_and_help(
6860
cx,
6961
UNUSED_SELF,
@@ -78,28 +70,3 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
7870
}
7971
}
8072
}
81-
82-
struct UnusedSelfVisitor<'a, 'tcx> {
83-
cx: &'a LateContext<'tcx>,
84-
uses_self: bool,
85-
self_hir_id: &'a HirId,
86-
}
87-
88-
impl<'a, 'tcx> Visitor<'tcx> for UnusedSelfVisitor<'a, 'tcx> {
89-
type Map = Map<'tcx>;
90-
91-
fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
92-
if self.uses_self {
93-
// This function already uses `self`
94-
return;
95-
}
96-
if let Res::Local(hir_id) = &path.res {
97-
self.uses_self = self.self_hir_id == hir_id
98-
}
99-
walk_path(self, path);
100-
}
101-
102-
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
103-
NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
104-
}
105-
}

clippy_lints/src/utils/visitors.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::path_to_local_id;
22
use rustc_hir as hir;
33
use rustc_hir::intravisit::{self, walk_expr, NestedVisitorMap, Visitor};
4-
use rustc_hir::{Arm, Expr, HirId, Stmt};
4+
use rustc_hir::{Arm, Body, Expr, HirId, Stmt};
55
use rustc_lint::LateContext;
66
use rustc_middle::hir::map::Map;
77

@@ -157,6 +157,10 @@ impl<'hir> LocalUsedVisitor<'hir> {
157157
self.check(arm, Self::visit_arm)
158158
}
159159

160+
pub fn check_body(&mut self, body: &'hir Body<'_>) -> bool {
161+
self.check(body, Self::visit_body)
162+
}
163+
160164
pub fn check_expr(&mut self, expr: &'hir Expr<'_>) -> bool {
161165
self.check(expr, Self::visit_expr)
162166
}

0 commit comments

Comments
 (0)