diff --git a/clippy_lints/src/unit_types/let_unit_value.rs b/clippy_lints/src/unit_types/let_unit_value.rs index 2645e94358e1..1302e790bbf3 100644 --- a/clippy_lints/src/unit_types/let_unit_value.rs +++ b/clippy_lints/src/unit_types/let_unit_value.rs @@ -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) diff --git a/tests/ui/crashes/ice-8821.fixed b/tests/ui/crashes/ice-8821.fixed new file mode 100644 index 000000000000..1ad931acff75 --- /dev/null +++ b/tests/ui/crashes/ice-8821.fixed @@ -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 +} diff --git a/tests/ui/crashes/ice-8821.rs b/tests/ui/crashes/ice-8821.rs index c9fea3d1ccb8..fe6b95300065 100644 --- a/tests/ui/crashes/ice-8821.rs +++ b/tests/ui/crashes/ice-8821.rs @@ -1,5 +1,3 @@ -//@ check-pass - #![warn(clippy::let_unit_value)] fn f() {} @@ -7,4 +5,5 @@ static FN: fn() = f; fn main() { let _: () = FN(); + //~^ ERROR: this let-binding has unit value } diff --git a/tests/ui/crashes/ice-8821.stderr b/tests/ui/crashes/ice-8821.stderr new file mode 100644 index 000000000000..dcd6fbef2215 --- /dev/null +++ b/tests/ui/crashes/ice-8821.stderr @@ -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 + diff --git a/tests/ui/let_unit.fixed b/tests/ui/let_unit.fixed index 6d984a495d2b..49e24b8ce5cd 100644 --- a/tests/ui/let_unit.fixed +++ b/tests/ui/let_unit.fixed @@ -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 @@ -90,19 +93,24 @@ 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(mut x: Vec) -> T { x.pop().unwrap() } let _: () = f4(vec![()]); - let x: () = f4(vec![()]); + let _: () = f4(vec![()]); + //~^ let_unit_value let _: () = { let x = 5; @@ -110,7 +118,8 @@ fn _returns_generic() { }; 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 @@ -160,7 +169,8 @@ fn _returns_generic() { { let _: () = x; let _: () = y; - let _: () = z; + z; + //~^ let_unit_value let _: () = x1; let _: () = x2; let _: () = opt; diff --git a/tests/ui/let_unit.rs b/tests/ui/let_unit.rs index a0e32f0b67a0..6a4b6be93795 100644 --- a/tests/ui/let_unit.rs +++ b/tests/ui/let_unit.rs @@ -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 @@ -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(mut x: Vec) -> T { x.pop().unwrap() } let _: () = f4(vec![()]); let x: () = f4(vec![()]); + //~^ let_unit_value let _: () = { let x = 5; @@ -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 @@ -161,6 +170,7 @@ fn _returns_generic() { let _: () = x; let _: () = y; let _: () = z; + //~^ let_unit_value let _: () = x1; let _: () = x2; let _: () = opt; diff --git a/tests/ui/let_unit.stderr b/tests/ui/let_unit.stderr index 6e7b958df4d9..2f545ccd1b8a 100644 --- a/tests/ui/let_unit.stderr +++ b/tests/ui/let_unit.stderr @@ -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 | | @@ -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 | | @@ -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(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -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(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -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(), | ^^^^^^^^^^^^^^^^^^ @@ -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 {} | ^^^^^^^^^^^^^^ @@ -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"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -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!(); | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -128,5 +208,5 @@ LL | LL ~ Foo { value: () }; | -error: aborting due to 9 previous errors +error: aborting due to 17 previous errors