From 5656510eed663b5d5be6b6df3d033b836da5f759 Mon Sep 17 00:00:00 2001
From: daxpedda <daxpedda@gmail.com>
Date: Wed, 3 Mar 2021 17:32:49 +0100
Subject: [PATCH] Fix false-positive in `use_self`

---
 clippy_lints/src/use_self.rs | 17 +++++++++++------
 tests/ui/use_self.fixed      |  7 +++++++
 tests/ui/use_self.rs         |  7 +++++++
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs
index be7b9e9ff2d..c262ec993b1 100644
--- a/clippy_lints/src/use_self.rs
+++ b/clippy_lints/src/use_self.rs
@@ -262,12 +262,17 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
             // FIXME: this span manipulation should not be necessary
             // @flip1995 found an ast lowering issue in
             // https://github.com/rust-lang/rust/blob/master/src/librustc_ast_lowering/path.rs#l142-l162
-            match cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_ty.hir_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),
+            let hir = cx.tcx.hir();
+            let id = hir.get_parent_node(hir_ty.hir_id);
+
+            if !hir.opt_span(id).map(in_macro).unwrap_or(false) {
+                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),
+                }
             }
         }
     }
diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed
index 2b22a2ed2d5..a630936e3b1 100644
--- a/tests/ui/use_self.fixed
+++ b/tests/ui/use_self.fixed
@@ -454,3 +454,10 @@ mod nested_paths {
         }
     }
 }
+
+mod issue6818 {
+    #[derive(serde::Deserialize)]
+    struct A {
+        a: i32,
+    }
+}
diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs
index 609625abdec..f3e081dd203 100644
--- a/tests/ui/use_self.rs
+++ b/tests/ui/use_self.rs
@@ -454,3 +454,10 @@ mod nested_paths {
         }
     }
 }
+
+mod issue6818 {
+    #[derive(serde::Deserialize)]
+    struct A {
+        a: i32,
+    }
+}