Skip to content

Commit 0c347d3

Browse files
committed
Fix false positive for unit_arg lint
1 parent 40ce9f8 commit 0c347d3

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

clippy_lints/src/types.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,16 @@ 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+
match &arg.kind {
959+
ExprKind::Block(..)
960+
| ExprKind::Call(..)
961+
| ExprKind::If(..)
962+
| ExprKind::MethodCall(..) => true,
963+
ExprKind::Match(..) => {
964+
!matches!(&arg.kind, ExprKind::Match(.., MatchSource::TryDesugar))
965+
},
966+
_ => false,
967+
}
959968
} else {
960969
false
961970
}

tests/ui/unit_arg.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,18 @@ fn bad() {
5959
None.or(Some(foo(2)));
6060
// in this case, the suggestion can be inlined, no need for a surrounding block
6161
// foo(()); foo(()) instead of { foo(()); foo(()) }
62-
foo(foo(()))
62+
foo(foo(()));
63+
foo(if true {
64+
1;
65+
});
66+
foo(match Some(1) {
67+
Some(_) => {
68+
1;
69+
},
70+
None => {
71+
0;
72+
},
73+
});
6374
}
6475

6576
fn ok() {
@@ -71,6 +82,13 @@ fn ok() {
7182
b.bar({ 1 });
7283
b.bar(());
7384
question_mark();
85+
let named_unit_arg = ();
86+
foo(named_unit_arg);
87+
foo(if true { 1 } else { 0 });
88+
foo(match Some(1) {
89+
Some(_) => 1,
90+
None => 0,
91+
});
7492
}
7593

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

tests/ui/unit_arg.stderr

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,55 @@ LL | });
156156
error: passing a unit value to a function
157157
--> $DIR/unit_arg.rs:62: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(());
166+
|
167+
168+
error: passing a unit value to a function
169+
--> $DIR/unit_arg.rs:63:5
170+
|
171+
LL | / foo(if true {
172+
LL | | 1;
173+
LL | | });
174+
| |______^
175+
|
176+
help: move the expression in front of the call and replace it with the unit literal `()`
177+
|
178+
LL | if true {
179+
LL | 1;
180+
LL | };
181+
LL | foo(());
182+
|
183+
184+
error: passing a unit value to a function
185+
--> $DIR/unit_arg.rs:66:5
166186
|
187+
LL | / foo(match Some(1) {
188+
LL | | Some(_) => {
189+
LL | | 1;
190+
LL | | },
191+
... |
192+
LL | | },
193+
LL | | });
194+
| |______^
195+
|
196+
help: move the expression in front of the call and replace it with the unit literal `()`
197+
|
198+
LL | match Some(1) {
199+
LL | Some(_) => {
200+
LL | 1;
201+
LL | },
202+
LL | None => {
203+
LL | 0;
204+
...
167205

168206
error: passing a unit value to a function
169-
--> $DIR/unit_arg.rs:95:5
207+
--> $DIR/unit_arg.rs:113:5
170208
|
171209
LL | Some(foo(1))
172210
| ^^^^^^^^^^^^
@@ -177,5 +215,5 @@ LL | foo(1);
177215
LL | Some(())
178216
|
179217

180-
error: aborting due to 10 previous errors
218+
error: aborting due to 12 previous errors
181219

0 commit comments

Comments
 (0)