Skip to content

Suggest mutable method when iterating over binding - #160001

Open
estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-49839
Open

Suggest mutable method when iterating over binding#160001
estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-49839

Conversation

@estebank

@estebank estebank commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

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();
   |                         ++++

Fix #49839.

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();
   |                         ++++
```
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 27, 2026
@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

r? @mati865

rustbot has assigned @mati865.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: borrowck, compiler
  • borrowck, compiler expanded to 74 candidates
  • Random selection from 18 candidates

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()

@estebank estebank Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

@mati865

mati865 commented Jul 28, 2026

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned adwinwhite and unassigned mati865 Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error message about immutable binding is not actually caused by a binding

4 participants