Skip to content

Commit

Permalink
Consider lvalues of field and index as possibly temporary places
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Feb 24, 2025
1 parent 2797936 commit bad8e98
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
8 changes: 8 additions & 0 deletions compiler/rustc_lint/src/if_let_rescope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@ impl<'tcx> Visitor<'tcx> for FindSignificantDropper<'_, 'tcx> {
self.check_promoted_temp_with_drop(expr)?;
intravisit::walk_expr(self, expr)
}
// `(Drop, ()).1` introduces a temporary and then moves out of
// part of it, therefore we should check it for temporaries.
// FIXME: This may have false positives if we move the part
// that actually has drop, but oh well.
hir::ExprKind::Index(expr, _, _) | hir::ExprKind::Field(expr, _) => {
self.check_promoted_temp_with_drop(expr)?;
intravisit::walk_expr(self, expr)
}
// If always introduces a temporary terminating scope for its cond and arms,
// so don't visit them.
hir::ExprKind::If(..) => ControlFlow::Continue(()),
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/drop/lint-if-let-rescope-false-positives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ fn main() {

// Make sure we don't lint if we consume the droppy value.
if let None = consume(Drop) {} else {}

// Make sure we don't lint on field exprs of place exprs.
let tup_place = (Drop, ());
if let None = consume(tup_place.1) {} else {}
}
6 changes: 6 additions & 0 deletions tests/ui/drop/lint-if-let-rescope.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ fn main() {
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
}

match Some((droppy(), ()).1) { Some(_value) => {} _ => {}}
//~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
//~| WARN: this changes meaning in Rust 2024
//~| HELP: the value is now dropped here in Edition 2024
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021

// We want to keep the `if let`s below as direct descendents of match arms,
// so the formatting is suppressed.
#[rustfmt::skip]
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/drop/lint-if-let-rescope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ fn main() {
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
}

if let Some(_value) = Some((droppy(), ()).1) {} else {}
//~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
//~| WARN: this changes meaning in Rust 2024
//~| HELP: the value is now dropped here in Edition 2024
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021

// We want to keep the `if let`s below as direct descendents of match arms,
// so the formatting is suppressed.
#[rustfmt::skip]
Expand Down
23 changes: 22 additions & 1 deletion tests/ui/drop/lint-if-let-rescope.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,26 @@ LL - while (if let Some(_value) = droppy().get() { false } else { true }) {
LL + while (match droppy().get() { Some(_value) => { false } _ => { true }}) {
|

error: aborting due to 7 previous errors
error: `if let` assigns a shorter lifetime since Edition 2024
--> $DIR/lint-if-let-rescope.rs:97:8
|
LL | if let Some(_value) = Some((droppy(), ()).1) {} else {}
| ^^^^^^^^^^^^^^^^^^^^^^^^--------------^^^
| |
| 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
--> $DIR/lint-if-let-rescope.rs:97:51
|
LL | if let Some(_value) = Some((droppy(), ()).1) {} else {}
| ^
help: a `match` with a single arm can preserve the drop order up to Edition 2021
|
LL - if let Some(_value) = Some((droppy(), ()).1) {} else {}
LL + match Some((droppy(), ()).1) { Some(_value) => {} _ => {}}
|

error: aborting due to 8 previous errors

0 comments on commit bad8e98

Please sign in to comment.