]> git.lizzy.rs Git - rust.git/commitdiff
Fix false positive for unit_arg lint
authorMarc Dominik Migge <marcmigge@gmx.net>
Sun, 17 Jan 2021 13:42:36 +0000 (14:42 +0100)
committerMarc Dominik Migge <marcmigge@gmx.net>
Sun, 17 Jan 2021 13:42:36 +0000 (14:42 +0100)
clippy_lints/src/types.rs
tests/ui/unit_arg.rs
tests/ui/unit_arg.stderr

index 3b5a83d2a0bec7f0767718965bf42d845c101520..7d0eea37bc02a6e7c46795e232ec6af8e9601f71 100644 (file)
@@ -955,7 +955,16 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
                     .iter()
                     .filter(|arg| {
                         if is_unit(cx.typeck_results().expr_ty(arg)) && !is_unit_literal(arg) {
-                            !matches!(&arg.kind, ExprKind::Match(.., MatchSource::TryDesugar))
+                            match &arg.kind {
+                                ExprKind::Block(..)
+                                | ExprKind::Call(..)
+                                | ExprKind::If(..)
+                                | ExprKind::MethodCall(..) => true,
+                                ExprKind::Match(..) => {
+                                    !matches!(&arg.kind, ExprKind::Match(.., MatchSource::TryDesugar))
+                                },
+                                _ => false,
+                            }
                         } else {
                             false
                         }
index b6a7bc5a1cc95466bd3f3d4fa0c28a6998053753..79dac925f08caed4ae28dd0fb705325de215c35b 100644 (file)
@@ -59,7 +59,18 @@ 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(()));
+    foo(if true {
+        1;
+    });
+    foo(match Some(1) {
+        Some(_) => {
+            1;
+        },
+        None => {
+            0;
+        },
+    });
 }
 
 fn ok() {
@@ -71,6 +82,13 @@ fn ok() {
     b.bar({ 1 });
     b.bar(());
     question_mark();
+    let named_unit_arg = ();
+    foo(named_unit_arg);
+    foo(if true { 1 } else { 0 });
+    foo(match Some(1) {
+        Some(_) => 1,
+        None => 0,
+    });
 }
 
 fn question_mark() -> Result<(), ()> {
index 094cff8c98591146dd7558d321afed17894cd9d6..8679706f8ec8eb6cd3b3665f483ced9b67111700 100644 (file)
@@ -156,17 +156,55 @@ LL |     });
 error: passing a unit value to a function
   --> $DIR/unit_arg.rs:62: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:63:5
+   |
+LL | /     foo(if true {
+LL | |         1;
+LL | |     });
+   | |______^
+   |
+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.rs:66: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: passing a unit value to a function
-  --> $DIR/unit_arg.rs:95:5
+  --> $DIR/unit_arg.rs:113:5
    |
 LL |     Some(foo(1))
    |     ^^^^^^^^^^^^
@@ -177,5 +215,5 @@ LL |     foo(1);
 LL |     Some(())
    |
 
-error: aborting due to 10 previous errors
+error: aborting due to 12 previous errors