about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-08-26 13:29:01 +0000
committerbors <bors@rust-lang.org>2019-08-26 13:29:01 +0000
commitfea888f2ecd95d0a959637e9e0011a0f11bd9d72 (patch)
tree17906dd8d2e3762b89b37e7075aba99d9238d75b
parentf7600888205734d7c7b0ed2ec7db535f3d97873a (diff)
parent957dedb11d521af6f831e33d94394998fd21968d (diff)
downloadrust-fea888f2ecd95d0a959637e9e0011a0f11bd9d72.tar.gz
rust-fea888f2ecd95d0a959637e9e0011a0f11bd9d72.zip
Auto merge of #4445 - phansch:fix_unused_unit_sugg, r=flip1995
Fix unused_unit false positive

changelog: Fix `unused_unit` false positive

For some reason the `expr` of `stmt.node` didn't contain the expansion information, but the `stmt.span` does.

Fixes #4076
-rw-r--r--clippy_lints/src/returns.rs2
-rw-r--r--tests/ui/unused_unit.fixed14
-rw-r--r--tests/ui/unused_unit.rs14
-rw-r--r--tests/ui/unused_unit.stderr14
4 files changed, 36 insertions, 8 deletions
diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs
index 3cc053a0ebf..e864518ee59 100644
--- a/clippy_lints/src/returns.rs
+++ b/clippy_lints/src/returns.rs
@@ -277,7 +277,7 @@ impl EarlyLintPass for Return {
         if_chain! {
             if let Some(ref stmt) = block.stmts.last();
             if let ast::StmtKind::Expr(ref expr) = stmt.node;
-            if is_unit_expr(expr) && !expr.span.from_expansion();
+            if is_unit_expr(expr) && !stmt.span.from_expansion();
             then {
                 let sp = expr.span;
                 span_lint_and_then(cx, UNUSED_UNIT, sp, "unneeded unit expression", |db| {
diff --git a/tests/ui/unused_unit.fixed b/tests/ui/unused_unit.fixed
index 3c9e91a19f9..17c1a5de597 100644
--- a/tests/ui/unused_unit.fixed
+++ b/tests/ui/unused_unit.fixed
@@ -10,6 +10,7 @@
 #![rustfmt::skip]
 
 #![deny(clippy::unused_unit)]
+#![allow(dead_code)]
 
 struct Unitter;
 impl Unitter {
@@ -42,3 +43,16 @@ fn main() {
     }
     return;
 }
+
+// https://github.com/rust-lang/rust-clippy/issues/4076
+fn foo() {
+    macro_rules! foo {
+        (recv($r:expr) -> $res:pat => $body:expr) => {
+            $body
+        }
+    }
+
+    foo! {
+        recv(rx) -> _x => ()
+    }
+}
diff --git a/tests/ui/unused_unit.rs b/tests/ui/unused_unit.rs
index 1acd427be1e..e04c5257337 100644
--- a/tests/ui/unused_unit.rs
+++ b/tests/ui/unused_unit.rs
@@ -10,6 +10,7 @@
 #![rustfmt::skip]
 
 #![deny(clippy::unused_unit)]
+#![allow(dead_code)]
 
 struct Unitter;
 impl Unitter {
@@ -43,3 +44,16 @@ fn main() {
     }
     return();
 }
+
+// https://github.com/rust-lang/rust-clippy/issues/4076
+fn foo() {
+    macro_rules! foo {
+        (recv($r:expr) -> $res:pat => $body:expr) => {
+            $body
+        }
+    }
+
+    foo! {
+        recv(rx) -> _x => ()
+    }
+}
diff --git a/tests/ui/unused_unit.stderr b/tests/ui/unused_unit.stderr
index c33a220b98c..6ef6dc4f5d6 100644
--- a/tests/ui/unused_unit.stderr
+++ b/tests/ui/unused_unit.stderr
@@ -1,5 +1,5 @@
 error: unneeded unit return type
-  --> $DIR/unused_unit.rs:18:59
+  --> $DIR/unused_unit.rs:19:59
    |
 LL |       pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) ->
    |  ___________________________________________________________^
@@ -13,37 +13,37 @@ LL | #![deny(clippy::unused_unit)]
    |         ^^^^^^^^^^^^^^^^^^^
 
 error: unneeded unit return type
-  --> $DIR/unused_unit.rs:28:19
+  --> $DIR/unused_unit.rs:29:19
    |
 LL |     fn into(self) -> () {
    |                   ^^^^^ help: remove the `-> ()`
 
 error: unneeded unit expression
-  --> $DIR/unused_unit.rs:29:9
+  --> $DIR/unused_unit.rs:30:9
    |
 LL |         ()
    |         ^^ help: remove the final `()`
 
 error: unneeded unit return type
-  --> $DIR/unused_unit.rs:33:18
+  --> $DIR/unused_unit.rs:34:18
    |
 LL | fn return_unit() -> () { () }
    |                  ^^^^^ help: remove the `-> ()`
 
 error: unneeded unit expression
-  --> $DIR/unused_unit.rs:33:26
+  --> $DIR/unused_unit.rs:34:26
    |
 LL | fn return_unit() -> () { () }
    |                          ^^ help: remove the final `()`
 
 error: unneeded `()`
-  --> $DIR/unused_unit.rs:42:14
+  --> $DIR/unused_unit.rs:43:14
    |
 LL |         break();
    |              ^^ help: remove the `()`
 
 error: unneeded `()`
-  --> $DIR/unused_unit.rs:44:11
+  --> $DIR/unused_unit.rs:45:11
    |
 LL |     return();
    |           ^^ help: remove the `()`