Skip to content

Commit 86ec0ea

Browse files
Consider lvalues of field and index as possibly temporary places
1 parent 8920857 commit 86ec0ea

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

Diff for: compiler/rustc_lint/src/if_let_rescope.rs

+8
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,14 @@ impl<'tcx> Visitor<'tcx> for FindSignificantDropper<'_, 'tcx> {
436436
self.check_promoted_temp_with_drop(expr)?;
437437
intravisit::walk_expr(self, expr)
438438
}
439+
// `(Drop, ()).1` introduces a temporary and then moves out of
440+
// part of it, therefore we should check it for temporaries.
441+
// FIXME: This may have false positives if we move the part
442+
// that actually has drop, but oh well.
443+
hir::ExprKind::Index(expr, _, _) | hir::ExprKind::Field(expr, _) => {
444+
self.check_promoted_temp_with_drop(expr)?;
445+
intravisit::walk_expr(self, expr)
446+
}
439447
// If always introduces a temporary terminating scope for its cond and arms,
440448
// so don't visit them.
441449
hir::ExprKind::If(..) => ControlFlow::Continue(()),

Diff for: tests/ui/drop/lint-if-let-rescope-false-positives.rs

+4
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ fn main() {
2626

2727
// Make sure we don't lint if we consume the droppy value.
2828
if let None = consume(Drop) {} else {}
29+
30+
// Make sure we don't lint on field exprs of place exprs.
31+
let tup_place = (Drop, ());
32+
if let None = consume(tup_place.1) {} else {}
2933
}

Diff for: tests/ui/drop/lint-if-let-rescope.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ fn main() {
9494
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
9595
}
9696

97+
match Some((droppy(), ()).1) { Some(_value) => {} _ => {}}
98+
//~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
99+
//~| WARN: this changes meaning in Rust 2024
100+
//~| HELP: the value is now dropped here in Edition 2024
101+
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
102+
97103
// We want to keep the `if let`s below as direct descendents of match arms,
98104
// so the formatting is suppressed.
99105
#[rustfmt::skip]

Diff for: tests/ui/drop/lint-if-let-rescope.rs

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ fn main() {
9494
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
9595
}
9696

97+
if let Some(_value) = Some((droppy(), ()).1) {} else {}
98+
//~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
99+
//~| WARN: this changes meaning in Rust 2024
100+
//~| HELP: the value is now dropped here in Edition 2024
101+
//~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
102+
97103
// We want to keep the `if let`s below as direct descendents of match arms,
98104
// so the formatting is suppressed.
99105
#[rustfmt::skip]

Diff for: tests/ui/drop/lint-if-let-rescope.stderr

+22-1
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,26 @@ LL - while (if let Some(_value) = droppy().get() { false } else { true }) {
175175
LL + while (match droppy().get() { Some(_value) => { false } _ => { true }}) {
176176
|
177177

178-
error: aborting due to 7 previous errors
178+
error: `if let` assigns a shorter lifetime since Edition 2024
179+
--> $DIR/lint-if-let-rescope.rs:97:8
180+
|
181+
LL | if let Some(_value) = Some((droppy(), ()).1) {} else {}
182+
| ^^^^^^^^^^^^^^^^^^^^^^^^--------------^^^
183+
| |
184+
| this value has a significant drop implementation which may observe a major change in drop order and requires your discretion
185+
|
186+
= warning: this changes meaning in Rust 2024
187+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-if-let-scope.html>
188+
help: the value is now dropped here in Edition 2024
189+
--> $DIR/lint-if-let-rescope.rs:97:51
190+
|
191+
LL | if let Some(_value) = Some((droppy(), ()).1) {} else {}
192+
| ^
193+
help: a `match` with a single arm can preserve the drop order up to Edition 2021
194+
|
195+
LL - if let Some(_value) = Some((droppy(), ()).1) {} else {}
196+
LL + match Some((droppy(), ()).1) { Some(_value) => {} _ => {}}
197+
|
198+
199+
error: aborting due to 8 previous errors
179200

0 commit comments

Comments
 (0)