-
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?
Conversation
You need to compare the defid with the defid of the HashMap/BTreeMap diagnostic item and hard code the special case |
.get_mut
instead of &mut
when occur HashMap
and BtreeMap
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.
The second commit shows the testcase difference.
In your first version you checked if the assignment on the type came from an overloaded index. You should check whether the overloaded index type implements @rustbot author |
Reminder, once the PR becomes ready for a review, use |
8d3b265
to
c43a91f
Compare
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.
@rustbot ready
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] |
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 add a case that type impls IndexMut
, i.e. Vec
.
.type_implements_trait( | ||
index_mut_trait, | ||
[ty, index_ty], | ||
self.infcx.param_env, | ||
) | ||
.must_apply_modulo_regions() |
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 impl IndexMut
.
This comment has been minimized.
This comment has been minimized.
.get_mut
instead of &mut
when occur HashMap
and BtreeMap
.get_mut
instead of &mut
when overloaded index type not impl IndexMut
@rustbot author |
This comment asks me to check the type which use overloaded index but not implement |
because there are other types that aren't |
@Kobzol suggest to consider all the types in this category? What do you think? If we only consider a special case, we don't have to check whether it implements IndexMut. Because these two types didnt implement IndexMut, it seems to be enough to judge directly by using defid. |
My suggestion was:
|
This is clear. I forgot that some types do not have get_mut. |
Signed-off-by: xizheyin <[email protected]>
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.
@rustbot ready
This time, I considered different scenarios:
- suggest to use &mut
- suggest to use .get_mut
- dont suggest
I also added a new testcase.
…or BTreeMap Signed-off-by: xizheyin <[email protected]>
I spilt the testcase into two. One is marked |
if let Some(mir::Statement { | ||
source_info: _, | ||
kind: mir::StatementKind::Assign(box (_, mir::Rvalue::Ref(_, _, place))), | ||
.. | ||
}) = self.body[location.block].statements.get(location.statement_index) |
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.
this part is duplicated from the let opt_assignment_rhs_span
part above - you should find a way to deduplicate
&self, | ||
local: Local, | ||
opt_assignment_rhs_span: Option<Span>, | ||
) -> (bool, Option<AmpMutSugg>) { |
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 don't like this too much, try ControlFlow<(), Option<AmpMutSugg>>
Fixes #143732
r? compiler