Skip to content

Commit 7f61ddd

Browse files
camsteffenflip1995
authored andcommitted
Move "types to lint" to the item stack
1 parent 7e1c1c1 commit 7f61ddd

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

clippy_lints/src/use_self.rs

+41-32
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ declare_clippy_lint! {
5959
pub struct UseSelf {
6060
msrv: Option<RustcVersion>,
6161
stack: Vec<StackItem>,
62-
types_to_skip: Vec<HirId>,
63-
types_to_lint: Vec<HirId>,
6462
}
6563

6664
const USE_SELF_MSRV: RustcVersion = RustcVersion::new(1, 37, 0);
@@ -75,11 +73,13 @@ impl UseSelf {
7573
}
7674
}
7775

78-
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
76+
#[derive(Debug)]
7977
enum StackItem {
8078
Check {
8179
hir_id: HirId,
8280
impl_trait_ref_def_id: Option<LocalDefId>,
81+
types_to_skip: Vec<HirId>,
82+
types_to_lint: Vec<HirId>,
8383
},
8484
NoCheck,
8585
}
@@ -116,6 +116,8 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
116116
self.stack.push(StackItem::Check {
117117
hir_id: hir_self_ty.hir_id,
118118
impl_trait_ref_def_id,
119+
types_to_lint: Vec::new(),
120+
types_to_skip: Vec::new(),
119121
});
120122
} else {
121123
self.stack.push(StackItem::NoCheck);
@@ -149,7 +151,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
149151
// declaration. The collection of those types is all this method implementation does.
150152
if_chain! {
151153
if let ImplItemKind::Fn(FnSig { decl, .. }, ..) = impl_item.kind;
152-
if let Some(StackItem::Check { impl_trait_ref_def_id: Some(def_id), .. }) = self.stack.last().copied();
154+
if let Some(&mut StackItem::Check {
155+
impl_trait_ref_def_id: Some(def_id),
156+
ref mut types_to_skip,
157+
..
158+
}) = self.stack.last_mut();
153159
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(def_id);
154160
then {
155161
// `self_ty` is the semantic self type of `impl <trait> for <type>`. This cannot be
@@ -191,17 +197,13 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
191197
if trait_sem_ty.walk().any(|inner| inner == self_ty.into()) {
192198
let mut visitor = SkipTyCollector::default();
193199
visitor.visit_ty(&impl_hir_ty);
194-
self.types_to_skip.extend(visitor.types_to_skip);
200+
types_to_skip.extend(visitor.types_to_skip);
195201
}
196202
}
197203
}
198204
}
199205
}
200206

201-
fn check_impl_item_post(&mut self, _: &LateContext<'_>, _: &hir::ImplItem<'_>) {
202-
self.types_to_skip.clear();
203-
}
204-
205207
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) {
206208
// `hir_ty_to_ty` cannot be called in `Body`s or it will panic (sometimes). But in bodies
207209
// we can use `cx.typeck_results.node_type(..)` to get the `ty::Ty` from a `hir::Ty`.
@@ -211,7 +213,13 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
211213
// which shouldn't, with a visitor. We could directly lint in the visitor, but then we
212214
// could only allow this lint on item scope. And we would have to check if those types are
213215
// already dealt with in `check_ty` anyway.
214-
if let Some(StackItem::Check { hir_id, .. }) = self.stack.last() {
216+
if let Some(StackItem::Check {
217+
hir_id,
218+
types_to_lint,
219+
types_to_skip,
220+
..
221+
}) = self.stack.last_mut()
222+
{
215223
let self_ty = ty_from_hir_id(cx, *hir_id);
216224

217225
let mut visitor = LintTyCollector {
@@ -221,25 +229,36 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
221229
types_to_skip: vec![],
222230
};
223231
visitor.visit_expr(&body.value);
224-
self.types_to_lint.extend(visitor.types_to_lint);
225-
self.types_to_skip.extend(visitor.types_to_skip);
232+
types_to_lint.extend(visitor.types_to_lint);
233+
types_to_skip.extend(visitor.types_to_skip);
226234
}
227235
}
228236

229-
fn check_body_post(&mut self, _: &LateContext<'_>, _: &hir::Body<'_>) {
230-
self.types_to_lint.clear();
231-
}
232-
233237
fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
234-
if in_macro(hir_ty.span)
235-
| in_impl(cx, hir_ty)
236-
| self.types_to_skip.contains(&hir_ty.hir_id)
237-
| !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV)
238-
{
238+
if in_macro(hir_ty.span) | in_impl(cx, hir_ty) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
239239
return;
240240
}
241241

242-
let lint_dependend_on_expr_kind = || {
242+
let lint_dependend_on_expr_kind = if let Some(StackItem::Check {
243+
hir_id,
244+
types_to_lint,
245+
types_to_skip,
246+
..
247+
}) = self.stack.last()
248+
{
249+
if types_to_skip.contains(&hir_ty.hir_id) {
250+
false
251+
} else if types_to_lint.contains(&hir_ty.hir_id) {
252+
true
253+
} else {
254+
let self_ty = ty_from_hir_id(cx, *hir_id);
255+
should_lint_ty(hir_ty, hir_ty_to_ty(cx.tcx, hir_ty), self_ty)
256+
}
257+
} else {
258+
false
259+
};
260+
261+
if lint_dependend_on_expr_kind {
243262
// FIXME: this span manipulation should not be necessary
244263
// @flip1995 found an ast lowering issue in
245264
// https://github.com/rust-lang/rust/blob/master/src/librustc_ast_lowering/path.rs#l142-l162
@@ -250,16 +269,6 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
250269
})) => span_lint_until_last_segment(cx, hir_ty.span, segment),
251270
_ => span_lint(cx, hir_ty.span),
252271
}
253-
};
254-
255-
if self.types_to_lint.contains(&hir_ty.hir_id) {
256-
lint_dependend_on_expr_kind();
257-
} else if let Some(StackItem::Check { hir_id, .. }) = self.stack.last() {
258-
let self_ty = ty_from_hir_id(cx, *hir_id);
259-
260-
if should_lint_ty(hir_ty, hir_ty_to_ty(cx.tcx, hir_ty), self_ty) {
261-
lint_dependend_on_expr_kind();
262-
}
263272
}
264273
}
265274

0 commit comments

Comments
 (0)