Skip to content

Commit 0eda509

Browse files
committed
Do not suggest ampmut if rhs is already mutable
1 parent ce0d64e commit 0eda509

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,13 @@ fn suggest_ampmut<'tcx>(
902902
{
903903
let lt_name = &src[1..ws_pos];
904904
let ty = &src[ws_pos..];
905-
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
905+
if !ty.trim_start().starts_with("mut") {
906+
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
907+
}
906908
} else if let Some(stripped) = src.strip_prefix('&') {
907-
return (assignment_rhs_span, format!("&mut {}", stripped));
909+
if !stripped.trim_start().starts_with("mut") {
910+
return (assignment_rhs_span, format!("&mut {}", stripped));
911+
}
908912
}
909913
}
910914
}

src/test/ui/borrowck/issue-85765.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let mut test = Vec::new();
3+
let rofl: &Vec<Vec<i32>> = &mut test;
4+
//~^ HELP consider changing this to be a mutable reference
5+
rofl.push(Vec::new());
6+
//~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference
7+
//~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
2+
--> $DIR/issue-85765.rs:5:5
3+
|
4+
LL | let rofl: &Vec<Vec<i32>> = &mut test;
5+
| ---- help: consider changing this to be a mutable reference: `&mut Vec<Vec<i32>>`
6+
LL |
7+
LL | rofl.push(Vec::new());
8+
| ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)