Better span for "make binding mutable" suggestion

This commit is contained in:
Esteban Küber 2024-07-03 21:01:29 +00:00
parent 140392b041
commit 89ecae5d85
25 changed files with 134 additions and 101 deletions

View File

@ -3757,13 +3757,11 @@ pub(crate) fn report_illegal_reassignment(
assigned_span: Span,
err_place: Place<'tcx>,
) {
let (from_arg, local_decl, local_name) = match err_place.as_local() {
Some(local) => (
self.body.local_kind(local) == LocalKind::Arg,
Some(&self.body.local_decls[local]),
self.local_names[local],
),
None => (false, None, None),
let (from_arg, local_decl) = match err_place.as_local() {
Some(local) => {
(self.body.local_kind(local) == LocalKind::Arg, Some(&self.body.local_decls[local]))
}
None => (false, None),
};
// If root local is initialized immediately (everything apart from let
@ -3795,13 +3793,12 @@ pub(crate) fn report_illegal_reassignment(
err.span_label(assigned_span, format!("first assignment to {place_description}"));
}
if let Some(decl) = local_decl
&& let Some(name) = local_name
&& decl.can_be_made_mutable()
{
err.span_suggestion(
decl.source_info.span,
err.span_suggestion_verbose(
decl.source_info.span.shrink_to_lo(),
"consider making this binding mutable",
format!("mut {name}"),
"mut ".to_string(),
Applicability::MachineApplicable,
);
if !from_arg
@ -3813,10 +3810,10 @@ pub(crate) fn report_illegal_reassignment(
}))
)
{
err.span_suggestion(
decl.source_info.span,
err.span_suggestion_verbose(
decl.source_info.span.shrink_to_lo(),
"to modify the original value, take a borrow instead",
format!("ref mut {name}"),
"ref mut ".to_string(),
Applicability::MaybeIncorrect,
);
}

View File

@ -1,7 +1,7 @@
fn test() {
let v: isize;
//~^ HELP consider making this binding mutable
//~| SUGGESTION mut v
//~| SUGGESTION mut
v = 1; //~ NOTE first assignment
println!("v={}", v);
v = 2; //~ ERROR cannot assign twice to immutable variable

View File

@ -1,14 +1,16 @@
error[E0384]: cannot assign twice to immutable variable `v`
--> $DIR/assign-imm-local-twice.rs:7:5
|
LL | let v: isize;
| - help: consider making this binding mutable: `mut v`
...
LL | v = 1;
| ----- first assignment to `v`
LL | println!("v={}", v);
LL | v = 2;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut v: isize;
| +++
error: aborting due to 1 previous error

View File

@ -13,12 +13,14 @@ error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/issue-61452.rs:9:5
|
LL | pub async fn g(x: usize) {
| -
| |
| first assignment to `x`
| help: consider making this binding mutable: `mut x`
| - first assignment to `x`
LL | x += 1;
| ^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | pub async fn g(mut x: usize) {
| +++
error: aborting due to 2 previous errors

View File

@ -9,11 +9,11 @@ LL | x += 1;
help: consider making this binding mutable
|
LL | mut x => {
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | ref mut x => {
| ~~~~~~~~~
| +++++++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/borrowck-match-binding-is-assignment.rs:20:13
@ -26,11 +26,11 @@ LL | x += 1;
help: consider making this binding mutable
|
LL | E::Foo(mut x) => {
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | E::Foo(ref mut x) => {
| ~~~~~~~~~
| +++++++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/borrowck-match-binding-is-assignment.rs:26:13
@ -43,11 +43,11 @@ LL | x += 1;
help: consider making this binding mutable
|
LL | S { bar: mut x } => {
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | S { bar: ref mut x } => {
| ~~~~~~~~~
| +++++++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/borrowck-match-binding-is-assignment.rs:32:13
@ -60,11 +60,11 @@ LL | x += 1;
help: consider making this binding mutable
|
LL | (mut x,) => {
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | (ref mut x,) => {
| ~~~~~~~~~
| +++++++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/borrowck-match-binding-is-assignment.rs:38:13
@ -77,11 +77,11 @@ LL | x += 1;
help: consider making this binding mutable
|
LL | [mut x,_,_] => {
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | [ref mut x,_,_] => {
| ~~~~~~~~~
| +++++++
error: aborting due to 5 previous errors

View File

@ -1,10 +1,13 @@
error[E0384]: cannot assign to immutable argument `_x`
--> $DIR/immutable-arg.rs:2:5
|
LL | fn foo(_x: u32) {
| -- help: consider making this binding mutable: `mut _x`
LL | _x = 4;
| ^^^^^^ cannot assign to immutable argument
|
help: consider making this binding mutable
|
LL | fn foo(mut _x: u32) {
| +++
error: aborting due to 1 previous error

View File

@ -1,7 +1,7 @@
fn test_drop_replace() {
let b: Box<isize>;
//~^ HELP consider making this binding mutable
//~| SUGGESTION mut b
//~| SUGGESTION mut
b = Box::new(1); //~ NOTE first assignment
b = Box::new(2); //~ ERROR cannot assign twice to immutable variable `b`
//~| NOTE cannot assign twice to immutable
@ -10,13 +10,13 @@ fn test_drop_replace() {
fn test_call() {
let b = Box::new(1); //~ NOTE first assignment
//~| HELP consider making this binding mutable
//~| SUGGESTION mut b
//~| SUGGESTION mut
b = Box::new(2); //~ ERROR cannot assign twice to immutable variable `b`
//~| NOTE cannot assign twice to immutable
}
fn test_args(b: Box<i32>) { //~ HELP consider making this binding mutable
//~| SUGGESTION mut b
//~| SUGGESTION mut
b = Box::new(2); //~ ERROR cannot assign to immutable argument `b`
//~| NOTE cannot assign to immutable argument
}

View File

@ -1,34 +1,40 @@
error[E0384]: cannot assign twice to immutable variable `b`
--> $DIR/issue-45199.rs:6:5
|
LL | let b: Box<isize>;
| - help: consider making this binding mutable: `mut b`
...
LL | b = Box::new(1);
| - first assignment to `b`
LL | b = Box::new(2);
| ^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut b: Box<isize>;
| +++
error[E0384]: cannot assign twice to immutable variable `b`
--> $DIR/issue-45199.rs:14:5
|
LL | let b = Box::new(1);
| -
| |
| first assignment to `b`
| help: consider making this binding mutable: `mut b`
| - first assignment to `b`
...
LL | b = Box::new(2);
| ^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut b = Box::new(1);
| +++
error[E0384]: cannot assign to immutable argument `b`
--> $DIR/issue-45199.rs:20:5
|
LL | fn test_args(b: Box<i32>) {
| - help: consider making this binding mutable: `mut b`
LL |
LL | b = Box::new(2);
| ^ cannot assign to immutable argument
|
help: consider making this binding mutable
|
LL | fn test_args(mut b: Box<i32>) {
| +++
error: aborting due to 3 previous errors

View File

@ -9,11 +9,11 @@ LL | x = 2;
help: consider making this binding mutable
|
LL | if let Some(mut x) = y {
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | if let Some(ref mut x) = y {
| ~~~~~~~~~
| +++++++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/suggest-ref-mut-issue-118596.rs:9:5
@ -26,11 +26,11 @@ LL | x = 0;
help: consider making this binding mutable
|
LL | let [mut x, ref xs_hold @ ..] = arr;
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | let [ref mut x, ref xs_hold @ ..] = arr;
| ~~~~~~~~~
| +++++++
error: aborting due to 2 previous errors

View File

@ -2,12 +2,14 @@ error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/tainted-promoteds.rs:7:5
|
LL | let a = 0;
| -
| |
| first assignment to `a`
| help: consider making this binding mutable: `mut a`
| - first assignment to `a`
LL | a = &0 * &1 * &2 * &3;
| ^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut a = 0;
| +++
error: aborting due to 1 previous error

View File

@ -2,12 +2,14 @@ error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/command-line-diagnostics.rs:6:5
|
LL | let x = 42;
| -
| |
| first assignment to `x`
| help: consider making this binding mutable: `mut x`
| - first assignment to `x`
LL | x = 43;
| ^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut x = 42;
| +++
error: aborting due to 1 previous error

View File

@ -16,10 +16,13 @@ LL | fn foo<'a>(mut x: Ref<'a, 'a>, y: &'a u32) {
error[E0384]: cannot assign to immutable argument `y`
--> $DIR/ex3-both-anon-regions-one-is-struct-2.rs:4:5
|
LL | fn foo(mut x: Ref, y: &u32) {
| - help: consider making this binding mutable: `mut y`
LL | y = x.b;
| ^^^^^^^ cannot assign to immutable argument
|
help: consider making this binding mutable
|
LL | fn foo(mut x: Ref, mut y: &u32) {
| +++
error: aborting due to 2 previous errors

View File

@ -1,45 +1,53 @@
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/liveness-assign-imm-local-notes.rs:10:9
|
LL | let x;
| - help: consider making this binding mutable: `mut x`
...
LL | x = 2;
| ----- first assignment to `x`
LL | x = 3;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut x;
| +++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/liveness-assign-imm-local-notes.rs:21:13
|
LL | let x;
| - help: consider making this binding mutable: `mut x`
...
LL | x = 2;
| ----- first assignment to `x`
LL | x = 3;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut x;
| +++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/liveness-assign-imm-local-notes.rs:30:13
|
LL | let x;
| - help: consider making this binding mutable: `mut x`
...
LL | x = 1;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut x;
| +++
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/liveness-assign-imm-local-notes.rs:32:13
|
LL | let x;
| - help: consider making this binding mutable: `mut x`
...
LL | x = 1;
| ----- first assignment to `x`
LL | } else {
LL | x = 2;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut x;
| +++
error: aborting due to 4 previous errors

View File

@ -1,7 +1,7 @@
fn test() {
let v: isize;
//~^ HELP consider making this binding mutable
//~| SUGGESTION mut v
//~| SUGGESTION mut
loop {
v = 1; //~ ERROR cannot assign twice to immutable variable `v`
//~| NOTE cannot assign twice to immutable variable

View File

@ -1,11 +1,13 @@
error[E0384]: cannot assign twice to immutable variable `v`
--> $DIR/liveness-assign-imm-local-in-loop.rs:6:9
|
LL | let v: isize;
| - help: consider making this binding mutable: `mut v`
...
LL | v = 1;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut v: isize;
| +++
error: aborting due to 1 previous error

View File

@ -1,7 +1,7 @@
fn test() {
let v: isize;
//~^ HELP consider making this binding mutable
//~| SUGGESTION mut v
//~| SUGGESTION mut
v = 2; //~ NOTE first assignment
v += 1; //~ ERROR cannot assign twice to immutable variable `v`
//~| NOTE cannot assign twice to immutable

View File

@ -1,13 +1,15 @@
error[E0384]: cannot assign twice to immutable variable `v`
--> $DIR/liveness-assign-imm-local-in-op-eq.rs:6:5
|
LL | let v: isize;
| - help: consider making this binding mutable: `mut v`
...
LL | v = 2;
| ----- first assignment to `v`
LL | v += 1;
| ^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut v: isize;
| +++
error: aborting due to 1 previous error

View File

@ -1,7 +1,7 @@
fn test() {
let b = Box::new(1); //~ NOTE first assignment
//~| HELP consider making this binding mutable
//~| SUGGESTION mut b
//~| SUGGESTION mut
drop(b);
b = Box::new(2); //~ ERROR cannot assign twice to immutable variable `b`
//~| NOTE cannot assign twice to immutable

View File

@ -2,13 +2,15 @@ error[E0384]: cannot assign twice to immutable variable `b`
--> $DIR/liveness-assign-imm-local-with-drop.rs:6:5
|
LL | let b = Box::new(1);
| -
| |
| first assignment to `b`
| help: consider making this binding mutable: `mut b`
| - first assignment to `b`
...
LL | b = Box::new(2);
| ^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut b = Box::new(1);
| +++
error: aborting due to 1 previous error

View File

@ -1,7 +1,7 @@
fn test() {
let v: isize = 1; //~ NOTE first assignment
//~| HELP consider making this binding mutable
//~| SUGGESTION mut v
//~| SUGGESTION mut
v.clone();
v = 2; //~ ERROR cannot assign twice to immutable variable `v`
//~| NOTE cannot assign twice to immutable

View File

@ -2,13 +2,15 @@ error[E0384]: cannot assign twice to immutable variable `v`
--> $DIR/liveness-assign-imm-local-with-init.rs:6:5
|
LL | let v: isize = 1;
| -
| |
| first assignment to `v`
| help: consider making this binding mutable: `mut v`
| - first assignment to `v`
...
LL | v = 2;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | let mut v: isize = 1;
| +++
error: aborting due to 1 previous error

View File

@ -9,11 +9,11 @@ LL | x += 1;
help: consider making this binding mutable
|
LL | let &mut mut x = foo;
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | let &mut ref mut x = foo;
| ~~~~~~~~~
| +++++++
error[E0506]: cannot assign to `*foo` because it is borrowed
--> $DIR/mut-pattern-internal-mutability.rs:13:5

View File

@ -78,11 +78,11 @@ LL | | Err(a @ b @ a)
help: consider making this binding mutable
|
LL | Ok(a @ b @ mut a)
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | Ok(a @ b @ ref mut a)
| ~~~~~~~~~
| +++++++
error: aborting due to 12 previous errors

View File

@ -22,11 +22,11 @@ LL | _x1 = U;
help: consider making this binding mutable
|
LL | let [ref _x0_hold, mut _x1, ref xs_hold @ ..] = arr;
| ~~~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | let [ref _x0_hold, ref mut _x1, ref xs_hold @ ..] = arr;
| ~~~~~~~~~~~
| +++++++
error[E0505]: cannot move out of `arr[..]` because it is borrowed
--> $DIR/borrowck-move-ref-pattern.rs:11:10
@ -86,11 +86,11 @@ LL | _x1 = U;
help: consider making this binding mutable
|
LL | let (ref _x0, mut _x1, ref _x2, ..) = tup;
| ~~~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | let (ref _x0, ref mut _x1, ref _x2, ..) = tup;
| ~~~~~~~~~~~
| +++++++
error[E0502]: cannot borrow `tup.0` as mutable because it is also borrowed as immutable
--> $DIR/borrowck-move-ref-pattern.rs:24:20

View File

@ -9,11 +9,11 @@ LL | a = 42;
help: consider making this binding mutable
|
LL | let Foo(mut a) = Foo(0);
| ~~~~~
| +++
help: to modify the original value, take a borrow instead
|
LL | let Foo(ref mut a) = Foo(0);
| ~~~~~~~~~
| +++++++
error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:15:5