Code
#[deny(unused_assignments)]
fn main() {
let mut x = 1;
catch(|| {
x = 2; // <-
panic!();
});
dbg!(x);
}
fn catch<F: FnOnce()>(f: F) {
if let Ok(true) = std::fs::exists("may_or_may_not_call_f") {
_ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
}
}
Current output
error: value assigned to `x` is never read
--> src/main.rs:5:9
|
5 | x = 2; // <-
| ^^^^^
|
= help: maybe it is overwritten before being read?
note: the lint level is defined here
--> src/main.rs:1:8
|
1 | #[deny(unused_assignments)]
| ^^^^^^^^^^^^^^^^^^
Desired output
Rationale and extra context
This issue only exists if there is a panic!() inside the closure, otherwise no warning is emitted.
The initial value and the assigned value are all useful depending on if catch calls the closure. Neither of them should be considered "unused".
Other cases
Rust Version
1.95.0-nightly (2026-02-02 f60a0f1bcc5a2a6dd8eb)
(reproduced on playground nightly)
Anything else?
playground link
The false positive only exists since 1.92.0 and the same code does not warn on 1.91.0.
Code
Current output
Desired output
Rationale and extra context
This issue only exists if there is a
panic!()inside the closure, otherwise no warning is emitted.The initial value and the assigned value are all useful depending on if
catchcalls the closure. Neither of them should be considered "unused".Other cases
Rust Version
Anything else?
playground link
The false positive only exists since 1.92.0 and the same code does not warn on 1.91.0.