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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3201,12 +3201,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let expr_ty: Option<Ty<'_>> =
visitor.prop_expr.map(|expr| typeck_results.expr_ty(expr).peel_refs());

let is_format_arguments_item = if let Some(expr_ty) = expr_ty
let (is_format_arguments_item, is_pin_struct) = if let Some(expr_ty) =
expr_ty
&& let ty::Adt(adt, _) = expr_ty.kind()
{
self.infcx.tcx.is_lang_item(adt.did(), LangItem::FormatArguments)
(
self.infcx.tcx.is_lang_item(adt.did(), LangItem::FormatArguments),
(self.infcx.tcx.is_lang_item(adt.did(), LangItem::Pin)),
)
} else {
false
(false, false)
};

if visitor.found == 0
Expand All @@ -3229,8 +3233,22 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
Applicability::MaybeIncorrect,
);
}

let mutability = if matches!(borrow.kind(), BorrowKind::Mut { .. })
&& !is_pin_struct
{
"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
8 changes: 8 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,8 @@
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);
}
20 changes: 20 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,20 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/sugg-mut-for-binding-issue-137486.rs:5: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: aborting due to 1 previous error

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