about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-05-05 11:45:34 +0000
committerbors <bors@rust-lang.org>2021-05-05 11:45:34 +0000
commit7e538e3522797ed8fb9d4cbf6a01602fa76cae65 (patch)
tree2fe84b936299a2f68e2069d1d4a72a32a1cceb61
parenta8f28b636515cd859d8c9cc78dd17794f8a3c0ad (diff)
parent83329ec7050da5093b0310ba3140c8c5ec093321 (diff)
downloadrust-7e538e3522797ed8fb9d4cbf6a01602fa76cae65.tar.gz
rust-7e538e3522797ed8fb9d4cbf6a01602fa76cae65.zip
Auto merge of #7167 - camsteffen:unused-unit-macro, r=flip1995
Fix unused_unit macro false positive

changelog: Fix [`unused_unit`] false positive with macros

Fixes #7055
-rw-r--r--clippy_lints/src/unused_unit.rs4
-rw-r--r--tests/ui/unused_unit.fixed7
-rw-r--r--tests/ui/unused_unit.rs7
3 files changed, 17 insertions, 1 deletions
diff --git a/clippy_lints/src/unused_unit.rs b/clippy_lints/src/unused_unit.rs
index ce2d0b3ab2f..e14945651f5 100644
--- a/clippy_lints/src/unused_unit.rs
+++ b/clippy_lints/src/unused_unit.rs
@@ -47,7 +47,9 @@ impl EarlyLintPass for UnusedUnit {
         if_chain! {
             if let Some(stmt) = block.stmts.last();
             if let ast::StmtKind::Expr(ref expr) = stmt.kind;
-            if is_unit_expr(expr) && !stmt.span.from_expansion();
+            if is_unit_expr(expr);
+            let ctxt = block.span.ctxt();
+            if stmt.span.ctxt() == ctxt && expr.span.ctxt() == ctxt;
             then {
                 let sp = expr.span;
                 span_lint_and_sugg(
diff --git a/tests/ui/unused_unit.fixed b/tests/ui/unused_unit.fixed
index a192ebde3eb..7bb43cf7ae8 100644
--- a/tests/ui/unused_unit.fixed
+++ b/tests/ui/unused_unit.fixed
@@ -80,3 +80,10 @@ fn test2(){}
 
 #[rustfmt::skip]
 fn test3(){}
+
+fn macro_expr() {
+    macro_rules! e {
+        () => (());
+    }
+    e!()
+}
diff --git a/tests/ui/unused_unit.rs b/tests/ui/unused_unit.rs
index 96041a7dd85..21073fb802a 100644
--- a/tests/ui/unused_unit.rs
+++ b/tests/ui/unused_unit.rs
@@ -80,3 +80,10 @@ fn test2() ->(){}
 
 #[rustfmt::skip]
 fn test3()-> (){}
+
+fn macro_expr() {
+    macro_rules! e {
+        () => (());
+    }
+    e!()
+}