`unused_assignments` triggered by closure even though value is used
Open
Nobody has claimed this yet.
A-closures
A-diagnostics
A-lints
D-imprecise-spans
L-unused_assignments
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by compiling the minimal Rust closure example from the issue and compare the current diagnostic with the desired output. Trace the compiler's unused-assignment analysis for moved closures; done when it reports the initial assignment as unread, avoids the incorrect captured-value warning, and preserves the behavior shown in the second example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100