Skip to content

Commit 2c524c0

Browse files
committed
use_self: Fix false positive in macro expansion
1 parent b80903b commit 2c524c0

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

clippy_lints/src/use_self.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
236236
}
237237

238238
fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
239-
if in_macro(hir_ty.span) | in_impl(cx, hir_ty) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
239+
if in_macro_recursively(cx, hir_ty.hir_id)
240+
| in_impl(cx, hir_ty)
241+
| !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV)
242+
{
240243
return;
241244
}
242245

@@ -265,15 +268,12 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
265268
// https://github.com/rust-lang/rust/blob/master/src/librustc_ast_lowering/path.rs#l142-l162
266269
let hir = cx.tcx.hir();
267270
let id = hir.get_parent_node(hir_ty.hir_id);
268-
269-
if !hir.opt_span(id).map_or(false, in_macro) {
270-
match hir.find(id) {
271-
Some(Node::Expr(Expr {
272-
kind: ExprKind::Path(QPath::TypeRelative(_, segment)),
273-
..
274-
})) => span_lint_until_last_segment(cx, hir_ty.span, segment),
275-
_ => span_lint(cx, hir_ty.span),
276-
}
271+
match hir.find(id) {
272+
Some(Node::Expr(Expr {
273+
kind: ExprKind::Path(QPath::TypeRelative(_, segment)),
274+
..
275+
})) => span_lint_until_last_segment(cx, hir_ty.span, segment),
276+
_ => span_lint(cx, hir_ty.span),
277277
}
278278
}
279279
}
@@ -288,7 +288,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
288288
}
289289
}
290290

291-
if in_macro(expr.span) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
291+
if in_macro_recursively(cx, expr.hir_id) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
292292
return;
293293
}
294294

@@ -467,3 +467,18 @@ fn should_lint_ty(hir_ty: &hir::Ty<'_>, ty: Ty<'_>, self_ty: Ty<'_>) -> bool {
467467
}
468468
}
469469
}
470+
471+
fn in_macro_recursively(cx: &LateContext<'_>, hir_id: HirId) -> bool {
472+
let map = cx.tcx.hir();
473+
if map.opt_span(hir_id).map_or(false, in_macro) {
474+
return true;
475+
}
476+
477+
for (parent_id, _) in map.parent_iter(hir_id) {
478+
if map.opt_span(parent_id).map_or(false, in_macro) {
479+
return true;
480+
}
481+
}
482+
483+
false
484+
}

tests/ui/use_self.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,11 @@ mod issue6818 {
461461
a: i32,
462462
}
463463
}
464+
465+
mod issue6902 {
466+
#[derive(serde::Deserialize)]
467+
#[serde(untagged)]
468+
enum Direction {
469+
North,
470+
}
471+
}

tests/ui/use_self.rs

+8
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,11 @@ mod issue6818 {
461461
a: i32,
462462
}
463463
}
464+
465+
mod issue6902 {
466+
#[derive(serde::Deserialize)]
467+
#[serde(untagged)]
468+
enum Direction {
469+
North,
470+
}
471+
}

0 commit comments

Comments
 (0)