Skip to content

Commit 90a46e9

Browse files
committed
Fix let_unit_value to lint when explicit unit type annotation is present
Remove the early return that was skipping let bindings with explicit `()` type annotations. The lint should trigger for redundant unit bindings regardless of whether the type is explicitly annotated or inferred.
1 parent d74200a commit 90a46e9

File tree

7 files changed

+145
-28
lines changed

7 files changed

+145
-28
lines changed

clippy_lints/src/unit_types/let_unit_value.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, format_args: &FormatArgsStorag
3333
return;
3434
}
3535

36-
// skip `let _: () = { ... }`
37-
if let Some(ty) = local.ty
38-
&& let TyKind::Tup([]) = ty.kind
39-
{
40-
return;
41-
}
42-
4336
if (local.ty.is_some_and(|ty| !matches!(ty.kind, TyKind::Infer(())))
4437
|| matches!(local.pat.kind, PatKind::Tuple([], ddpos) if ddpos.as_opt_usize().is_none()))
4538
&& expr_needs_inferred_result(cx, init)

tests/ui/crashes/ice-8821.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![warn(clippy::let_unit_value)]
2+
3+
fn f() {}
4+
static FN: fn() = f;
5+
6+
fn main() {
7+
FN();
8+
//~^ ERROR: this let-binding has unit value
9+
}

tests/ui/crashes/ice-8821.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
//@ check-pass
2-
31
#![warn(clippy::let_unit_value)]
42

53
fn f() {}
64
static FN: fn() = f;
75

86
fn main() {
97
let _: () = FN();
8+
//~^ ERROR: this let-binding has unit value
109
}

tests/ui/crashes/ice-8821.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: this let-binding has unit value
2+
--> tests/ui/crashes/ice-8821.rs:7:5
3+
|
4+
LL | let _: () = FN();
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::let-unit-value` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::let_unit_value)]`
9+
help: omit the `let` binding
10+
|
11+
LL - let _: () = FN();
12+
LL + FN();
13+
|
14+
15+
error: aborting due to 1 previous error
16+

