Skip to content

Commit fbbc964

Browse files
committed
Auto merge of rust-lang#129466 - dingxiangfei2009:let-chain-lint-crater-run, r=<try>
[CRATER RUN DO NOT MERGE] Let chain lint crater run Tracked by rust-lang#124085 Related to rust-lang#107251 cc `@jieyouxu` for review context cc `@traviscross` for edition tracking There is one unresolved issue that `cargo fix --edition` does not emit `if-let-rescope` lint. Details in rust-lang/cargo#14447. Note that this patch is assuming that the feature gate `if_let_rescope` is always on just for this crater run.
2 parents 5fe0e40 + 083cf03 commit fbbc964

File tree

8 files changed

+83
-7
lines changed

8 files changed

+83
-7
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
shallow = true
55
[submodule "src/tools/cargo"]
66
path = src/tools/cargo
7-
url = https://github.com/rust-lang/cargo.git
7+
url = https://github.com/dingxiangfei2009/cargo.git
88
shallow = true
99
[submodule "src/doc/reference"]
1010
path = src/doc/reference

compiler/rustc_hir_analysis/src/check/region.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
472472

473473
hir::ExprKind::If(cond, then, Some(otherwise)) => {
474474
let expr_cx = visitor.cx;
475-
let data = if expr.span.at_least_rust_2024() && visitor.tcx.features().if_let_rescope {
475+
let data = if expr.span.at_least_rust_2024() {
476476
ScopeData::IfThenRescope
477477
} else {
478478
ScopeData::IfThen
@@ -487,7 +487,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
487487

488488
hir::ExprKind::If(cond, then, None) => {
489489
let expr_cx = visitor.cx;
490-
let data = if expr.span.at_least_rust_2024() && visitor.tcx.features().if_let_rescope {
490+
let data = if expr.span.at_least_rust_2024() {
491491
ScopeData::IfThenRescope
492492
} else {
493493
ScopeData::IfThen

compiler/rustc_lint/src/if_let_rescope.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ declare_lint! {
2424
/// ### Example
2525
///
2626
/// ```rust,edition2021
27-
/// #![feature(if_let_rescope)]
2827
/// #![warn(if_let_rescope)]
2928
/// #![allow(unused_variables)]
3029
///
@@ -239,7 +238,7 @@ impl_lint_pass!(
239238

240239
impl<'tcx> LateLintPass<'tcx> for IfLetRescope {
241240
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 {
241+
if expr.span.edition().at_least_rust_2024() {
243242
return;
244243
}
245244
if let (Level::Allow, _) = cx.tcx.lint_level_at_node(IF_LET_RESCOPE, expr.hir_id) {

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ impl<'tcx> Cx<'tcx> {
707707
if_then_scope: region::Scope {
708708
id: then.hir_id.local_id,
709709
data: {
710-
if expr.span.at_least_rust_2024() && tcx.features().if_let_rescope {
710+
if expr.span.at_least_rust_2024() {
711711
region::ScopeData::IfThenRescope
712712
} else {
713713
region::ScopeData::IfThen

src/tools/cargo

Submodule cargo updated 182 files
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+
}
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+
}
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)