-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve behavior of IF_LET_RESCOPE
around temporaries and place expressions
#137444
base: master
Are you sure you want to change the base?
Conversation
cc @dingxiangfei2009 #107251 (and @jieyouxu who reviewed it) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
The #137376 fix would need to be a bit restricted to limit false negatives. If the E.g. wrapping a #![warn(rust_2024_compatibility)]
struct Droppy(u16);
impl Drop for Droppy {
fn drop(&mut self) {
println!("drop(Droppy({}))", self.0)
}
}
fn do_stuff_with(property: bool) {
println!("do_stuff_with({property})");
}
fn main() {
f1();
f2();
}
fn f1() {
println!("==================== f1 ====================");
do_stuff_with(if let Some(_x) = Some(Droppy(1337)) {
true
} else {
false
});
}
fn f2() {
println!("==================== f2 ====================");
do_stuff_with(if let Some(_x) = Some((Droppy(42), Droppy(69)).0) {
true
} else {
false
});
} 2021:
2024:
At least in the second case, there’s still a temporary left that escapes the And if the pattern does not consume the contents of the
2024:
so well… one could either conservatively trying to just keep the lint on all of these [of course currently, they are all linted], or trying to notice the specific un-problematic case. Half of noticing the specific case was about whether there are any other temporaries around. Those are arguably still relevant even without any fn f3() {
if let Some(()) = (Droppy(123), None).1 {} else {
println!("else {{\n …\n}}");
};
} 2021:
2024:
also compare the playground linked in #133753 |
Ok, well I don't think #137376 is fixable in general given how little information we have in this point of the compiler about match exhaustivity and what temporaries have definitely been "accounted for" (i.e. moved or dropped). The case of |
Perhaps you were already doing this, but it seems worthwhile to capture @steffahn's examples there as tests so that if someone tries again, we'll have to confront those. |
@steffahn is welcome to put up a separate PR to make them into tests. |
2a9cefe
to
86ec0ea
Compare
86ec0ea
to
bad8e98
Compare
@bors r+ rollup |
Improve behavior of `IF_LET_RESCOPE` around temporaries and place expressions Heavily reworks the `IF_LET_RESCOPE` to be more sensitive around 1. temporaries that get consumed/terminated and therefore should not trigger the lint, and 2. borrows of place expressions, which are not temporary values. Fixes rust-lang#137411
Rollup of 8 pull requests Successful merges: - rust-lang#137370 (adjust_abi: make fallback logic for ABIs a bit easier to read) - rust-lang#137444 (Improve behavior of `IF_LET_RESCOPE` around temporaries and place expressions) - rust-lang#137464 (Fix invalid suggestion from type error for derive macro) - rust-lang#137539 ( Add rustdoc-gui regression test for rust-lang#137082 ) - rust-lang#137576 (Don't doc-comment BTreeMap<K, SetValZST, A>) - rust-lang#137595 (remove `simd_fpow` and `simd_fpowi`) - rust-lang#137600 (type_ir: remove redundant part of comment) - rust-lang#137602 (feature: fix typo in attribute description) r? `@ghost` `@rustbot` modify labels: rollup
Heavily reworks the
IF_LET_RESCOPE
to be more sensitive around 1. temporaries that get consumed/terminated and therefore should not trigger the lint, and 2. borrows of place expressions, which are not temporary values.Fixes #137411