Uh oh!
There was an error while loading. Please reload this page.
Suggest use .get_mut instead of &mut when overloaded index type not impl IndexMut - #144018
Suggest use .get_mut instead of &mut when overloaded index type not impl IndexMut#144018xizheyin wants to merge 2 commits into
.get_mut instead of &mut when overloaded index type not impl IndexMut#144018Conversation
fee1-dead
commented
Jul 16, 2025
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
xizheyin
left a comment
There was a problem hiding this comment.
The second commit shows the testcase difference.
Uh oh!
There was an error while loading. Please reload this page.
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 |
rustbot
commented
Jul 17, 2025
Reminder, once the PR becomes ready for a review, use |
8d3b265 to
c43a91fCompare| 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.
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.
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 IndexMutfee1-dead
commented
Jul 21, 2025
@rustbot author |
This comment asks me to check the type which use overloaded index but not implement |
fee1-dead
commented
Jul 21, 2025
because there are other types that aren't |
xizheyin
commented
Jul 21, 2025
@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. |
Kobzol
commented
Jul 21, 2025
My suggestion was:
|
xizheyin
commented
Jul 21, 2025
This is clear. I forgot that some types do not have get_mut. |
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
xizheyin
commented
Jul 24, 2025
I spilt the testcase into two. One is marked |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…or BTreeMap Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
| // If there is a assignment, we should return the rhs | ||
| rhs = Some(rvalue); | ||
| if let mir::Rvalue::Use(mir::Operand::Copy(place)) = rvalue { | ||
| span = self.body.local_decls[place.local].source_info.span; | ||
| } |
There was a problem hiding this comment.
To dedup if let StatementKind..., I return the RValue if it is a Assign and pass it into suggest_get_mut_or_ref_mut.
| // We only suggest use `.get_mut()` for HashMap or BTreeMap | ||
| // We don't try suggesting use `&mut`, so break | ||
| ControlFlow::Break(Some(AmpMutSugg { | ||
| has_sugg: true, | ||
| span: rhs_span, | ||
| suggestion: format!("{}.get_mut({}).unwrap()", map_part, key_part), | ||
| additional: None, | ||
| })) | ||
| } else { | ||
| // Type not implemented `IndexMut` but not HashMap or BTreeMap or error | ||
| // and we did not suggest either `.get_mut()` or `&mut`, so break | ||
| ControlFlow::Break(None) | ||
| } | ||
| } else { | ||
| // Type Impl IndexMut, we should continue to try suggesting use `&mut` | ||
| ControlFlow::Continue(()) |
There was a problem hiding this comment.
In these three situations, we return different values. If we don't want to try suggesting &mut, it return Break. Or we return Continue, which indicates continue to try suggesting &mut.
…m, r=nnethercote overhaul `&mut` suggestions in borrowck errors * This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR. * If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`. * Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`. Supersedes rust-lang#144018 cc `@xizheyin` Closesrust-lang#143732
…m, r=nnethercote overhaul `&mut` suggestions in borrowck errors * This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR. * If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`. * Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`. Supersedes rust-lang#144018 cc ``@xizheyin`` Closesrust-lang#143732
…m, r=nnethercote overhaul `&mut` suggestions in borrowck errors * This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR. * If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`. * Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`. Supersedes rust-lang#144018 cc ```@xizheyin``` Closesrust-lang#143732
Rollup merge of #145013 - fee1-dead-contrib:push-vwvsqsqnrxqm, r=nnethercote overhaul `&mut` suggestions in borrowck errors * This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR. * If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`. * Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`. Supersedes #144018 cc ```@xizheyin``` Closes#143732
…hercote overhaul `&mut` suggestions in borrowck errors * This refactors the logic so that it does not use fuzzy string matching for suggestions; it instead uses information directly from MIR. * If something comes from a custom `Index` impl for which the `IndexMut` trait does not apply, do not suggest adding `mut` after `&`. * Suggest `get_mut` with `unwrap` if error is fired on `BTreeMap` or `HashMap`. Supersedes rust-lang/rust#144018 cc ```@xizheyin``` Closesrust-lang/rust#143732
Fixes#143732
r? compiler