about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-02-25 21:16:02 +0000
committerbors <bors@rust-lang.org>2021-02-25 21:16:02 +0000
commitd5223be2e30a9d116b839bda418bec99d2949a68 (patch)
treec184e67b13c36ab2e3bdfa7da63ca8f78a439524
parent5c6cd87b94cb59efff08c0f0e44c376f92afa7e5 (diff)
parentcbe6eec3e67347af130769ad4a3bd2168997b411 (diff)
downloadrust-d5223be2e30a9d116b839bda418bec99d2949a68.tar.gz
rust-d5223be2e30a9d116b839bda418bec99d2949a68.zip
Auto merge of #6601 - mdm:fix-unit-args-false-positive, r=camsteffen
Fix false positive for unit_arg lint

Fixes #6447

To avoid false positives don't complain about unit args when they come from a path expression, e.g. a local variable.

**Note:** This is my first contribution to Clippy, so I might have messed up somewhere. Any feedback is welcome and I'm happy to work out any kinks.

---

changelog: Do not lint unit arguments when they come from a path expression.
-rw-r--r--clippy_lints/src/types.rs5
-rw-r--r--tests/ui/unit_arg.rs30
-rw-r--r--tests/ui/unit_arg.stderr24
3 files changed, 45 insertions, 14 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs
index 05754503163..af8d6865f93 100644
--- a/clippy_lints/src/types.rs
+++ b/clippy_lints/src/types.rs
@@ -955,7 +955,10 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
                     .iter()
                     .filter(|arg| {
                         if is_unit(cx.typeck_results().expr_ty(arg)) && !is_unit_literal(arg) {
-                            !matches!(&arg.kind, ExprKind::Match(.., MatchSource::TryDesugar))
+                            !matches!(
+                                &arg.kind,
+                                ExprKind::Match(.., MatchSource::TryDesugar) | ExprKind::Path(..)
+                            )
                         } else {
                             false
                         }
diff --git a/tests/ui/unit_arg.rs b/tests/ui/unit_arg.rs
index b6a7bc5a1cc..938cc3c7859 100644
--- a/tests/ui/unit_arg.rs
+++ b/tests/ui/unit_arg.rs
@@ -27,6 +27,30 @@ impl Bar {
     }
 }
 
+fn baz<T: Debug>(t: T) {
+    foo(t);
+}
+
+trait Tr {
+    type Args;
+    fn do_it(args: Self::Args);
+}
+
+struct A;
+impl Tr for A {
+    type Args = ();
+    fn do_it(_: Self::Args) {}
+}
+
+struct B;
+impl Tr for B {
+    type Args = <A as Tr>::Args;
+
+    fn do_it(args: Self::Args) {
+        A::do_it(args)
+    }
+}
+
 fn bad() {
     foo({
         1;
@@ -59,7 +83,7 @@ fn bad() {
     None.or(Some(foo(2)));
     // in this case, the suggestion can be inlined, no need for a surrounding block
     // foo(()); foo(()) instead of { foo(()); foo(()) }
-    foo(foo(()))
+    foo(foo(()));
 }
 
 fn ok() {
@@ -71,6 +95,10 @@ fn ok() {
     b.bar({ 1 });
     b.bar(());
     question_mark();
+    let named_unit_arg = ();
+    foo(named_unit_arg);
+    baz(());
+    B::do_it(());
 }
 
 fn question_mark() -> Result<(), ()> {
diff --git a/tests/ui/unit_arg.stderr b/tests/ui/unit_arg.stderr
index 094cff8c985..354fd51cd6b 100644
--- a/tests/ui/unit_arg.stderr
+++ b/tests/ui/unit_arg.stderr
@@ -1,5 +1,5 @@
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:31:5
+  --> $DIR/unit_arg.rs:55:5
    |
 LL | /     foo({
 LL | |         1;
@@ -20,7 +20,7 @@ LL |     foo(());
    |
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:34:5
+  --> $DIR/unit_arg.rs:58:5
    |
 LL |     foo(foo(1));
    |     ^^^^^^^^^^^
@@ -32,7 +32,7 @@ LL |     foo(());
    |
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:35:5
+  --> $DIR/unit_arg.rs:59:5
    |
 LL | /     foo({
 LL | |         foo(1);
@@ -54,7 +54,7 @@ LL |     foo(());
    |
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:40:5
+  --> $DIR/unit_arg.rs:64:5
    |
 LL | /     b.bar({
 LL | |         1;
@@ -74,7 +74,7 @@ LL |     b.bar(());
    |
 
 error: passing unit values to a function
-  --> $DIR/unit_arg.rs:43:5
+  --> $DIR/unit_arg.rs:67:5
    |
 LL |     taking_multiple_units(foo(0), foo(1));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -87,7 +87,7 @@ LL |     taking_multiple_units((), ());
    |
 
 error: passing unit values to a function
-  --> $DIR/unit_arg.rs:44:5
+  --> $DIR/unit_arg.rs:68:5
    |
 LL | /     taking_multiple_units(foo(0), {
 LL | |         foo(1);
@@ -110,7 +110,7 @@ LL |     taking_multiple_units((), ());
    |
 
 error: passing unit values to a function
-  --> $DIR/unit_arg.rs:48:5
+  --> $DIR/unit_arg.rs:72:5
    |
 LL | /     taking_multiple_units(
 LL | |         {
@@ -140,7 +140,7 @@ LL |         foo(2);
  ...
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:59:13
+  --> $DIR/unit_arg.rs:83:13
    |
 LL |     None.or(Some(foo(2)));
    |             ^^^^^^^^^^^^
@@ -154,19 +154,19 @@ LL |     });
    |
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:62:5
+  --> $DIR/unit_arg.rs:86:5
    |
-LL |     foo(foo(()))
+LL |     foo(foo(()));
    |     ^^^^^^^^^^^^
    |
 help: move the expression in front of the call and replace it with the unit literal `()`
    |
 LL |     foo(());
-LL |     foo(())
+LL |     foo(());
    |
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:95:5
+  --> $DIR/unit_arg.rs:123:5
    |
 LL |     Some(foo(1))
    |     ^^^^^^^^^^^^