about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarc Dominik Migge <marcmigge@gmx.net>2021-02-24 13:31:04 +0100
committerMarc Dominik Migge <marcmigge@gmx.net>2021-02-24 13:31:04 +0100
commit9fe9d94abda8d2698d1dede6961007e0026760ea (patch)
treee4f89f6d93959cb10eb229f79bdb9a50b75d9f3e
parenteb476c6c70bccb87378462e4854f8b8fa12c40be (diff)
downloadrust-9fe9d94abda8d2698d1dede6961007e0026760ea.tar.gz
rust-9fe9d94abda8d2698d1dede6961007e0026760ea.zip
Don't lint unit args if expression kind is path
-rw-r--r--clippy_lints/src/types.rs14
-rw-r--r--tests/ui/unit_arg.rs5
-rw-r--r--tests/ui/unit_arg.stderr20
-rw-r--r--tests/ui/unit_arg_expressions.rs35
-rw-r--r--tests/ui/unit_arg_expressions.stderr41
5 files changed, 19 insertions, 96 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs
index 7d0eea37bc0..77cde8b60c1 100644
--- a/clippy_lints/src/types.rs
+++ b/clippy_lints/src/types.rs
@@ -955,16 +955,10 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
                     .iter()
                     .filter(|arg| {
                         if is_unit(cx.typeck_results().expr_ty(arg)) && !is_unit_literal(arg) {
-                            match &arg.kind {
-                                ExprKind::Block(..)
-                                | ExprKind::Call(..)
-                                | ExprKind::If(..)
-                                | ExprKind::MethodCall(..) => true,
-                                ExprKind::Match(..) => {
-                                    !matches!(&arg.kind, ExprKind::Match(.., MatchSource::TryDesugar))
-                                },
-                                _ => false,
-                            }
+                            !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 cce543006d7..5ea2e5d65c5 100644
--- a/tests/ui/unit_arg.rs
+++ b/tests/ui/unit_arg.rs
@@ -27,6 +27,10 @@ impl Bar {
     }
 }
 
+fn baz<T: Debug>(t: T) {
+    foo(t);
+}
+
 fn bad() {
     foo({
         1;
@@ -73,6 +77,7 @@ fn ok() {
     question_mark();
     let named_unit_arg = ();
     foo(named_unit_arg);
+    baz(());
 }
 
 fn question_mark() -> Result<(), ()> {
diff --git a/tests/ui/unit_arg.stderr b/tests/ui/unit_arg.stderr
index 8e3f1811c65..b3fe9addb62 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:35: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:38: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:39: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:44: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:47: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:48: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:52: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:63:13
    |
 LL |     None.or(Some(foo(2)));
    |             ^^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL |     });
    |
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:62:5
+  --> $DIR/unit_arg.rs:66:5
    |
 LL |     foo(foo(()));
    |     ^^^^^^^^^^^^
@@ -166,7 +166,7 @@ LL |     foo(());
    |
 
 error: passing a unit value to a function
-  --> $DIR/unit_arg.rs:97:5
+  --> $DIR/unit_arg.rs:102:5
    |
 LL |     Some(foo(1))
    |     ^^^^^^^^^^^^
diff --git a/tests/ui/unit_arg_expressions.rs b/tests/ui/unit_arg_expressions.rs
deleted file mode 100644
index a6807cb2e97..00000000000
--- a/tests/ui/unit_arg_expressions.rs
+++ /dev/null
@@ -1,35 +0,0 @@
-#![warn(clippy::unit_arg)]
-#![allow(clippy::no_effect)]
-
-use std::fmt::Debug;
-
-fn foo<T: Debug>(t: T) {
-    println!("{:?}", t);
-}
-
-fn bad() {
-    foo(if true {
-        1;
-    });
-    foo(match Some(1) {
-        Some(_) => {
-            1;
-        },
-        None => {
-            0;
-        },
-    });
-}
-
-fn ok() {
-    foo(if true { 1 } else { 0 });
-    foo(match Some(1) {
-        Some(_) => 1,
-        None => 0,
-    });
-}
-
-fn main() {
-    bad();
-    ok();
-}
diff --git a/tests/ui/unit_arg_expressions.stderr b/tests/ui/unit_arg_expressions.stderr
deleted file mode 100644
index 9fb08106b72..00000000000
--- a/tests/ui/unit_arg_expressions.stderr
+++ /dev/null
@@ -1,41 +0,0 @@
-error: passing a unit value to a function
-  --> $DIR/unit_arg_expressions.rs:11:5
-   |
-LL | /     foo(if true {
-LL | |         1;
-LL | |     });
-   | |______^
-   |
-   = note: `-D clippy::unit-arg` implied by `-D warnings`
-help: move the expression in front of the call and replace it with the unit literal `()`
-   |
-LL |     if true {
-LL |         1;
-LL |     };
-LL |     foo(());
-   |
-
-error: passing a unit value to a function
-  --> $DIR/unit_arg_expressions.rs:14:5
-   |
-LL | /     foo(match Some(1) {
-LL | |         Some(_) => {
-LL | |             1;
-LL | |         },
-...  |
-LL | |         },
-LL | |     });
-   | |______^
-   |
-help: move the expression in front of the call and replace it with the unit literal `()`
-   |
-LL |     match Some(1) {
-LL |         Some(_) => {
-LL |             1;
-LL |         },
-LL |         None => {
-LL |             0;
- ...
-
-error: aborting due to 2 previous errors
-