Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions clippy_lints/src/unit_types/let_unit_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, format_args: &FormatArgsStorag
return;
}

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

if (local.ty.is_some_and(|ty| !matches!(ty.kind, TyKind::Infer(())))
|| matches!(local.pat.kind, PatKind::Tuple([], ddpos) if ddpos.as_opt_usize().is_none()))
&& expr_needs_inferred_result(cx, init)
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/crashes/ice-8821.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![warn(clippy::let_unit_value)]

fn f() {}
static FN: fn() = f;

fn main() {
FN();
//~^ ERROR: this let-binding has unit value
}
3 changes: 1 addition & 2 deletions tests/ui/crashes/ice-8821.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//@ check-pass

#![warn(clippy::let_unit_value)]

fn f() {}
static FN: fn() = f;

fn main() {
let _: () = FN();
//~^ ERROR: this let-binding has unit value
}
16 changes: 16 additions & 0 deletions tests/ui/crashes/ice-8821.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: this let-binding has unit value
--> tests/ui/crashes/ice-8821.rs:7:5
|
LL | let _: () = FN();
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::let-unit-value` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::let_unit_value)]`
help: omit the `let` binding
|
LL - let _: () = FN();
LL + FN();
|

error: aborting due to 1 previous error

28 changes: 19 additions & 9 deletions tests/ui/let_unit.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ fn main() {
let _y = 1; // this is fine
let _z = ((), 1); // this as well
if true {
// do not lint this, since () is explicit
// do not lint these, since () is explicit
let _a = ();
let () = returns_unit();
let () = ();
() = returns_unit();
() = ();
let _a: () = ();
let _a: () = returns_unit();

// should lint: explicit type annotation is redundant
returns_unit();
//~^ let_unit_value
}

consume_units_with_for_loop(); // should be fine as well
Expand Down Expand Up @@ -90,27 +93,33 @@ fn _returns_generic() {
}

let _: () = f();
let x: () = f();
let _: () = f();
//~^ let_unit_value

let _: () = f2(0i32);
let x: () = f2(0i32);
let _: () = f2(0i32);
//~^ let_unit_value

let _: () = f3(());
let x: () = f3(());
f3(());
//~^ let_unit_value
f3(());
//~^ let_unit_value

fn f4<T>(mut x: Vec<T>) -> T {
x.pop().unwrap()
}
let _: () = f4(vec![()]);
let x: () = f4(vec![()]);
let _: () = f4(vec![()]);
//~^ let_unit_value

let _: () = {
let x = 5;
f2(x)
};

let _: () = if true { f() } else { f2(0) };
let x: () = if true { f() } else { f2(0) };
let _: () = if true { f() } else { f2(0) };
//~^ let_unit_value

match Some(0) {
//~^ let_unit_value
Expand Down Expand Up @@ -160,7 +169,8 @@ fn _returns_generic() {
{
let _: () = x;
let _: () = y;
let _: () = z;
z;
//~^ let_unit_value
let _: () = x1;
let _: () = x2;
let _: () = opt;
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/let_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ fn main() {
let _y = 1; // this is fine
let _z = ((), 1); // this as well
if true {
// do not lint this, since () is explicit
// do not lint these, since () is explicit
let _a = ();
let () = returns_unit();
let () = ();
() = returns_unit();
() = ();
let _a: () = ();

// should lint: explicit type annotation is redundant
let _a: () = returns_unit();
//~^ let_unit_value
}

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

let _: () = f();
let x: () = f();
//~^ let_unit_value

let _: () = f2(0i32);
let x: () = f2(0i32);
//~^ let_unit_value

let _: () = f3(());
//~^ let_unit_value
let x: () = f3(());
//~^ let_unit_value

fn f4<T>(mut x: Vec<T>) -> T {
x.pop().unwrap()
}
let _: () = f4(vec![()]);
let x: () = f4(vec![()]);
//~^ let_unit_value

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

let _: () = if true { f() } else { f2(0) };
let x: () = if true { f() } else { f2(0) };
//~^ let_unit_value

let x = match Some(0) {
//~^ let_unit_value
Expand Down Expand Up @@ -161,6 +170,7 @@ fn _returns_generic() {
let _: () = x;
let _: () = y;
let _: () = z;
//~^ let_unit_value
let _: () = x1;
let _: () = x2;
let _: () = opt;
Expand Down
98 changes: 89 additions & 9 deletions tests/ui/let_unit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ LL + println!("x");
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:65:5
--> tests/ui/let_unit.rs:30:9
|
LL | let _a: () = returns_unit();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: omit the `let` binding
|
LL - let _a: () = returns_unit();
LL + returns_unit();
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:68:5
|
LL | / let _ = v
LL | |
Expand All @@ -31,7 +43,63 @@ LL + v
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:115:5
--> tests/ui/let_unit.rs:96:5
|
LL | let x: () = f();
| ^^^^-^^^^^^^^^^^
| |
| help: use a wildcard binding: `_`

error: this let-binding has unit value
--> tests/ui/let_unit.rs:100:5
|
LL | let x: () = f2(0i32);
| ^^^^-^^^^^^^^^^^^^^^^
| |
| help: use a wildcard binding: `_`

error: this let-binding has unit value
--> tests/ui/let_unit.rs:103:5
|
LL | let _: () = f3(());
| ^^^^^^^^^^^^^^^^^^^
|
help: omit the `let` binding
|
LL - let _: () = f3(());
LL + f3(());
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:105:5
|
LL | let x: () = f3(());
| ^^^^^^^^^^^^^^^^^^^
|
help: omit the `let` binding
|
LL - let x: () = f3(());
LL + f3(());
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:112:5
|
LL | let x: () = f4(vec![()]);
| ^^^^-^^^^^^^^^^^^^^^^^^^^
| |
| help: use a wildcard binding: `_`

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

error: this let-binding has unit value
--> tests/ui/let_unit.rs:124:5
|
LL | / let x = match Some(0) {
LL | |
Expand All @@ -49,7 +117,19 @@ LL + match Some(0) {
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:195:9
--> tests/ui/let_unit.rs:172:13
|
LL | let _: () = z;
| ^^^^^^^^^^^^^^
|
help: omit the `let` binding
|
LL - let _: () = z;
LL + z;
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:205:9
|
LL | let res = returns_unit();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -63,7 +143,7 @@ LL ~ returns_result(()).unwrap();
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:208:5
--> tests/ui/let_unit.rs:218:5
|
LL | let res = returns_unit();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -77,7 +157,7 @@ LL ~ takes_unit(());
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:216:14
--> tests/ui/let_unit.rs:226:14
|
LL | _ => _ = returns_unit(),
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -89,7 +169,7 @@ LL + _ => returns_unit(),
|

error: this let-binding has unit value
--> tests/ui/let_unit.rs:220:5
--> tests/ui/let_unit.rs:230:5
|
LL | _ = if true {}
| ^^^^^^^^^^^^^^
Expand All @@ -101,7 +181,7 @@ LL + if true {}
|

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

error: this let-binding has unit value
--> tests/ui/let_unit.rs:235:5
--> tests/ui/let_unit.rs:245:5
|
LL | let value = println!();
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -128,5 +208,5 @@ LL |
LL ~ Foo { value: () };
|

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