Skip to content

Suggest mut when possbile for temporary value dropped while borrowed #141069

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

Merged
merged 1 commit into from
Jun 13, 2025
Merged
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
14 changes: 13 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3229,8 +3229,20 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
Applicability::MaybeIncorrect,
);
}

let mutability = if matches!(borrow.kind(), BorrowKind::Mut { .. }) {
"mut "
} else {
""
};

if !is_format_arguments_item {
let addition = format!("let binding = {};\n{}", s, " ".repeat(p));
let addition = format!(
"let {}binding = {};\n{}",
mutability,
s,
" ".repeat(p)
);
err.multipart_suggestion_verbose(
msg,
vec![
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/coroutine/auto-trait-regions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | assert_foo(a);
|
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let mut binding = true;
LL ~ let a = A(&mut binding, &mut true, No);
|

Expand All @@ -28,7 +28,7 @@ LL | assert_foo(a);
|
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let mut binding = true;
LL ~ let a = A(&mut true, &mut binding, No);
|

Expand Down
23 changes: 23 additions & 0 deletions tests/ui/nll/sugg-mut-for-binding-issue-137486.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ run-rustfix
#![allow(unused_assignments)]

use std::pin::Pin;
fn main() {
let mut s = String::from("hello");
let mut ref_s = &mut s;

let mut binding = String::from("world");
ref_s = &mut binding; //~ ERROR temporary value dropped while borrowed [E0716]

print!("r1 = {}", ref_s);

let mut val: u8 = 5;
let mut s = Pin::new(&mut val);
let mut ref_s = &mut s;

let mut val2: u8 = 10;
let mut binding = Pin::new(&mut val2);
ref_s = &mut binding; //~ ERROR temporary value dropped while borrowed [E0716]

print!("r1 = {}", ref_s);
}
21 changes: 21 additions & 0 deletions tests/ui/nll/sugg-mut-for-binding-issue-137486.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ run-rustfix
#![allow(unused_assignments)]

use std::pin::Pin;
fn main() {
let mut s = String::from("hello");
let mut ref_s = &mut s;

ref_s = &mut String::from("world"); //~ ERROR temporary value dropped while borrowed [E0716]

print!("r1 = {}", ref_s);

let mut val: u8 = 5;
let mut s = Pin::new(&mut val);
let mut ref_s = &mut s;

let mut val2: u8 = 10;
ref_s = &mut Pin::new(&mut val2); //~ ERROR temporary value dropped while borrowed [E0716]

print!("r1 = {}", ref_s);
}
37 changes: 37 additions & 0 deletions tests/ui/nll/sugg-mut-for-binding-issue-137486.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/sugg-mut-for-binding-issue-137486.rs:9:18
|
LL | ref_s = &mut String::from("world");
| ^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
LL |
LL | print!("r1 = {}", ref_s);
| ----- borrow later used here
|
help: consider using a `let` binding to create a longer lived value
|
LL ~ let mut binding = String::from("world");
LL ~ ref_s = &mut binding;
|

error[E0716]: temporary value dropped while borrowed
--> $DIR/sugg-mut-for-binding-issue-137486.rs:18:18
|
LL | ref_s = &mut Pin::new(&mut val2);
| ^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
LL |
LL | print!("r1 = {}", ref_s);
| ----- borrow later used here
|
help: consider using a `let` binding to create a longer lived value
|
LL ~ let mut binding = Pin::new(&mut val2);
LL ~ ref_s = &mut binding;
|

error: aborting due to 2 previous errors

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