Skip to content

Commit 1544905

Browse files
crater run
1 parent e9e13a6 commit 1544905

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl_lint_pass!(
239239

240240
impl<'tcx> LateLintPass<'tcx> for IfLetRescope {
241241
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
242-
if expr.span.edition().at_least_rust_2024() || !cx.tcx.features().if_let_rescope {
242+
if expr.span.edition().at_least_rust_2024() {
243243
return;
244244
}
245245
if let (Level::Allow, _) = cx.tcx.lint_level_at_node(IF_LET_RESCOPE, expr.hir_id) {

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

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ edition:2024
2+
//@ compile-flags: -Z validate-mir -Zunstable-options
3+
//@ run-rustfix
4+
5+
#![deny(if_let_rescope)]
6+
#![feature(if_let_rescope)]
7+
8+
struct Droppy;
9+
impl Drop for Droppy {
10+
fn drop(&mut self) {
11+
println!("dropped");
12+
}
13+
}
14+
impl Droppy {
15+
fn get_ref(&self) -> Option<&u8> {
16+
None
17+
}
18+
}
19+
20+
fn do_something<T>(_: &T) {}
21+
22+
fn main() {
23+
let binding = Droppy;
24+
do_something(match binding.get_ref() { Some(value) => { value } _ => { &0 }});
25+
//~^ ERROR: temporary value dropped while borrowed
26+
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ edition:2024
2+
//@ compile-flags: -Z validate-mir -Zunstable-options
3+
//@ run-rustfix
4+
5+
#![deny(if_let_rescope)]
6+
#![feature(if_let_rescope)]
7+
8+
struct Droppy;
9+
impl Drop for Droppy {
10+
fn drop(&mut self) {
11+
println!("dropped");
12+
}
13+
}
14+
impl Droppy {
15+
fn get_ref(&self) -> Option<&u8> {
16+
None
17+
}
18+
}
19+
20+
fn do_something<T>(_: &T) {}
21+
22+
fn main() {
23+
do_something(if let Some(value) = Droppy.get_ref() { value } else { &0 });
24+
//~^ ERROR: temporary value dropped while borrowed
25+
}

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

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/if-let-rescope-suggestions.rs:23:39
3+
|
4+
LL | do_something(if let Some(value) = Droppy.get_ref() { value } else { &0 });
5+
| ^^^^^^ - temporary value is freed at the end of this statement
6+
| |
7+
| creates a temporary value which is freed while still in use
8+
|
9+
note: lifetime for temporaries generated in `if let`s have been shorted in Edition 2024
10+
--> $DIR/if-let-rescope-suggestions.rs:23:65
11+
|
12+
LL | do_something(if let Some(value) = Droppy.get_ref() { value } else { &0 });
13+
| ^
14+
help: consider using a `let` binding to create a longer lived value
15+
|
16+
LL ~ let binding = Droppy;
17+
LL ~ do_something(if let Some(value) = binding.get_ref() { value } else { &0 });
18+
|
19+
help: consider rewriting the `if` into `match` which preserves the extended lifetime
20+
|
21+
LL | do_something(match Droppy.get_ref() { Some(value) => { value } _ => { &0 }});
22+
| ~~~~~ ++++++++++++++++ ~~~~ +
23+
24+
error: aborting due to 1 previous error
25+
26+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)