mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Use a common subdiagnostic format for generic borrows
This is setup for unifing the logic for suggestions to borrow arguments in generic positions. As of this commit, it's still special cases for `AsRef`/`Borrow`-like traits and `Fn`-like traits.
This commit is contained in:
parent
2280d8ea00
commit
ea37000b56
@ -478,7 +478,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut is_mut = false;
|
let mut mutbl = ty::Mutability::Not;
|
||||||
if let Some(param) = arg_param
|
if let Some(param) = arg_param
|
||||||
&& self
|
&& self
|
||||||
.infcx
|
.infcx
|
||||||
@ -504,7 +504,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
|||||||
]
|
]
|
||||||
.contains(&Some(predicate.def_id()))
|
.contains(&Some(predicate.def_id()))
|
||||||
{
|
{
|
||||||
is_mut = true;
|
mutbl = ty::Mutability::Mut;
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@ -514,13 +514,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
|||||||
{
|
{
|
||||||
// The type of the argument corresponding to the expression that got moved
|
// The type of the argument corresponding to the expression that got moved
|
||||||
// is a type parameter `T`, which is has a `T: AsRef` obligation.
|
// is a type parameter `T`, which is has a `T: AsRef` obligation.
|
||||||
|
let place_desc = if let Some(desc) = self.describe_place(moved_place) {
|
||||||
|
format!("`{desc}`")
|
||||||
|
} else {
|
||||||
|
"here".to_owned()
|
||||||
|
};
|
||||||
err.span_suggestion_verbose(
|
err.span_suggestion_verbose(
|
||||||
expr.span.shrink_to_lo(),
|
expr.span.shrink_to_lo(),
|
||||||
"borrow the value to avoid moving it",
|
format!("consider {}borrowing {place_desc}", mutbl.mutably_str()),
|
||||||
format!("&{}", if is_mut { "mut " } else { "" }),
|
mutbl.ref_prefix_str(),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
can_suggest_clone = is_mut;
|
can_suggest_clone = mutbl.is_mut();
|
||||||
} else if let Some(local_def_id) = def_id.as_local()
|
} else if let Some(local_def_id) = def_id.as_local()
|
||||||
&& let node = self.infcx.tcx.hir_node_by_def_id(local_def_id)
|
&& let node = self.infcx.tcx.hir_node_by_def_id(local_def_id)
|
||||||
&& let Some(fn_decl) = node.fn_decl()
|
&& let Some(fn_decl) = node.fn_decl()
|
||||||
|
@ -8,7 +8,7 @@ LL | foo(bar);
|
|||||||
LL | let _baa = bar;
|
LL | let _baa = bar;
|
||||||
| ^^^ value used here after move
|
| ^^^ value used here after move
|
||||||
|
|
|
|
||||||
help: borrow the value to avoid moving it
|
help: consider borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | foo(&bar);
|
LL | foo(&bar);
|
||||||
| +
|
| +
|
||||||
@ -31,7 +31,7 @@ LL | struct Bar;
|
|||||||
...
|
...
|
||||||
LL | qux(bar);
|
LL | qux(bar);
|
||||||
| --- you could clone this value
|
| --- you could clone this value
|
||||||
help: borrow the value to avoid moving it
|
help: consider mutably borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | qux(&mut bar);
|
LL | qux(&mut bar);
|
||||||
| ++++
|
| ++++
|
||||||
@ -46,7 +46,7 @@ LL | bat(bar);
|
|||||||
LL | let _baa = bar;
|
LL | let _baa = bar;
|
||||||
| ^^^ value used here after move
|
| ^^^ value used here after move
|
||||||
|
|
|
|
||||||
help: borrow the value to avoid moving it
|
help: consider borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | bat(&bar);
|
LL | bat(&bar);
|
||||||
| +
|
| +
|
||||||
@ -69,7 +69,7 @@ LL | struct Bar;
|
|||||||
...
|
...
|
||||||
LL | baz(bar);
|
LL | baz(bar);
|
||||||
| --- you could clone this value
|
| --- you could clone this value
|
||||||
help: borrow the value to avoid moving it
|
help: consider mutably borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | baz(&mut bar);
|
LL | baz(&mut bar);
|
||||||
| ++++
|
| ++++
|
||||||
|
@ -8,15 +8,15 @@ extern crate suggest_borrow_for_generic_arg_aux as aux;
|
|||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let bar = aux::Bar;
|
let bar = aux::Bar;
|
||||||
aux::foo(&bar); //~ HELP borrow the value
|
aux::foo(&bar); //~ HELP consider borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
let mut bar = aux::Bar;
|
let mut bar = aux::Bar;
|
||||||
aux::qux(&mut bar); //~ HELP borrow the value
|
aux::qux(&mut bar); //~ HELP consider mutably borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
let bar = aux::Bar;
|
let bar = aux::Bar;
|
||||||
aux::bat(&bar); //~ HELP borrow the value
|
aux::bat(&bar); //~ HELP consider borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
let mut bar = aux::Bar;
|
let mut bar = aux::Bar;
|
||||||
aux::baz(&mut bar); //~ HELP borrow the value
|
aux::baz(&mut bar); //~ HELP consider mutably borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,15 @@ extern crate suggest_borrow_for_generic_arg_aux as aux;
|
|||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let bar = aux::Bar;
|
let bar = aux::Bar;
|
||||||
aux::foo(bar); //~ HELP borrow the value
|
aux::foo(bar); //~ HELP consider borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
let mut bar = aux::Bar;
|
let mut bar = aux::Bar;
|
||||||
aux::qux(bar); //~ HELP borrow the value
|
aux::qux(bar); //~ HELP consider mutably borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
let bar = aux::Bar;
|
let bar = aux::Bar;
|
||||||
aux::bat(bar); //~ HELP borrow the value
|
aux::bat(bar); //~ HELP consider borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
let mut bar = aux::Bar;
|
let mut bar = aux::Bar;
|
||||||
aux::baz(bar); //~ HELP borrow the value
|
aux::baz(bar); //~ HELP consider mutably borrowing `bar`
|
||||||
let _baa = bar; //~ ERROR use of moved value
|
let _baa = bar; //~ ERROR use of moved value
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ LL | aux::foo(bar);
|
|||||||
LL | let _baa = bar;
|
LL | let _baa = bar;
|
||||||
| ^^^ value used here after move
|
| ^^^ value used here after move
|
||||||
|
|
|
|
||||||
help: borrow the value to avoid moving it
|
help: consider borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | aux::foo(&bar);
|
LL | aux::foo(&bar);
|
||||||
| +
|
| +
|
||||||
@ -23,7 +23,7 @@ LL | aux::qux(bar);
|
|||||||
LL | let _baa = bar;
|
LL | let _baa = bar;
|
||||||
| ^^^ value used here after move
|
| ^^^ value used here after move
|
||||||
|
|
|
|
||||||
help: borrow the value to avoid moving it
|
help: consider mutably borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | aux::qux(&mut bar);
|
LL | aux::qux(&mut bar);
|
||||||
| ++++
|
| ++++
|
||||||
@ -38,7 +38,7 @@ LL | aux::bat(bar);
|
|||||||
LL | let _baa = bar;
|
LL | let _baa = bar;
|
||||||
| ^^^ value used here after move
|
| ^^^ value used here after move
|
||||||
|
|
|
|
||||||
help: borrow the value to avoid moving it
|
help: consider borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | aux::bat(&bar);
|
LL | aux::bat(&bar);
|
||||||
| +
|
| +
|
||||||
@ -53,7 +53,7 @@ LL | aux::baz(bar);
|
|||||||
LL | let _baa = bar;
|
LL | let _baa = bar;
|
||||||
| ^^^ value used here after move
|
| ^^^ value used here after move
|
||||||
|
|
|
|
||||||
help: borrow the value to avoid moving it
|
help: consider mutably borrowing `bar`
|
||||||
|
|
|
|
||||||
LL | aux::baz(&mut bar);
|
LL | aux::baz(&mut bar);
|
||||||
| ++++
|
| ++++
|
||||||
|
Loading…
Reference in New Issue
Block a user