tests/ui/let_unit.fixed

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ fn main() {
1818
let _y = 1; // this is fine
1919
let _z = ((), 1); // this as well
2020
if true {
21-
// do not lint this, since () is explicit
21+
// do not lint these, since () is explicit
2222
let _a = ();
2323
let () = returns_unit();
2424
let () = ();
2525
() = returns_unit();
2626
() = ();
2727
let _a: () = ();
28-
let _a: () = returns_unit();
28+
29+
// should lint: explicit type annotation is redundant
30+
returns_unit();
31+
//~^ let_unit_value
2932
}
3033

3134
consume_units_with_for_loop(); // should be fine as well
@@ -90,27 +93,33 @@ fn _returns_generic() {
9093
}
9194

9295
let _: () = f();
93-
let x: () = f();
96+
let _: () = f();
97+
//~^ ERROR: this let-binding has unit value
9498

9599
let _: () = f2(0i32);
96-
let x: () = f2(0i32);
100+
let _: () = f2(0i32);
101+
//~^ ERROR: this let-binding has unit value
97102

98-
let _: () = f3(());
99-
let x: () = f3(());
103+
f3(());
104+
//~^ ERROR: this let-binding has unit value
105+
f3(());
106+
//~^ ERROR: this let-binding has unit value
100107

101108
fn f4<T>(mut x: Vec<T>) -> T {
102109
x.pop().unwrap()
103110
}
104111
let _: () = f4(vec![()]);
105-
let x: () = f4(vec![()]);
112+
let _: () = f4(vec![()]);
113+
//~^ ERROR: this let-binding has unit value
106114

107115
let _: () = {
108116
let x = 5;
109117
f2(x)
110118
};
111119

112120
let _: () = if true { f() } else { f2(0) };
113-
let x: () = if true { f() } else { f2(0) };
121+
let _: () = if true { f() } else { f2(0) };
122+
//~^ ERROR: this let-binding has unit value
114123

115124
match Some(0) {
116125
//~^ let_unit_value
@@ -160,7 +169,8 @@ fn _returns_generic() {
160169
{
161170
let _: () = x;
162171
let _: () = y;
163-
let _: () = z;
172+
z;
173+
//~^ ERROR: this let-binding has unit value
164174
let _: () = x1;
165175
let _: () = x2;
166176
let _: () = opt;

tests/ui/let_unit.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ fn main() {
1818
let _y = 1; // this is fine
1919
let _z = ((), 1); // this as well
2020
if true {
21-
// do not lint this, since () is explicit
21+
// do not lint these, since () is explicit
2222
let _a = ();
2323
let () = returns_unit();
2424
let () = ();
2525
() = returns_unit();
2626
() = ();
2727
let _a: () = ();
28+
29+
// should lint: explicit type annotation is redundant
2830
let _a: () = returns_unit();
31+
//~^ let_unit_value
2932
}
3033

3134
consume_units_with_for_loop(); // should be fine as well
@@ -91,18 +94,23 @@ fn _returns_generic() {
9194

9295
let _: () = f();
9396
let x: () = f();
97+
//~^ ERROR: this let-binding has unit value
9498

9599
let _: () = f2(0i32);
96100
let x: () = f2(0i32);
101+
//~^ ERROR: this let-binding has unit value
97102

98103
let _: () = f3(());
104+
//~^ ERROR: this let-binding has unit value
99105
let x: () = f3(());
106+
//~^ ERROR: this let-binding has unit value
100107

101108
fn f4<T>(mut x: Vec<T>) -> T {
102109
x.pop().unwrap()
103110
}
104111
let _: () = f4(vec![()]);
105112
let x: () = f4(vec![()]);
113+
//~^ ERROR: this let-binding has unit value
106114

107115
let _: () = {
108116
let x = 5;
@@ -111,6 +119,7 @@ fn _returns_generic() {
111119

112120
let _: () = if true { f() } else { f2(0) };
113121
let x: () = if true { f() } else { f2(0) };
122+
//~^ ERROR: this let-binding has unit value
114123

115124
let x = match Some(0) {
116125
//~^ let_unit_value
@@ -161,6 +170,7 @@ fn _returns_generic() {
161170
let _: () = x;
162171
let _: () = y;
163172
let _: () = z;
173+
//~^ ERROR: this let-binding has unit value
164174
let _: () = x1;
165175
let _: () = x2;
166176
let _: () = opt;

tests/ui/let_unit.stderr

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@ LL + println!("x");
1313
|
1414

1515
error: this let-binding has unit value
16-
--> tests/ui/let_unit.rs:65:5
16+
--> tests/ui/let_unit.rs:30:9
17+
|
18+
LL | let _a: () = returns_unit();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: omit the `let` binding
22+
|
23+
LL - let _a: () = returns_unit();
24+
LL + returns_unit();
25+
|
26+
27+
error: this let-binding has unit value
28+
--> tests/ui/let_unit.rs:68:5
1729
|
1830
LL | / let _ = v
1931
LL | |
@@ -31,7 +43,63 @@ LL + v
3143
|
3244

3345
error: this let-binding has unit value
34-
--> tests/ui/let_unit.rs:115:5
46+
--> tests/ui/let_unit.rs:96:5
47+
|
48+
LL | let x: () = f();
49+
| ^^^^-^^^^^^^^^^^
50+
| |
51+
| help: use a wildcard binding: `_`
52+
53+
error: this let-binding has unit value
54+
--> tests/ui/let_unit.rs:100:5
55+
|
56+
LL | let x: () = f2(0i32);
57+
| ^^^^-^^^^^^^^^^^^^^^^
58+
| |
59+
| help: use a wildcard binding: `_`
60+
61+
error: this let-binding has unit value
62+
--> tests/ui/let_unit.rs:103:5
63+
|
64+
LL | let _: () = f3(());
65+
| ^^^^^^^^^^^^^^^^^^^
66+
|
67+
help: omit the `let` binding
68+
|
69+
LL - let _: () = f3(());
70+
LL + f3(());
71+
|
72+
73+
error: this let-binding has unit value
74+
--> tests/ui/let_unit.rs:105:5
75+
|
76+
LL | let x: () = f3(());
77+
| ^^^^^^^^^^^^^^^^^^^
78+
|
79+
help: omit the `let` binding
80+
|
81+
LL - let x: () = f3(());
82+
LL + f3(());
83+
|
84+
85+
error: this let-binding has unit value
86+
--> tests/ui/let_unit.rs:112:5
87+
|
88+
LL | let x: () = f4(vec![()]);
89+
| ^^^^-^^^^^^^^^^^^^^^^^^^^
90+
| |
91+
| help: use a wildcard binding: `_`
92+
93+
error: this let-binding has unit value
94+
--> tests/ui/let_unit.rs:121:5
95+
|
96+
LL | let x: () = if true { f() } else { f2(0) };
97+
| ^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98+
| |
99+
| help: use a wildcard binding: `_`
100+
101+
error: this let-binding has unit value
102+
--> tests/ui/let_unit.rs:124:5
35103
|
36104
LL | / let x = match Some(0) {
37105
LL | |
@@ -49,7 +117,19 @@ LL + match Some(0) {
49117
|
50118

51119
error: this let-binding has unit value
52-
--> tests/ui/let_unit.rs:195:9
120+
--> tests/ui/let_unit.rs:172:13
121+
|
122+
LL | let _: () = z;
123+
| ^^^^^^^^^^^^^^
124+
|
125+
help: omit the `let` binding
126+
|
127+
LL - let _: () = z;
128+
LL + z;
129+
|
130+
131+
error: this let-binding has unit value
132+
--> tests/ui/let_unit.rs:205:9
53133
|
54134
LL | let res = returns_unit();
55135
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -63,7 +143,7 @@ LL ~ returns_result(()).unwrap();
63143
|
64144

65145
error: this let-binding has unit value
66-
--> tests/ui/let_unit.rs:208:5
146+
--> tests/ui/let_unit.rs:218:5
67147
|
68148
LL | let res = returns_unit();
69149
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -77,7 +157,7 @@ LL ~ takes_unit(());
77157
|
78158

79159
error: this let-binding has unit value
80-
--> tests/ui/let_unit.rs:216:14
160+
--> tests/ui/let_unit.rs:226:14
81161
|
82162
LL | _ => _ = returns_unit(),
83163
| ^^^^^^^^^^^^^^^^^^
@@ -89,7 +169,7 @@ LL + _ => returns_unit(),
89169
|
90170

91171
error: this let-binding has unit value
92-
--> tests/ui/let_unit.rs:220:5
172+
--> tests/ui/let_unit.rs:230:5
93173
|
94174
LL | _ = if true {}
95175
| ^^^^^^^^^^^^^^
@@ -101,7 +181,7 @@ LL + if true {}
101181
|
102182

103183
error: this let-binding has unit value
104-
--> tests/ui/let_unit.rs:225:5
184+
--> tests/ui/let_unit.rs:235:5
105185
|
106186
LL | let res = eprintln!("I return unit");
107187
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -115,7 +195,7 @@ LL ~ takes_unit(());
115195
|
116196

117197
error: this let-binding has unit value
118-
--> tests/ui/let_unit.rs:235:5
198+
--> tests/ui/let_unit.rs:245:5
119199
|
120200
LL | let value = println!();
121201
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -128,5 +208,5 @@ LL |
128208
LL ~ Foo { value: () };
129209
|
130210

131-
error: aborting due to 9 previous errors
211+
error: aborting due to 17 previous errors
132212

0 commit comments

Comments
 (0)