-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Suggest use .get_mut
instead of &mut
when overloaded index type not impl IndexMut
#144018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// We don't suggest change `&` to `&mut` | ||
// instead we suggest using .get_mut() instead of &mut, see issue #143732 | ||
|
||
// Custom type that implements Index<usize> but not IndexMut | ||
struct ReadOnlyVec<T> { | ||
data: Vec<T>, | ||
} | ||
|
||
impl<T> ReadOnlyVec<T> { | ||
fn new(data: Vec<T>) -> Self { | ||
ReadOnlyVec { data } | ||
} | ||
} | ||
|
||
impl<T> std::ops::Index<usize> for ReadOnlyVec<T> { | ||
type Output = T; | ||
|
||
fn index(&self, index: usize) -> &Self::Output { | ||
&self.data[index] | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut map = std::collections::BTreeMap::new(); | ||
map.insert(0, "string".to_owned()); | ||
|
||
let string = &map[&0]; | ||
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596] | ||
|
||
let mut map = std::collections::HashMap::new(); | ||
map.insert(0, "string".to_owned()); | ||
|
||
let string = &map[&0]; | ||
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596] | ||
|
||
let mut vec = vec![String::new(), String::new()]; | ||
let string = &vec[0]; | ||
string.push_str("test"); //~ ERROR cannot borrow `*string` as mutable, as it is behind a `&` reference [E0596] | ||
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I add a case that type impls |
||
|
||
// Example with our custom ReadOnlyVec type | ||
let read_only_vec = ReadOnlyVec::new(vec![String::new(), String::new()]); | ||
let string_ref = &read_only_vec[0]; | ||
string_ref.push_str("test"); //~ ERROR cannot borrow `*string_ref` as mutable, as it is behind a `&` reference [E0596] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference | ||
--> $DIR/suggest-use-as-mut-for-map.rs:28:5 | ||
| | ||
LL | string.push_str("test"); | ||
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | ||
help: consider changing this to be a mutable reference | ||
| | ||
LL - let string = &map[&0]; | ||
LL + let string = map.get_mut(&0).unwrap(); | ||
| | ||
|
||
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference | ||
--> $DIR/suggest-use-as-mut-for-map.rs:34:5 | ||
| | ||
LL | string.push_str("test"); | ||
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | ||
help: consider changing this to be a mutable reference | ||
| | ||
LL - let string = &map[&0]; | ||
LL + let string = map.get_mut(&0).unwrap(); | ||
| | ||
|
||
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference | ||
--> $DIR/suggest-use-as-mut-for-map.rs:38:5 | ||
| | ||
LL | string.push_str("test"); | ||
| ^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | ||
help: consider changing this to be a mutable reference | ||
| | ||
LL | let string = &mut vec[0]; | ||
| +++ | ||
|
||
error[E0596]: cannot borrow `*string_ref` as mutable, as it is behind a `&` reference | ||
--> $DIR/suggest-use-as-mut-for-map.rs:43:5 | ||
| | ||
LL | string_ref.push_str("test"); | ||
| ^^^^^^^^^^ `string_ref` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
|
||
Comment on lines
+36
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a testcase by defining a type that impl |
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0596`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use
type_implements_trait
to judge if the type implIndexMut
.