Skip to content

Commit aa50ab3

Browse files
committed
Add test suggest-use-as-mut-for-map.rs
Signed-off-by: xizheyin <[email protected]>
1 parent 7e310f4 commit aa50ab3

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// We don't suggest change `&` to `&mut`
2+
// instead we suggest using .get_mut() instead of &mut, see issue #143732
3+
fn main() {
4+
let mut map = std::collections::BTreeMap::new();
5+
map.insert(0, "string".to_owned());
6+
7+
let string = &map[&0];
8+
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596]
9+
10+
let mut map = std::collections::HashMap::new();
11+
map.insert(0, "string".to_owned());
12+
13+
let string = &map[&0];
14+
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596]
15+
16+
let mut vec = vec![String::new(), String::new()];
17+
let string = &vec[0];
18+
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596]
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
2+
--> $DIR/suggest-use-as-mut-for-map.rs:8:5
3+
|
4+
LL | string.push_str("test");
5+
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
6+
|
7+
help: consider changing this to be a mutable reference
8+
|
9+
LL | let string = &mut map[&0];
10+
| +++
11+
12+
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
13+
--> $DIR/suggest-use-as-mut-for-map.rs:14:5
14+
|
15+
LL | string.push_str("test");
16+
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
17+
|
18+
help: consider changing this to be a mutable reference
19+
|
20+
LL | let string = &mut map[&0];
21+
| +++
22+
23+
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
24+
--> $DIR/suggest-use-as-mut-for-map.rs:18:5
25+
|
26+
LL | string.push_str("test");
27+
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
28+
|
29+
help: consider changing this to be a mutable reference
30+
|
31+
LL | let string = &mut vec[0];
32+
| +++
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)