Skip to content

Commit a5d6855

Browse files
committed
Use LocalUsedVisitor in more places
1 parent fff5fa6 commit a5d6855

File tree

2 files changed

+8
-74
lines changed

2 files changed

+8
-74
lines changed

clippy_lints/src/let_if_seq.rs

+6-47
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
use crate::utils::visitors::LocalUsedVisitor;
12
use crate::utils::{higher, qpath_res, snippet, span_lint_and_then};
23
use if_chain::if_chain;
34
use rustc_errors::Applicability;
45
use rustc_hir as hir;
56
use rustc_hir::def::Res;
6-
use rustc_hir::intravisit;
77
use rustc_hir::BindingAnnotation;
88
use rustc_lint::{LateContext, LateLintPass};
9-
use rustc_middle::hir::map::Map;
109
use rustc_session::{declare_lint_pass, declare_tool_lint};
1110

1211
declare_clippy_lint! {
@@ -66,10 +65,10 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
6665
if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind;
6766
if let hir::StmtKind::Expr(ref if_) = expr.kind;
6867
if let Some((ref cond, ref then, ref else_)) = higher::if_block(&if_);
69-
if !used_in_expr(cx, canonical_id, cond);
68+
if !LocalUsedVisitor::new(canonical_id).check_expr(cond);
7069
if let hir::ExprKind::Block(ref then, _) = then.kind;
7170
if let Some(value) = check_assign(cx, canonical_id, &*then);
72-
if !used_in_expr(cx, canonical_id, value);
71+
if !LocalUsedVisitor::new(canonical_id).check_expr(value);
7372
then {
7473
let span = stmt.span.to(if_.span);
7574

@@ -136,32 +135,6 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
136135
}
137136
}
138137

139-
struct UsedVisitor<'a, 'tcx> {
140-
cx: &'a LateContext<'tcx>,
141-
id: hir::HirId,
142-
used: bool,
143-
}
144-
145-
impl<'a, 'tcx> intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
146-
type Map = Map<'tcx>;
147-
148-
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
149-
if_chain! {
150-
if let hir::ExprKind::Path(ref qpath) = expr.kind;
151-
if let Res::Local(local_id) = qpath_res(self.cx, qpath, expr.hir_id);
152-
if self.id == local_id;
153-
then {
154-
self.used = true;
155-
return;
156-
}
157-
}
158-
intravisit::walk_expr(self, expr);
159-
}
160-
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
161-
intravisit::NestedVisitorMap::None
162-
}
163-
}
164-
165138
fn check_assign<'tcx>(
166139
cx: &LateContext<'tcx>,
167140
decl: hir::HirId,
@@ -176,18 +149,10 @@ fn check_assign<'tcx>(
176149
if let Res::Local(local_id) = qpath_res(cx, qpath, var.hir_id);
177150
if decl == local_id;
178151
then {
179-
let mut v = UsedVisitor {
180-
cx,
181-
id: decl,
182-
used: false,
183-
};
184-
185-
for s in block.stmts.iter().take(block.stmts.len()-1) {
186-
intravisit::walk_stmt(&mut v, s);
152+
let mut v = LocalUsedVisitor::new(decl);
187153

188-
if v.used {
189-
return None;
190-
}
154+
if block.stmts.iter().take(block.stmts.len()-1).any(|stmt| v.check_stmt(stmt)) {
155+
return None;
191156
}
192157

193158
return Some(value);
@@ -196,9 +161,3 @@ fn check_assign<'tcx>(
196161

197162
None
198163
}
199-
200-
fn used_in_expr<'tcx>(cx: &LateContext<'tcx>, id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> bool {
201-
let mut v = UsedVisitor { cx, id, used: false };
202-
intravisit::walk_expr(&mut v, expr);
203-
v.used
204-
}

clippy_lints/src/loops.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::consts::constant;
22
use crate::utils::paths;
33
use crate::utils::sugg::Sugg;
44
use crate::utils::usage::{is_unused, mutated_variables};
5+
use crate::utils::visitors::LocalUsedVisitor;
56
use crate::utils::{
67
contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
78
indent_of, is_in_panic_handler, is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item,
@@ -2069,28 +2070,6 @@ fn pat_is_wild<'tcx>(pat: &'tcx PatKind<'_>, body: &'tcx Expr<'_>) -> bool {
20692070
}
20702071
}
20712072

2072-
struct LocalUsedVisitor<'a, 'tcx> {
2073-
cx: &'a LateContext<'tcx>,
2074-
local: HirId,
2075-
used: bool,
2076-
}
2077-
2078-
impl<'a, 'tcx> Visitor<'tcx> for LocalUsedVisitor<'a, 'tcx> {
2079-
type Map = Map<'tcx>;
2080-
2081-
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
2082-
if same_var(self.cx, expr, self.local) {
2083-
self.used = true;
2084-
} else {
2085-
walk_expr(self, expr);
2086-
}
2087-
}
2088-
2089-
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
2090-
NestedVisitorMap::None
2091-
}
2092-
}
2093-
20942073
struct VarVisitor<'a, 'tcx> {
20952074
/// context reference
20962075
cx: &'a LateContext<'tcx>,
@@ -2124,11 +2103,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
21242103
then {
21252104
let index_used_directly = same_var(self.cx, idx, self.var);
21262105
let indexed_indirectly = {
2127-
let mut used_visitor = LocalUsedVisitor {
2128-
cx: self.cx,
2129-
local: self.var,
2130-
used: false,
2131-
};
2106+
let mut used_visitor = LocalUsedVisitor::new(self.var);
21322107
walk_expr(&mut used_visitor, idx);
21332108
used_visitor.used
21342109
};

0 commit comments

Comments
 (0)