Open
Description
Code
fn main() {
let mut a: Option<u32> = None;
let mut cl = move || {
a = Some(42);
a.take().unwrap()
};
println!("{}", cl())
}
Current output
warning: value captured by `a` is never read
--> src/main.rs:4:9
|
4 | a = Some(42);
| ^
|
= help: did you mean to capture by reference instead?
= note: `#[warn(unused_assignments)]` on by default
Desired output
warning: value assigned to `a` is never read
--> src/main.rs:2:13
|
2 | let mut a: Option<u32> = None;
| ^
|
= help: maybe it is overwritten before being read?
= note: `#[warn(unused_assignments)]` on by default
Rationale and extra context
No response
Other cases
If the initial value is used, then the warning goes away, suggesting that it should warn on that instead:
fn main() {
let mut a: Option<u32> = None;
let mut cl = move || {
let _ = a;
a = Some(42);
a.take().unwrap()
};
println!("{}", cl())
}
Rust Version
rustc 1.89.0-nightly (d97326eab 2025-05-15)
binary: rustc
commit-hash: d97326eabfc3b2c33abcb08d6bc117aefa697cb7
commit-date: 2025-05-15
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.4
Anything else?
No response
Metadata
Metadata
Assignees
Labels
Area: Closures (`|…| { … }`)Area: Messages for errors, warnings, and lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Diagnostics: spans don't point to exactly the erroneous codeLint: unused_assignmentsRelevant to the compiler team, which will review and decide on the PR/issue.