Rollup merge of #136032 - estebank:issue-136028, r=SparrowLii
Account for mutable borrow in argument suggestion ``` error: value assigned to `object` is never read --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5 | LL | object = &mut object2; | ^^^^^^ | help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding | LL ~ fn change_object3(object: &mut Object) { LL | LL | let object2 = Object; LL ~ *object = object2; | ``` instead of ``` error: value assigned to `object` is never read --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5 | LL | object = &mut object2; | ^^^^^^ | help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding | LL ~ fn change_object3(object: &mut mut Object) { LL | LL | let object2 = Object; LL ~ *object = object2; | ``` Fix #136028.
This commit is contained in:
commit
64550d1ed8
6 changed files with 114 additions and 57 deletions
|
@ -851,32 +851,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
&& let hir::PatKind::Binding(hir::BindingMode::MUT, _hir_id, ident, _) = pat.kind
|
||||
|
||||
// Look for the type corresponding to the argument pattern we have in the argument list.
|
||||
&& let Some(ty_sugg) = fn_decl
|
||||
&& let Some(ty_ref) = fn_decl
|
||||
.inputs
|
||||
.iter()
|
||||
.filter_map(|ty| {
|
||||
if ty.span == *ty_span
|
||||
&& let hir::TyKind::Ref(lt, x) = ty.kind
|
||||
{
|
||||
// `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
|
||||
Some((
|
||||
x.ty.span.shrink_to_lo(),
|
||||
format!(
|
||||
"{}mut ",
|
||||
if lt.ident.span.lo() == lt.ident.span.hi() { "" } else { " " }
|
||||
),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.filter_map(|ty| match ty.kind {
|
||||
hir::TyKind::Ref(lt, mut_ty) if ty.span == *ty_span => Some((lt, mut_ty)),
|
||||
_ => None,
|
||||
})
|
||||
.next()
|
||||
{
|
||||
let sugg = vec![
|
||||
ty_sugg,
|
||||
let mut sugg = if ty_ref.1.mutbl.is_mut() {
|
||||
// Leave `&'name mut Ty` and `&mut Ty` as they are (#136028).
|
||||
vec![]
|
||||
} else {
|
||||
// `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
|
||||
vec![(
|
||||
ty_ref.1.ty.span.shrink_to_lo(),
|
||||
format!(
|
||||
"{}mut ",
|
||||
if ty_ref.0.ident.span.lo() == ty_ref.0.ident.span.hi() { "" } else { " " },
|
||||
),
|
||||
)]
|
||||
};
|
||||
sugg.extend([
|
||||
(pat.span.until(ident.span), String::new()),
|
||||
(lhs.span.shrink_to_lo(), "*".to_string()),
|
||||
];
|
||||
]);
|
||||
// We suggest changing the argument from `mut ident: &Ty` to `ident: &'_ mut Ty` and the
|
||||
// assignment from `ident = val;` to `*ident = val;`.
|
||||
err.multipart_suggestion_verbose(
|
||||
|
|
|
@ -1791,7 +1791,7 @@ pub(crate) struct UnusedAssign {
|
|||
pub(crate) struct UnusedAssignSuggestion {
|
||||
pub pre: &'static str,
|
||||
#[suggestion_part(code = "{pre}mut ")]
|
||||
pub ty_span: Span,
|
||||
pub ty_span: Option<Span>,
|
||||
#[suggestion_part(code = "")]
|
||||
pub ty_ref_span: Span,
|
||||
#[suggestion_part(code = "*")]
|
||||
|
|
|
@ -1620,24 +1620,28 @@ impl<'tcx> Liveness<'_, 'tcx> {
|
|||
&& let item = self.ir.tcx.hir_owner_node(item_id)
|
||||
&& let Some(fn_decl) = item.fn_decl()
|
||||
&& let hir::PatKind::Binding(hir::BindingMode::MUT, _hir_id, ident, _) = pat.kind
|
||||
&& let Some((ty_span, pre)) = fn_decl
|
||||
&& let Some((lt, mut_ty)) = fn_decl
|
||||
.inputs
|
||||
.iter()
|
||||
.filter_map(|ty| {
|
||||
if ty.span == *ty_span
|
||||
&& let hir::TyKind::Ref(lt, mut_ty) = ty.kind
|
||||
{
|
||||
// `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
|
||||
Some((
|
||||
mut_ty.ty.span.shrink_to_lo(),
|
||||
if lt.ident.span.lo() == lt.ident.span.hi() { "" } else { " " },
|
||||
))
|
||||
Some((lt, mut_ty))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.next()
|
||||
{
|
||||
let ty_span = if mut_ty.mutbl.is_mut() {
|
||||
// Leave `&'name mut Ty` and `&mut Ty` as they are (#136028).
|
||||
None
|
||||
} else {
|
||||
// `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
|
||||
Some(mut_ty.ty.span.shrink_to_lo())
|
||||
};
|
||||
let pre = if lt.ident.span.lo() == lt.ident.span.hi() { "" } else { " " };
|
||||
Some(errors::UnusedAssignSuggestion {
|
||||
ty_span,
|
||||
pre,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
//@ run-rustfix
|
||||
#![deny(unused_assignments, unused_variables)]
|
||||
#![allow(unused_mut)]
|
||||
struct Object;
|
||||
|
||||
fn change_object(object: &mut Object) { //~ HELP you might have meant to mutate
|
||||
|
@ -15,8 +16,17 @@ fn change_object2(object: &mut Object) { //~ ERROR variable `object` is assigned
|
|||
//~| ERROR value assigned to `object` is never read
|
||||
}
|
||||
|
||||
fn change_object3(object: &mut Object) { //~ ERROR variable `object` is assigned to, but never used
|
||||
//~^ HELP you might have meant to mutate
|
||||
let mut object2 = Object; //~ HELP consider changing this to be mutable
|
||||
*object = object2;
|
||||
//~^ ERROR cannot borrow `object2` as mutable
|
||||
//~| ERROR value assigned to `object` is never read
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut object = Object;
|
||||
change_object(&mut object);
|
||||
change_object2(&mut object);
|
||||
change_object3(&mut object);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
//@ run-rustfix
|
||||
#![deny(unused_assignments, unused_variables)]
|
||||
#![allow(unused_mut)]
|
||||
struct Object;
|
||||
|
||||
fn change_object(mut object: &Object) { //~ HELP you might have meant to mutate
|
||||
|
@ -15,8 +16,17 @@ fn change_object2(mut object: &Object) { //~ ERROR variable `object` is assigned
|
|||
//~| ERROR value assigned to `object` is never read
|
||||
}
|
||||
|
||||
fn change_object3(mut object: &mut Object) { //~ ERROR variable `object` is assigned to, but never used
|
||||
//~^ HELP you might have meant to mutate
|
||||
let object2 = Object; //~ HELP consider changing this to be mutable
|
||||
object = &mut object2;
|
||||
//~^ ERROR cannot borrow `object2` as mutable
|
||||
//~| ERROR value assigned to `object` is never read
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut object = Object;
|
||||
change_object(&mut object);
|
||||
change_object2(&mut object);
|
||||
change_object3(&mut object);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
error[E0308]: mismatched types
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:7:14
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:8:13
|
||||
|
|
||||
LL | fn change_object(mut object: &Object) {
|
||||
| ------- expected due to this parameter type
|
||||
|
@ -15,7 +15,7 @@ LL ~ *object = object2;
|
|||
|
|
||||
|
||||
error: value assigned to `object` is never read
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:13:5
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:14:4
|
||||
|
|
||||
LL | object = &object2;
|
||||
| ^^^^^^
|
||||
|
@ -34,7 +34,7 @@ LL ~ *object = object2;
|
|||
|
|
||||
|
||||
error: variable `object` is assigned to, but never used
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:10:23
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:11:23
|
||||
|
|
||||
LL | fn change_object2(mut object: &Object) {
|
||||
| ^^^^^^
|
||||
|
@ -47,7 +47,7 @@ LL | #![deny(unused_assignments, unused_variables)]
|
|||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0597]: `object2` does not live long enough
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:13:14
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:14:13
|
||||
|
|
||||
LL | fn change_object2(mut object: &Object) {
|
||||
| - let's call the lifetime of this reference `'1`
|
||||
|
@ -63,7 +63,40 @@ LL | object = &object2;
|
|||
LL | }
|
||||
| - `object2` dropped here while still borrowed
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: value assigned to `object` is never read
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:22:5
|
||||
|
|
||||
LL | object = &mut object2;
|
||||
| ^^^^^^
|
||||
|
|
||||
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
|
||||
|
|
||||
LL ~ fn change_object3(object: &mut Object) {
|
||||
LL |
|
||||
LL | let object2 = Object;
|
||||
LL ~ *object = object2;
|
||||
|
|
||||
|
||||
Some errors have detailed explanations: E0308, E0597.
|
||||
error: variable `object` is assigned to, but never used
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:19:23
|
||||
|
|
||||
LL | fn change_object3(mut object: &mut Object) {
|
||||
| ^^^^^^
|
||||
|
|
||||
= note: consider using `_object` instead
|
||||
|
||||
error[E0596]: cannot borrow `object2` as mutable, as it is not declared as mutable
|
||||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:22:14
|
||||
|
|
||||
LL | object = &mut object2;
|
||||
| ^^^^^^^^^^^^ cannot borrow as mutable
|
||||
|
|
||||
help: consider changing this to be mutable
|
||||
|
|
||||
LL | let mut object2 = Object;
|
||||
| +++
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0308, E0596, E0597.
|
||||
For more information about an error, try `rustc --explain E0308`.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue