Don't lint unit args if expression kind is path

This commit is contained in:
Marc Dominik Migge 2021-02-24 13:31:04 +01:00
parent eb476c6c70
commit 9fe9d94abd
5 changed files with 19 additions and 96 deletions

View File

@ -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
}

View File

@ -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<(), ()> {

View File

@ -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))
| ^^^^^^^^^^^^

View File

@ -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();
}

View File

@ -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