-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bd81ee
commit 3ddf6f7
Showing
11 changed files
with
229 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
fn main() { | ||
let _ = || { | ||
let mut test = Vec::new(); | ||
let rofl: &Vec<Vec<i32>> = &mut test; | ||
//~^ HELP consider changing this binding's type | ||
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 | ||
|
||
let mut mutvar = 42; | ||
let r = &mutvar; | ||
//~^ HELP consider changing this to be a mutable reference | ||
*r = 0; | ||
//~^ ERROR cannot assign to `*r`, which is behind a `&` reference | ||
//~| NOTE `r` is a `&` reference, so the data it refers to cannot be written | ||
|
||
#[rustfmt::skip] | ||
let x: &usize = &mut{0}; | ||
//~^ HELP consider changing this binding's type | ||
*x = 1; | ||
//~^ ERROR cannot assign to `*x`, which is behind a `&` reference | ||
//~| NOTE `x` is a `&` reference, so the data it refers to cannot be written | ||
|
||
#[rustfmt::skip] | ||
let y: &usize = &mut(0); | ||
//~^ HELP consider changing this binding's type | ||
*y = 1; | ||
//~^ ERROR cannot assign to `*y`, which is behind a `&` reference | ||
//~| NOTE `y` is a `&` reference, so the data it refers to cannot be written | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference | ||
--> $DIR/issue-85765-closure.rs:6:9 | ||
| | ||
LL | rofl.push(Vec::new()); | ||
| ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | ||
help: consider changing this binding's type | ||
| | ||
LL | let rofl: &mut Vec<Vec<i32>> = &mut test; | ||
| ~~~~~~~~~~~~~~~~~~ | ||
|
||
error[E0594]: cannot assign to `*r`, which is behind a `&` reference | ||
--> $DIR/issue-85765-closure.rs:13:9 | ||
| | ||
LL | *r = 0; | ||
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written | ||
| | ||
help: consider changing this to be a mutable reference | ||
| | ||
LL | let r = &mut mutvar; | ||
| +++ | ||
|
||
error[E0594]: cannot assign to `*x`, which is behind a `&` reference | ||
--> $DIR/issue-85765-closure.rs:20:9 | ||
| | ||
LL | *x = 1; | ||
| ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written | ||
| | ||
help: consider changing this binding's type | ||
| | ||
LL | let x: &mut usize = &mut{0}; | ||
| ~~~~~~~~~~ | ||
|
||
error[E0594]: cannot assign to `*y`, which is behind a `&` reference | ||
--> $DIR/issue-85765-closure.rs:27:9 | ||
| | ||
LL | *y = 1; | ||
| ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written | ||
| | ||
help: consider changing this binding's type | ||
| | ||
LL | let y: &mut usize = &mut(0); | ||
| ~~~~~~~~~~ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0594, E0596. | ||
For more information about an error, try `rustc --explain E0594`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use std::collections::BTreeMap; | ||
|
||
fn main() { | ||
let _ = || { | ||
let mut map = BTreeMap::<u32, u32>::new(); | ||
map[&0] = 1; //~ ERROR cannot assign | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0594]: cannot assign to data in an index of `BTreeMap<u32, u32>` | ||
--> $DIR/btreemap-index-mut-2.rs:6:9 | ||
| | ||
LL | map[&0] = 1; | ||
| ^^^^^^^^^^^ cannot assign | ||
| | ||
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `BTreeMap<u32, u32>` | ||
help: to modify a `BTreeMap<u32, u32>`, use `.get_mut()`, `.insert()` or the entry API | ||
| | ||
LL | map.insert(&0, 1); | ||
| ~~~~~~~~ ~ + | ||
LL | map.get_mut(&0).map(|val| { *val = 1; }); | ||
| ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ++++ | ||
LL | let val = map.entry(&0).or_insert(1); | ||
| +++++++++ ~~~~~~~ ~~~~~~~~~~~~ + | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0594`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
fn take(_x: Box<isize>) {} | ||
|
||
|
||
fn main() { | ||
let _ = || { | ||
let x: Box<isize> = Box::new(25); | ||
|
||
loop { | ||
take(x); //~ ERROR use of moved value: `x` | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error[E0382]: use of moved value: `x` | ||
--> $DIR/liveness-move-call-arg-2.rs:9:18 | ||
| | ||
LL | let x: Box<isize> = Box::new(25); | ||
| - move occurs because `x` has type `Box<isize>`, which does not implement the `Copy` trait | ||
LL | | ||
LL | loop { | ||
| ---- inside of this loop | ||
LL | take(x); | ||
| ^ value moved here, in previous iteration of loop | ||
| | ||
note: consider changing this parameter type in function `take` to borrow instead if owning the value isn't necessary | ||
--> $DIR/liveness-move-call-arg-2.rs:1:13 | ||
| | ||
LL | fn take(_x: Box<isize>) {} | ||
| ---- ^^^^^^^^^^ this parameter takes ownership of the value | ||
| | | ||
| in this function | ||
help: consider cloning the value if the performance cost is acceptable | ||
| | ||
LL | take(x.clone()); | ||
| ++++++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
19 changes: 19 additions & 0 deletions
19
tests/ui/suggestions/suggest-mut-method-for-loop-closure.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::collections::HashMap; | ||
struct X(usize); | ||
struct Y { | ||
v: u32, | ||
} | ||
|
||
fn main() { | ||
let _ = || { | ||
let mut buzz = HashMap::new(); | ||
buzz.insert("a", Y { v: 0 }); | ||
|
||
for mut t in buzz.values() { | ||
//~^ HELP | ||
//~| SUGGESTION values_mut() | ||
t.v += 1; | ||
//~^ ERROR cannot assign | ||
} | ||
}; | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/ui/suggestions/suggest-mut-method-for-loop-closure.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0594]: cannot assign to `t.v`, which is behind a `&` reference | ||
--> $DIR/suggest-mut-method-for-loop-closure.rs:15:13 | ||
| | ||
LL | for mut t in buzz.values() { | ||
| ------------- | ||
| | | | ||
| | help: use mutable method: `values_mut()` | ||
| this iterator yields `&` references | ||
... | ||
LL | t.v += 1; | ||
| ^^^^^^^^ `t` is a `&` reference, so the data it refers to cannot be written | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0594`. |