Skip to content
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

[CRATER RUN DO NOT MERGE] crater run to evaluate effectiveness of lint for if_let_rescope #129466

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
shallow = true
[submodule "src/tools/cargo"]
path = src/tools/cargo
url = https://github.com/rust-lang/cargo.git
url = https://github.com/dingxiangfei2009/cargo.git
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: maybe this also needs to be on a separate branch?

shallow = true
[submodule "src/doc/reference"]
path = src/doc/reference
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h

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

hir::ExprKind::If(cond, then, None) => {
let expr_cx = visitor.cx;
let data = if expr.span.at_least_rust_2024() && visitor.tcx.features().if_let_rescope {
let data = if expr.span.at_least_rust_2024() {
ScopeData::IfThenRescope
} else {
ScopeData::IfThen
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/if_let_rescope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl_lint_pass!(

impl<'tcx> LateLintPass<'tcx> for IfLetRescope {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
if expr.span.edition().at_least_rust_2024() || !cx.tcx.features().if_let_rescope {
if expr.span.edition().at_least_rust_2024() {
return;
}
if let (Level::Allow, _) = cx.tcx.lint_level_at_node(IF_LET_RESCOPE, expr.hir_id) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ impl<'tcx> Cx<'tcx> {
if_then_scope: region::Scope {
id: then.hir_id.local_id,
data: {
if expr.span.at_least_rust_2024() && tcx.features().if_let_rescope {
if expr.span.at_least_rust_2024() {
region::ScopeData::IfThenRescope
} else {
region::ScopeData::IfThen
Expand Down
2 changes: 1 addition & 1 deletion src/tools/cargo
26 changes: 26 additions & 0 deletions tests/ui/drop/if-let-rescope-suggestions.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//@ edition:2024
//@ compile-flags: -Z validate-mir -Zunstable-options
//@ run-rustfix

#![deny(if_let_rescope)]
#![feature(if_let_rescope)]

struct Droppy;
impl Drop for Droppy {
fn drop(&mut self) {
println!("dropped");
}
}
impl Droppy {
fn get_ref(&self) -> Option<&u8> {
None
}
}

fn do_something<T>(_: &T) {}

fn main() {
let binding = Droppy;
do_something(match binding.get_ref() { Some(value) => { value } _ => { &0 }});
//~^ ERROR: temporary value dropped while borrowed
}
25 changes: 25 additions & 0 deletions tests/ui/drop/if-let-rescope-suggestions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ edition:2024
//@ compile-flags: -Z validate-mir -Zunstable-options
//@ run-rustfix

#![deny(if_let_rescope)]
#![feature(if_let_rescope)]

struct Droppy;
impl Drop for Droppy {
fn drop(&mut self) {
println!("dropped");
}
}
impl Droppy {
fn get_ref(&self) -> Option<&u8> {
None
}
}

fn do_something<T>(_: &T) {}

fn main() {
do_something(if let Some(value) = Droppy.get_ref() { value } else { &0 });
//~^ ERROR: temporary value dropped while borrowed
}
26 changes: 26 additions & 0 deletions tests/ui/drop/if-let-rescope-suggestions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/if-let-rescope-suggestions.rs:23:39
|
LL | do_something(if let Some(value) = Droppy.get_ref() { value } else { &0 });
| ^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
|
note: lifetime for temporaries generated in `if let`s have been shorted in Edition 2024
--> $DIR/if-let-rescope-suggestions.rs:23:65
|
LL | do_something(if let Some(value) = Droppy.get_ref() { value } else { &0 });
| ^
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = Droppy;
LL ~ do_something(if let Some(value) = binding.get_ref() { value } else { &0 });
|
help: consider rewriting the `if` into `match` which preserves the extended lifetime
|
LL | do_something(match Droppy.get_ref() { Some(value) => { value } _ => { &0 }});
| ~~~~~ ++++++++++++++++ ~~~~ +

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0716`.
Loading