Suggest mutable method when iterating over binding - #160001
Open
estebank wants to merge 1 commit into
Open
Conversation
When encountering a for loop that requires a mutable iterator item, we previously suggested changing the method on the iterated expression, but ignored bindings. We now go to the binding's definition and suggest changing the method there too.
```
error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
--> $DIR/suggest-mut-method-for-loop-hashmap.rs:31:9
|
LL | for (_k, v) in x {
| - this iterator yields `&` references
...
LL | v.v += 1;
| ^^^^^^^^ `v` is a `&` reference, so it cannot be written to
|
help: use mutable method
|
LL | let mut x = map.iter_mut();
| ++++
```
Collaborator
|
r? @mati865 rustbot has assigned @mati865. Use Why was this reviewer chosen?The reviewer was selected based on:
|
estebank
commented
Jul 27, 2026
Comment on lines
+1112
to
+1123
| assoc_items | ||
| .in_definition_order() | ||
| .map(|assoc_item_def| assoc_item_def.ident(tcx)) | ||
| .filter(|&ident| { | ||
| let original_method_ident = path_segment.ident; | ||
| original_method_ident != ident | ||
| && ident | ||
| .as_str() | ||
| .starts_with(&original_method_ident.name.to_string()) | ||
| }) | ||
| .map(|ident| format!("{ident}()")) | ||
| .peekable() |
Contributor
Author
There was a problem hiding this comment.
I would want us to make this logic more principled and actually check that the associated function will give back an appropriate iterator of mutable items, even if the name is different.
Member
|
@rustbot reroll |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When encountering a for loop that requires a mutable iterator item, we previously suggested changing the method on the iterated expression, but ignored bindings. We now go to the binding's definition and suggest changing the method there too.
Fix #49839.