Do not suggest ampmut if rhs is already mutable

This commit is contained in:
Deadbeef 2021-05-30 10:54:50 +08:00
parent ce0d64e03e
commit 0eda5098bd
No known key found for this signature in database
GPG key ID: C86982F2EDE0C128
3 changed files with 26 additions and 2 deletions

View file

@ -902,9 +902,13 @@ fn suggest_ampmut<'tcx>(
{
let lt_name = &src[1..ws_pos];
let ty = &src[ws_pos..];
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
if !ty.trim_start().starts_with("mut") {
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
}
} else if let Some(stripped) = src.strip_prefix('&') {
return (assignment_rhs_span, format!("&mut {}", stripped));
if !stripped.trim_start().starts_with("mut") {
return (assignment_rhs_span, format!("&mut {}", stripped));
}
}
}
}

View file

@ -0,0 +1,8 @@
fn main() {
let mut test = Vec::new();
let rofl: &Vec<Vec<i32>> = &mut test;
//~^ HELP consider changing this to be a mutable reference
rofl.push(Vec::new());
//~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference
//~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
}

View file

@ -0,0 +1,12 @@
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
--> $DIR/issue-85765.rs:5:5
|
LL | let rofl: &Vec<Vec<i32>> = &mut test;
| ---- help: consider changing this to be a mutable reference: `&mut Vec<Vec<i32>>`
LL |
LL | rofl.push(Vec::new());
| ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0596`.