More accurate mutability suggestion

This commit is contained in:
Esteban Küber 2024-07-03 23:26:10 +00:00
parent 2699d8108c
commit ff92ab0903
19 changed files with 244 additions and 140 deletions

View File

@ -408,10 +408,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
fn_decl.implicit_self,
hir::ImplicitSelfKind::RefImm | hir::ImplicitSelfKind::RefMut
) {
err.span_suggestion(
upvar_ident.span,
err.span_suggestion_verbose(
upvar_ident.span.shrink_to_lo(),
"consider changing this to be mutable",
format!("mut {}", upvar_ident.name),
"mut ",
Applicability::MachineApplicable,
);
break;
@ -419,10 +419,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
}
}
} else {
err.span_suggestion(
upvar_ident.span,
err.span_suggestion_verbose(
upvar_ident.span.shrink_to_lo(),
"consider changing this to be mutable",
format!("mut {}", upvar_ident.name),
"mut ",
Applicability::MachineApplicable,
);
}
@ -755,13 +755,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
..
}) = node
&& let Ok(name) =
self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span)
{
err.span_suggestion(
pat_span,
err.multipart_suggestion(
"consider changing this to be mutable",
format!("&(mut {name})"),
vec![
(pat_span.until(local_decl.source_info.span), "&(mut ".to_string()),
(
local_decl.source_info.span.shrink_to_hi().with_hi(pat_span.hi()),
")".to_string(),
),
],
Applicability::MachineApplicable,
);
return;

View File

@ -12,11 +12,13 @@ LL | let mut x = 0;
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/borrow-raw-address-of-mutability.rs:11:17
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
LL | let mut f = || {
LL | let y = &raw mut x;
| ^^^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
--> $DIR/borrow-raw-address-of-mutability.rs:21:5

View File

@ -43,10 +43,13 @@ LL | c1;
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/borrowck-closures-unique.rs:43:38
|
LL | fn e(x: &'static mut isize) {
| - help: consider changing this to be mutable: `mut x`
LL | let c1 = |y: &'static mut isize| x = y;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn e(mut x: &'static mut isize) {
| +++
error: aborting due to 4 previous errors

View File

@ -19,10 +19,13 @@ LL | || bar(&mut self);
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-111554.rs:21:16
|
LL | pub fn quux(self) {
| ---- help: consider changing this to be mutable: `mut self`
LL | || bar(&mut self);
| ^^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | pub fn quux(mut self) {
| +++
error: aborting due to 4 previous errors

View File

@ -44,56 +44,68 @@ LL | borrowck_closures_unique::e(addr_of_mut!(X));
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:9:46
|
LL | pub fn e(x: &'static mut isize) {
| - help: consider changing this to be mutable: `mut x`
LL | static mut Y: isize = 3;
LL | let mut c1 = |y: &'static mut isize| x = y;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | pub fn e(mut x: &'static mut isize) {
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:22:50
|
LL | pub fn ee(x: &'static mut isize) {
| - help: consider changing this to be mutable: `mut x`
...
LL | let mut c2 = |y: &'static mut isize| x = y;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | pub fn ee(mut x: &'static mut isize) {
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:37:13
|
LL | pub fn capture_assign_whole(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
LL | || {
LL | x = (1,);
| ^^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | pub fn capture_assign_whole(mut x: (i32,)) {
| +++
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:43:13
|
LL | pub fn capture_assign_part(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
LL | || {
LL | x.0 = 1;
| ^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | pub fn capture_assign_part(mut x: (i32,)) {
| +++
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:49:13
|
LL | pub fn capture_reborrow_whole(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
LL | || {
LL | &mut x;
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | pub fn capture_reborrow_whole(mut x: (i32,)) {
| +++
error[E0596]: cannot borrow `x.0` as mutable, as `x` is not declared as mutable
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:55:13
|
LL | pub fn capture_reborrow_part(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
LL | || {
LL | &mut x.0;
| ^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | pub fn capture_reborrow_part(mut x: (i32,)) {
| +++
error: aborting due to 6 previous errors; 3 warnings emitted

View File

@ -262,74 +262,90 @@ LL | fn imm_local(mut x: (i32,)) {
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/mutability-errors.rs:60:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
LL | || {
LL | x = (1,);
| ^^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
--> $DIR/mutability-errors.rs:61:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x.0 = 1;
| ^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/mutability-errors.rs:62:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
...
LL | &mut x;
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0596]: cannot borrow `x.0` as mutable, as `x` is not declared as mutable
--> $DIR/mutability-errors.rs:63:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
...
LL | &mut x.0;
| ^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/mutability-errors.rs:66:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x = (1,);
| ^^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
--> $DIR/mutability-errors.rs:67:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x.0 = 1;
| ^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/mutability-errors.rs:68:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
...
LL | &mut x;
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0596]: cannot borrow `x.0` as mutable, as `x` is not declared as mutable
--> $DIR/mutability-errors.rs:69:9
|
LL | fn imm_capture(x: (i32,)) {
| - help: consider changing this to be mutable: `mut x`
...
LL | &mut x.0;
| ^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | fn imm_capture(mut x: (i32,)) {
| +++
error[E0594]: cannot assign to immutable static item `X`
--> $DIR/mutability-errors.rs:76:5

View File

@ -1,18 +1,24 @@
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/cannot-mutate-captured-non-mut-var.rs:9:25
|
LL | let x = 1;
| - help: consider changing this to be mutable: `mut x`
LL | to_fn_once(move|| { x = 2; });
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut x = 1;
| +++
error[E0596]: cannot borrow `s` as mutable, as it is not declared as mutable
--> $DIR/cannot-mutate-captured-non-mut-var.rs:13:25
|
LL | let s = std::io::stdin();
| - help: consider changing this to be mutable: `mut s`
LL | to_fn_once(move|| { s.read_to_end(&mut Vec::new()); });
| ^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut s = std::io::stdin();
| +++
error: aborting due to 2 previous errors

View File

@ -1,11 +1,13 @@
error[E0596]: cannot borrow `x[..]` as mutable, as `x` is not declared as mutable
--> $DIR/array_subslice.rs:7:21
|
LL | pub fn subslice_array(x: [u8; 3]) {
| - help: consider changing this to be mutable: `mut x`
...
LL | let [ref y, ref mut z @ ..] = x;
| ^^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | pub fn subslice_array(mut x: [u8; 3]) {
| +++
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
--> $DIR/array_subslice.rs:10:5

View File

@ -1,20 +1,24 @@
error[E0594]: cannot assign to `z.0.0.0`, as it is not declared as mutable
--> $DIR/cant-mutate-imm.rs:12:9
|
LL | let z = (y, 10);
| - help: consider changing this to be mutable: `mut z`
...
LL | z.0.0.0 = 20;
| ^^^^^^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut z = (y, 10);
| +++
error[E0594]: cannot assign to `*bx.0`, as it is not declared as mutable
--> $DIR/cant-mutate-imm.rs:24:9
|
LL | let bx = Box::new(x);
| -- help: consider changing this to be mutable: `mut bx`
...
LL | bx.0 = 20;
| ^^^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut bx = Box::new(x);
| +++
error: aborting due to 2 previous errors

View File

@ -1,10 +1,13 @@
error[E0594]: cannot assign to `y`, as it is not declared as mutable
--> $DIR/closure-immutable-outer-variable.rs:11:26
|
LL | let y = true;
| - help: consider changing this to be mutable: `mut y`
LL | foo(Box::new(move || y = !y) as Box<_>);
| ^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut y = true;
| +++
error: aborting due to 1 previous error

View File

@ -34,11 +34,13 @@ LL | fn fun() -> _ {
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/suggest-return-closure.rs:23:9
|
LL | let x = String::new();
| - help: consider changing this to be mutable: `mut x`
...
LL | x.push(c);
| ^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut x = String::new();
| +++
error[E0597]: `x` does not live long enough
--> $DIR/suggest-return-closure.rs:23:9

View File

@ -1,38 +1,46 @@
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/closure-captures.rs:7:5
|
LL | fn one_closure(x: i32) {
| - help: consider changing this to be mutable: `mut x`
LL | ||
LL | x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn one_closure(mut x: i32) {
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/closure-captures.rs:9:5
|
LL | fn one_closure(x: i32) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn one_closure(mut x: i32) {
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/closure-captures.rs:15:9
|
LL | fn two_closures(x: i32) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn two_closures(mut x: i32) {
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/closure-captures.rs:19:9
|
LL | fn two_closures(x: i32) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn two_closures(mut x: i32) {
| +++
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/closure-captures.rs:27:9
@ -67,11 +75,13 @@ LL | x = 1;});
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/closure-captures.rs:39:10
|
LL | fn two_closures_ref(x: i32) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x = 1;}
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn two_closures_ref(mut x: i32) {
| +++
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/closure-captures.rs:38:9
@ -91,11 +101,13 @@ LL | x = 1;}
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/closure-captures.rs:43:5
|
LL | fn two_closures_ref(x: i32) {
| - help: consider changing this to be mutable: `mut x`
...
LL | x = 1;});
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | fn two_closures_ref(mut x: i32) {
| +++
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/closure-captures.rs:42:9

View File

@ -1,11 +1,13 @@
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/coroutine-upvar-mutability.rs:10:9
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error: aborting due to 1 previous error

View File

@ -1,11 +1,13 @@
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/issue-46023.rs:5:9
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error: aborting due to 1 previous error

View File

@ -1,10 +1,13 @@
error[E0596]: cannot borrow `b` as mutable, as it is not declared as mutable
--> $DIR/patkind-ref-binding-issue-114896.rs:7:9
|
LL | let &b = a;
| -- help: consider changing this to be mutable: `&(mut b)`
LL | b.make_ascii_uppercase();
| ^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let &(mut b) = a;
| ~~~~~ +
error: aborting due to 1 previous error

View File

@ -1,10 +1,13 @@
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/patkind-ref-binding-issue-122415.rs:7:12
|
LL | fn foo(&x: &i32) {
| -- help: consider changing this to be mutable: `&(mut x)`
LL | mutate(&mut x);
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | fn foo(&(mut x): &i32) {
| ~~~~~ +
error: aborting due to 1 previous error

View File

@ -1,73 +1,90 @@
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:9:13
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
LL | move || x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:10:17
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
LL | move || x = 1;
LL | move || set(&mut x);
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:11:13
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | move || x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:12:17
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | move || set(&mut x);
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:13:8
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | || x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:14:12
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | || set(&mut x);
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:15:8
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | || x = 1;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closure-immutable-capture.rs:16:12
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
...
LL | || set(&mut x);
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut x = 0;
| +++
error: aborting due to 8 previous errors

View File

@ -1,13 +1,16 @@
error[E0596]: cannot borrow `tick1` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closures-infer-fnmut-calling-fnmut-no-mut.rs:16:9
|
LL | let tick1 = || {
| ----- help: consider changing this to be mutable: `mut tick1`
LL | counter += 1;
| ------- calling `tick1` requires mutable binding due to mutable borrow of `counter`
...
LL | tick1();
| ^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
LL | let mut tick1 = || {
| +++
error[E0596]: cannot borrow `tick2` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closures-infer-fnmut-calling-fnmut-no-mut.rs:19:5

View File

@ -1,29 +1,35 @@
error[E0594]: cannot assign to `n`, as it is not declared as mutable
--> $DIR/unboxed-closures-mutate-upvar.rs:15:9
|
LL | let n = 0;
| - help: consider changing this to be mutable: `mut n`
LL | let mut f = to_fn_mut(|| {
LL | n += 1;
| ^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut n = 0;
| +++
error[E0594]: cannot assign to `n`, as it is not declared as mutable
--> $DIR/unboxed-closures-mutate-upvar.rs:32:9
|
LL | let n = 0;
| - help: consider changing this to be mutable: `mut n`
...
LL | n += 1;
| ^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut n = 0;
| +++
error[E0594]: cannot assign to `n`, as it is not declared as mutable
--> $DIR/unboxed-closures-mutate-upvar.rs:46:9
|
LL | let n = 0;
| - help: consider changing this to be mutable: `mut n`
LL | let mut f = to_fn(move || {
LL | n += 1;
| ^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
LL | let mut n = 0;
| +++
error[E0594]: cannot assign to `n`, as it is a captured variable in a `Fn` closure
--> $DIR/unboxed-closures-mutate-upvar.rs:53:9