Skip to content

Commit a3baf20

Browse files
committed
skip let _: ()
1 parent bb0e9d4 commit a3baf20

File tree

5 files changed

+27
-95
lines changed

5 files changed

+27
-95
lines changed

clippy_lints/src/unit_types/let_unit_value.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
2424
&& cx.typeck_results().pat_ty(local.pat).is_unit()
2525
{
2626
// skip `let awa = ()`
27-
if let ExprKind::Tup(elements) = init.kind && elements.is_empty() {
27+
if let ExprKind::Tup([]) = init.kind {
28+
return;
29+
}
30+
31+
// skip `let _: () = { ... }`
32+
if let Some(ty) = local.ty && let TyKind::Tup([]) = ty.kind {
2833
return;
2934
}
3035

@@ -43,7 +48,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
4348
|diag| {
4449
diag.span_suggestion(
4550
local.pat.span,
46-
"use a wild (`_`) binding",
51+
"use a wildcard binding",
4752
"_",
4853
Applicability::MaybeIncorrect, // snippet
4954
);

tests/ui/crashes/ice-8821.stderr

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/ui/let_unit.fixed

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ fn main() {
1717
if true {
1818
// do not lint this, since () is explicit
1919
let _a = ();
20+
let () = dummy();
21+
let () = ();
22+
() = dummy();
23+
() = ();
24+
let _a: () = ();
25+
let _a: () = dummy();
2026
}
2127

22-
// do not lint this, since () is explicit
23-
let () = dummy();
24-
let () = ();
25-
() = dummy();
26-
() = ();
27-
2828
consume_units_with_for_loop(); // should be fine as well
2929

3030
multiline_sugg();
@@ -86,13 +86,13 @@ fn _returns_generic() {
8686
}
8787

8888
let _: () = f(); // Ok
89-
let _: () = f(); // Lint.
89+
let x: () = f(); // Lint.
9090

9191
let _: () = f2(0i32); // Ok
92-
let _: () = f2(0i32); // Lint.
92+
let x: () = f2(0i32); // Lint.
9393

94-
f3(()); // Lint
95-
f3(()); // Lint
94+
let _: () = f3(()); // Lint
95+
let x: () = f3(()); // Lint
9696

9797
// Should lint:
9898
// fn f4<T>(mut x: Vec<T>) -> T {
@@ -108,7 +108,7 @@ fn _returns_generic() {
108108
};
109109

110110
let _: () = if true { f() } else { f2(0) }; // Ok
111-
let _: () = if true { f() } else { f2(0) }; // Lint
111+
let x: () = if true { f() } else { f2(0) }; // Lint
112112

113113
// Ok
114114
let _: () = match Some(0) {
@@ -119,7 +119,7 @@ fn _returns_generic() {
119119
};
120120

121121
// Lint
122-
match Some(0) {
122+
let _: () = match Some(0) {
123123
None => f2(1),
124124
Some(0) => f(),
125125
Some(1) => f2(3),
@@ -166,7 +166,7 @@ fn _returns_generic() {
166166
{
167167
let _: () = x;
168168
let _: () = y;
169-
z;
169+
let _: () = z;
170170
let _: () = x1;
171171
let _: () = x2;
172172
let _: () = opt;

tests/ui/let_unit.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ fn main() {
1717
if true {
1818
// do not lint this, since () is explicit
1919
let _a = ();
20+
let () = dummy();
21+
let () = ();
22+
() = dummy();
23+
() = ();
24+
let _a: () = ();
25+
let _a: () = dummy();
2026
}
2127

22-
// do not lint this, since () is explicit
23-
let () = dummy();
24-
let () = ();
25-
() = dummy();
26-
() = ();
27-
2828
consume_units_with_for_loop(); // should be fine as well
2929

3030
multiline_sugg();

tests/ui/let_unit.stderr

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,68 +29,5 @@ LL + .next()
2929
LL + .unwrap();
3030
|
3131

32-
error: this let-binding has unit value
33-
--> $DIR/let_unit.rs:89:5
34-
|
35-
LL | let x: () = f(); // Lint.
36-
| ^^^^-^^^^^^^^^^^
37-
| |
38-
| help: use a wild (`_`) binding: `_`
39-
40-
error: this let-binding has unit value
41-
--> $DIR/let_unit.rs:92:5
42-
|
43-
LL | let x: () = f2(0i32); // Lint.
44-
| ^^^^-^^^^^^^^^^^^^^^^
45-
| |
46-
| help: use a wild (`_`) binding: `_`
47-
48-
error: this let-binding has unit value
49-
--> $DIR/let_unit.rs:94:5
50-
|
51-
LL | let _: () = f3(()); // Lint
52-
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`
53-
54-
error: this let-binding has unit value
55-
--> $DIR/let_unit.rs:95:5
56-
|
57-
LL | let x: () = f3(()); // Lint
58-
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`
59-
60-
error: this let-binding has unit value
61-
--> $DIR/let_unit.rs:111:5
62-
|
63-
LL | let x: () = if true { f() } else { f2(0) }; // Lint
64-
| ^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65-
| |
66-
| help: use a wild (`_`) binding: `_`
67-
68-
error: this let-binding has unit value
69-
--> $DIR/let_unit.rs:122:5
70-
|
71-
LL | / let _: () = match Some(0) {
72-
LL | | None => f2(1),
73-
LL | | Some(0) => f(),
74-
LL | | Some(1) => f2(3),
75-
LL | | Some(_) => (),
76-
LL | | };
77-
| |______^
78-
|
79-
help: omit the `let` binding
80-
|
81-
LL ~ match Some(0) {
82-
LL + None => f2(1),
83-
LL + Some(0) => f(),
84-
LL + Some(1) => f2(3),
85-
LL + Some(_) => (),
86-
LL + };
87-
|
88-
89-
error: this let-binding has unit value
90-
--> $DIR/let_unit.rs:169:13
91-
|
92-
LL | let _: () = z;
93-
| ^^^^^^^^^^^^^^ help: omit the `let` binding: `z;`
94-
95-
error: aborting due to 9 previous errors
32+
error: aborting due to 2 previous errors
9633

0 commit comments

Comments
 (0)