diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 116cb8b1e1c7..3224276df5aa 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -236,7 +236,10 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { } fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) { - if in_macro(hir_ty.span) | in_impl(cx, hir_ty) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) { + if in_macro_recusively(cx, hir_ty.hir_id) + | in_impl(cx, hir_ty) + | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) + { return; } @@ -265,15 +268,12 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { // https://github.com/rust-lang/rust/blob/master/src/librustc_ast_lowering/path.rs#l142-l162 let hir = cx.tcx.hir(); let id = hir.get_parent_node(hir_ty.hir_id); - - if !hir.opt_span(id).map_or(false, in_macro) { - match hir.find(id) { - Some(Node::Expr(Expr { - kind: ExprKind::Path(QPath::TypeRelative(_, segment)), - .. - })) => span_lint_until_last_segment(cx, hir_ty.span, segment), - _ => span_lint(cx, hir_ty.span), - } + match hir.find(id) { + Some(Node::Expr(Expr { + kind: ExprKind::Path(QPath::TypeRelative(_, segment)), + .. + })) => span_lint_until_last_segment(cx, hir_ty.span, segment), + _ => span_lint(cx, hir_ty.span), } } } @@ -288,7 +288,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { } } - if in_macro(expr.span) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) { + if in_macro_recusively(cx, expr.hir_id) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) { return; } @@ -467,3 +467,18 @@ fn should_lint_ty(hir_ty: &hir::Ty<'_>, ty: Ty<'_>, self_ty: Ty<'_>) -> bool { } } } + +fn in_macro_recusively(cx: &LateContext<'_>, hir_id: HirId) -> bool { + let map = cx.tcx.hir(); + if map.opt_span(hir_id).map_or(false, in_macro) { + return true; + } + + for (parent_id, _) in map.parent_iter(hir_id) { + if map.opt_span(parent_id).map_or(false, in_macro) { + return true; + } + } + + false +} diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index a6619f358920..3c22e3f0ee77 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -461,3 +461,11 @@ mod issue6818 { a: i32, } } + +mod issue6902 { + #[derive(serde::Deserialize)] + #[serde(untagged)] + enum Direction { + North, + } +} diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index 3c41ce500e0d..783ff34e8944 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -461,3 +461,11 @@ mod issue6818 { a: i32, } } + +mod issue6902 { + #[derive(serde::Deserialize)] + #[serde(untagged)] + enum Direction { + North, + } +}