Skip to content

Commit d5223be

Browse files
committed
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.
2 parents 5c6cd87 + cbe6eec commit d5223be

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

clippy_lints/src/types.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,10 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
955955
.iter()
956956
.filter(|arg| {
957957
if is_unit(cx.typeck_results().expr_ty(arg)) && !is_unit_literal(arg) {
958-
!matches!(&arg.kind, ExprKind::Match(.., MatchSource::TryDesugar))
958+
!matches!(
959+
&arg.kind,
960+
ExprKind::Match(.., MatchSource::TryDesugar) | ExprKind::Path(..)
961+
)
959962
} else {
960963
false
961964
}

tests/ui/unit_arg.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ impl Bar {
2727
}
2828
}
2929

30+
fn baz<T: Debug>(t: T) {
31+
foo(t);
32+
}
33+
34+
trait Tr {
35+
type Args;
36+
fn do_it(args: Self::Args);
37+
}
38+
39+
struct A;
40+
impl Tr for A {
41+
type Args = ();
42+
fn do_it(_: Self::Args) {}
43+
}
44+
45+
struct B;
46+
impl Tr for B {
47+
type Args = <A as Tr>::Args;
48+
49+
fn do_it(args: Self::Args) {
50+
A::do_it(args)
51+
}
52+
}
53+
3054
fn bad() {
3155
foo({
3256
1;
@@ -59,7 +83,7 @@ fn bad() {
5983
None.or(Some(foo(2)));
6084
// in this case, the suggestion can be inlined, no need for a surrounding block
6185
// foo(()); foo(()) instead of { foo(()); foo(()) }
62-
foo(foo(()))
86+
foo(foo(()));
6387
}
6488

6589
fn ok() {
@@ -71,6 +95,10 @@ fn ok() {
7195
b.bar({ 1 });
7296
b.bar(());
7397
question_mark();
98+
let named_unit_arg = ();
99+
foo(named_unit_arg);
100+
baz(());
101+
B::do_it(());
74102
}
75103

76104
fn question_mark() -> Result<(), ()> {

tests/ui/unit_arg.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: passing a unit value to a function
2-
--> $DIR/unit_arg.rs:31:5
2+
--> $DIR/unit_arg.rs:55:5
33
|
44
LL | / foo({
55
LL | | 1;
@@ -20,7 +20,7 @@ LL | foo(());
2020
|
2121

2222
error: passing a unit value to a function
23-
--> $DIR/unit_arg.rs:34:5
23+
--> $DIR/unit_arg.rs:58:5
2424
|
2525
LL | foo(foo(1));
2626
| ^^^^^^^^^^^
@@ -32,7 +32,7 @@ LL | foo(());
3232
|
3333

3434
error: passing a unit value to a function
35-
--> $DIR/unit_arg.rs:35:5
35+
--> $DIR/unit_arg.rs:59:5
3636
|
3737
LL | / foo({
3838
LL | | foo(1);
@@ -54,7 +54,7 @@ LL | foo(());
5454
|
5555

5656
error: passing a unit value to a function
57-
--> $DIR/unit_arg.rs:40:5
57+
--> $DIR/unit_arg.rs:64:5
5858
|
5959
LL | / b.bar({
6060
LL | | 1;
@@ -74,7 +74,7 @@ LL | b.bar(());
7474
|
7575

7676
error: passing unit values to a function
77-
--> $DIR/unit_arg.rs:43:5
77+
--> $DIR/unit_arg.rs:67:5
7878
|
7979
LL | taking_multiple_units(foo(0), foo(1));
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -87,7 +87,7 @@ LL | taking_multiple_units((), ());
8787
|
8888

8989
error: passing unit values to a function
90-
--> $DIR/unit_arg.rs:44:5
90+
--> $DIR/unit_arg.rs:68:5
9191
|
9292
LL | / taking_multiple_units(foo(0), {
9393
LL | | foo(1);
@@ -110,7 +110,7 @@ LL | taking_multiple_units((), ());
110110
|
111111

112112
error: passing unit values to a function
113-
--> $DIR/unit_arg.rs:48:5
113+
--> $DIR/unit_arg.rs:72:5
114114
|
115115
LL | / taking_multiple_units(
116116
LL | | {
@@ -140,7 +140,7 @@ LL | foo(2);
140140
...
141141

142142
error: passing a unit value to a function
143-
--> $DIR/unit_arg.rs:59:13
143+
--> $DIR/unit_arg.rs:83:13
144144
|
145145
LL | None.or(Some(foo(2)));
146146
| ^^^^^^^^^^^^
@@ -154,19 +154,19 @@ LL | });
154154
|
155155

156156
error: passing a unit value to a function
157-
--> $DIR/unit_arg.rs:62:5
157+
--> $DIR/unit_arg.rs:86:5
158158
|
159-
LL | foo(foo(()))
159+
LL | foo(foo(()));
160160
| ^^^^^^^^^^^^
161161
|
162162
help: move the expression in front of the call and replace it with the unit literal `()`
163163
|
164164
LL | foo(());
165-
LL | foo(())
165+
LL | foo(());
166166
|
167167

168168
error: passing a unit value to a function
169-
--> $DIR/unit_arg.rs:95:5
169+
--> $DIR/unit_arg.rs:123:5
170170
|
171171
LL | Some(foo(1))
172172
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)