Skip to content

Commit e41e112

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

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// We don't suggest change `&` to `&mut`
2+
// instead we suggest using .get_mut() instead of &mut, see issue #143732
3+
4+
// Custom type that implements Index<usize> but not IndexMut
5+
struct ReadOnlyVec<T> {
6+
data: Vec<T>,
7+
}
8+
9+
impl<T> ReadOnlyVec<T> {
10+
fn new(data: Vec<T>) -> Self {
11+
ReadOnlyVec { data }
12+
}
13+
}
14+
15+
impl<T> std::ops::Index<usize> for ReadOnlyVec<T> {
16+
type Output = T;
17+
18+
fn index(&self, index: usize) -> &Self::Output {
19+
&self.data[index]
20+
}
21+
}
22+
23+
fn main() {
24+
let mut map = std::collections::BTreeMap::new();
25+
map.insert(0, "string".to_owned());
26+
27+
let string = &map[&0];
28+
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596]
29+
30+
let mut map = std::collections::HashMap::new();
31+
map.insert(0, "string".to_owned());
32+
33+
let string = &map[&0];
34+
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596]
35+
36+
let mut vec = vec![String::new(), String::new()];
37+
let string = &vec[0];
38+
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596]
39+
40+
// Example with our custom ReadOnlyVec type
41+
let read_only_vec = ReadOnlyVec::new(vec![String::new(), String::new()]);
42+
let string_ref = &read_only_vec[0];
43+
string_ref.push_str("test"); //~ ERROR cannot borrow `*string_ref` as mutable, as it is behind a `&` reference [E0596]
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
2+
--> $DIR/suggest-use-as-mut-for-map.rs:28: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:34: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:38: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[E0596]: cannot borrow `*string_ref` as mutable, as it is behind a `&` reference
35+
--> $DIR/suggest-use-as-mut-for-map.rs:43:5
36+
|
37+
LL | string_ref.push_str("test");
38+
| ^^^^^^^^^^ `string_ref` is a `&` reference, so the data it refers to cannot be borrowed as mutable
39+
|
40+
help: consider changing this to be a mutable reference
41+
|
42+
LL | let string_ref = &mut read_only_vec[0];
43+
| +++
44+
45+
error: aborting due to 4 previous errors
46+
47+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)