Open
Description
Summary
When returning the result of a function fn f<T>(...) -> T
from a lambda || -> () { ... }
, removing the lambda’s return type annotation leads to a type inference failure. The unused_unit
lint should not trigger in that case because the unit is not actually unused.
Lint Name
unused_unit
Reproducer
I tried this code:
trait Convert<T> {
fn from(value: T) -> Self;
}
impl Convert<u64> for () {
fn from(_value: u64) -> Self {}
}
fn handle<T>(value: u64) -> T where T: Convert<u64> {
eprintln!("error: {value}");
Convert::from(value)
}
pub fn f() -> Option<bool> {
let result: Result<bool, u64> = Err(42);
result.map_err(|err| -> () { handle(err) }).ok()
}
pub fn main() {
dbg!(f());
}
I saw this happen:
warning: unneeded unit return type
--> src/main.rs:16:25
|
16 | result.map_err(|err| -> () { handle(err) }).ok()
| ^^^^^^ help: remove the `-> ()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit
= note: `#[warn(clippy::unused_unit)]` on by default
warning: `t` (bin "t") generated 1 warning (run `cargo clippy --fix --bin "t"` to apply 1 suggestion)
I expected to see this happen: No warning should be generated.
Godbolt link: https://godbolt.org/z/ojv53fWe8
Version
rustc 1.89.0-nightly (d13a431a6 2025-06-09)
binary: rustc
commit-hash: d13a431a6cc69cd65efe7c3eb7808251d6fd7a46
commit-date: 2025-06-09
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
Additional Labels
@rustbot label +I-suggestion-causes-error