Skip to content

Commit

Permalink
Auto merge of #6601 - mdm:fix-unit-args-false-positive, r=camsteffen
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Feb 25, 2021
2 parents 5c6cd87 + cbe6eec commit d5223be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
5 changes: 4 additions & 1 deletion clippy_lints/src/types.rs
Expand Up @@ -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
}
Expand Down
30 changes: 29 additions & 1 deletion tests/ui/unit_arg.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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<(), ()> {
Expand Down
24 changes: 12 additions & 12 deletions 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;
Expand All @@ -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));
| ^^^^^^^^^^^
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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);
Expand All @@ -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 | | {
Expand Down Expand Up @@ -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)));
| ^^^^^^^^^^^^
Expand All @@ -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))
| ^^^^^^^^^^^^
Expand Down

0 comments on commit d5223be

Please sign in to comment.