Fix false positive for unit_arg lint

This commit is contained in:
Marc Dominik Migge 2021-01-17 14:42:36 +01:00
parent 40ce9f83b6
commit 0c347d3d06
3 changed files with 71 additions and 6 deletions

View file

@ -955,7 +955,16 @@ 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))
match &arg.kind {
ExprKind::Block(..)
| ExprKind::Call(..)
| ExprKind::If(..)
| ExprKind::MethodCall(..) => true,
ExprKind::Match(..) => {
!matches!(&arg.kind, ExprKind::Match(.., MatchSource::TryDesugar))
},
_ => false,
}
} else {
false
}

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

View 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:95:5
--> $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: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