Skip to content

Commit

Permalink
skip let _: ()
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 18, 2023
1 parent bb0e9d4 commit a3baf20
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 95 deletions.
9 changes: 7 additions & 2 deletions clippy_lints/src/unit_types/let_unit_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
&& cx.typeck_results().pat_ty(local.pat).is_unit()
{
// skip `let awa = ()`
if let ExprKind::Tup(elements) = init.kind && elements.is_empty() {
if let ExprKind::Tup([]) = init.kind {
return;
}

// skip `let _: () = { ... }`
if let Some(ty) = local.ty && let TyKind::Tup([]) = ty.kind {
return;
}

Expand All @@ -43,7 +48,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
|diag| {
diag.span_suggestion(
local.pat.span,
"use a wild (`_`) binding",
"use a wildcard binding",
"_",
Applicability::MaybeIncorrect, // snippet
);
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/crashes/ice-8821.stderr

This file was deleted.

26 changes: 13 additions & 13 deletions tests/ui/let_unit.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ fn main() {
if true {
// do not lint this, since () is explicit
let _a = ();
let () = dummy();
let () = ();
() = dummy();
() = ();
let _a: () = ();
let _a: () = dummy();
}

// do not lint this, since () is explicit
let () = dummy();
let () = ();
() = dummy();
() = ();

consume_units_with_for_loop(); // should be fine as well

multiline_sugg();
Expand Down Expand Up @@ -86,13 +86,13 @@ fn _returns_generic() {
}

let _: () = f(); // Ok
let _: () = f(); // Lint.
let x: () = f(); // Lint.

let _: () = f2(0i32); // Ok
let _: () = f2(0i32); // Lint.
let x: () = f2(0i32); // Lint.

f3(()); // Lint
f3(()); // Lint
let _: () = f3(()); // Lint
let x: () = f3(()); // Lint

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

let _: () = if true { f() } else { f2(0) }; // Ok
let _: () = if true { f() } else { f2(0) }; // Lint
let x: () = if true { f() } else { f2(0) }; // Lint

// Ok
let _: () = match Some(0) {
Expand All @@ -119,7 +119,7 @@ fn _returns_generic() {
};

// Lint
match Some(0) {
let _: () = match Some(0) {
None => f2(1),
Some(0) => f(),
Some(1) => f2(3),
Expand Down Expand Up @@ -166,7 +166,7 @@ fn _returns_generic() {
{
let _: () = x;
let _: () = y;
z;
let _: () = z;
let _: () = x1;
let _: () = x2;
let _: () = opt;
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/let_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ fn main() {
if true {
// do not lint this, since () is explicit
let _a = ();
let () = dummy();
let () = ();
() = dummy();
() = ();
let _a: () = ();
let _a: () = dummy();
}

// do not lint this, since () is explicit
let () = dummy();
let () = ();
() = dummy();
() = ();

consume_units_with_for_loop(); // should be fine as well

multiline_sugg();
Expand Down
65 changes: 1 addition & 64 deletions tests/ui/let_unit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,68 +29,5 @@ LL + .next()
LL + .unwrap();
|

error: this let-binding has unit value
--> $DIR/let_unit.rs:89:5
|
LL | let x: () = f(); // Lint.
| ^^^^-^^^^^^^^^^^
| |
| help: use a wild (`_`) binding: `_`

error: this let-binding has unit value
--> $DIR/let_unit.rs:92:5
|
LL | let x: () = f2(0i32); // Lint.
| ^^^^-^^^^^^^^^^^^^^^^
| |
| help: use a wild (`_`) binding: `_`

error: this let-binding has unit value
--> $DIR/let_unit.rs:94:5
|
LL | let _: () = f3(()); // Lint
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`

error: this let-binding has unit value
--> $DIR/let_unit.rs:95:5
|
LL | let x: () = f3(()); // Lint
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`

error: this let-binding has unit value
--> $DIR/let_unit.rs:111:5
|
LL | let x: () = if true { f() } else { f2(0) }; // Lint
| ^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: use a wild (`_`) binding: `_`

error: this let-binding has unit value
--> $DIR/let_unit.rs:122:5
|
LL | / let _: () = match Some(0) {
LL | | None => f2(1),
LL | | Some(0) => f(),
LL | | Some(1) => f2(3),
LL | | Some(_) => (),
LL | | };
| |______^
|
help: omit the `let` binding
|
LL ~ match Some(0) {
LL + None => f2(1),
LL + Some(0) => f(),
LL + Some(1) => f2(3),
LL + Some(_) => (),
LL + };
|

error: this let-binding has unit value
--> $DIR/let_unit.rs:169:13
|
LL | let _: () = z;
| ^^^^^^^^^^^^^^ help: omit the `let` binding: `z;`

error: aborting due to 9 previous errors
error: aborting due to 2 previous errors

0 comments on commit a3baf20

Please sign in to comment.