Coming from #133167 (comment) and from this new forum thread, my takeaway of some remaining clear false positives is:
-
if the if let matches on an enum and only some enum variant(s) have fields with potentially-significant destructor, but those are exhaustively matched in the non-else case of the if let, then there can be no actual change in behavior, because the else case is only reached in case the drop is not significant
-
in the case of cargo-edit, this was if let Some(…irrefutable…) = expr on an Option<T>
-
in the urlo thread, I’ve identified the case of if let Err(…irrefutable…) on a Result<(), E>
Here’s a simple repro for an Option case:
Code
#![allow(unused)]
#![warn(if_let_rescope)]
struct Struct;
impl Drop for Struct {
fn drop(&mut self) {}
}
fn f(option_s: Option<Struct>) {
if let Some(s) = option_s {
// …
} else {
// …
}
}
Here’s the code in the playground.
Current output
warning: `if let` assigns a shorter lifetime since Edition 2024
--> src/lib.rs:10:8
|
10 | if let Some(s) = option_s {
| ^^^^^^^^^^^^^^--------
| |
| this value has a significant drop implementation which may observe a major change in drop order and requires your discretion
|
= warning: this changes meaning in Rust 2024
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-if-let-scope.html>
help: the value is now dropped here in Edition 2024
--> src/lib.rs:12:5
|
12 | } else {
| ^
note: the lint level is defined here
--> src/lib.rs:2:9
|
2 | #![warn(if_let_rescope)]
| ^^^^^^^^^^^^^^
help: a `match` with a single arm can preserve the drop order up to Edition 2021
|
10 ~ match option_s { Some(s) => {
11 | // …
12 ~ } _ => {
13 | // …
14 ~ }}
|
@rustbot label A-edition-2024, A-lints, L-if_let_rescope, C-enhancement
Coming from #133167 (comment) and from this new forum thread, my takeaway of some remaining clear false positives is:
if the
if letmatches on an enum and only some enum variant(s) have fields with potentially-significant destructor, but those are exhaustively matched in the non-elsecase of theif let, then there can be no actual change in behavior, because theelsecase is only reached in case the drop is not significantin the case of
cargo-edit, this wasif let Some(…irrefutable…) = expron anOption<T>in the urlo thread, I’ve identified the case of
if let Err(…irrefutable…)on aResult<(), E>Here’s a simple repro for an
Optioncase:Code
Here’s the code in the playground.
Current output
@rustbot label A-edition-2024, A-lints, L-if_let_rescope, C-enhancement