Skip to content

Commit 278e24e

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

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)