Auto merge of #6601 - mdm:fix-unit-args-false-positive, r=camsteffen
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.
This commit is contained in:
commit
d5223be2e3
@ -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
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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() {
|
||||
@ -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<(), ()> {
|
||||
|
@ -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;
|
||||
@ -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));
|
||||
| ^^^^^^^^^^^
|
||||
@ -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);
|
||||
@ -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;
|
||||
@ -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));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -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);
|
||||
@ -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 | | {
|
||||
@ -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)));
|
||||
| ^^^^^^^^^^^^
|
||||
@ -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))
|
||||
| ^^^^^^^^^^^^
|
||||
|
Loading…
x
Reference in New Issue
Block a user