Skip to content

Suggest mutable method when iterating over binding - #160001

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
estebank:issue-49839
Aug 5, 2026
Merged

Suggest mutable method when iterating over binding#160001
rust-bors[bot] merged 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That would be better but more involved. Could be a nice followup.

@mati865

mati865 commented Jul 28, 2026

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned adwinwhite and unassigned mati865 Jul 28, 2026
@adwinwhite

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 9fb185d has been approved by adwinwhite

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 4, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 4, 2026
Suggest mutable method when iterating over binding

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 rust-lang#49839.
rust-bors Bot pushed a commit that referenced this pull request Aug 5, 2026
…uwer

Rollup of 22 pull requests

Successful merges:

 - #160426 (`rust-analyzer` subtree update)
 - #160372 (Derive the allocator used by tools from rustc's allocator)
 - #146882 (fully deprecate the legacy integral modules)
 - #158727 (std: use `readdir` on nearly all UNIX platforms)
 - #159727 (Various steps in moving away from the big reflection enum to reflection functions)
 - #160443 (normalize in relations, not generalize, when relating infer with alias)
 - #160457 (implement -Zllvm-target-feature)
 - #160480 (Single-pass ASCII lower/upper case conversion)
 - #160502 (Reduce number of miri tests executed on PR CI)
 - #157430 (std::random: use little-endian for reproducibility)
 - #158110 (fix macro attribute feature-gate span)
 - #159975 (Use real ThinVec in StmtDebugInfos)
 - #160001 (Suggest mutable method when iterating over binding)
 - #160024 (Fix mono reachability with no-op landing pads)
 - #160154 (Add regression test for HRTB associated type projection closure)
 - #160176 (No more `tests/ui/issues`!)
 - #160326 (Remove hidden_glob_reexports)
 - #160407 (Add regression tests for a number of ICEs and diagnostics issues labelled `E-needs-test`)
 - #160430 (bootstrap: Don't produce mutated/filtered PathSets during command-line matching)
 - #160472 (Minor fixes to `core::io` & `alloc::io` Documentation)
 - #160486 (Remove unused `FreeRegionsVisitor`)
 - #160496 (clarify non-determinism docs for algebraic operations)

Failed merges:

 - #160501 (Add bootstrap CLI snapshot test for testing miri)
@rust-bors
rust-bors Bot merged commit 02a459b into rust-lang:main Aug 5, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Aug 5, 2026
rust-timer added a commit that referenced this pull request Aug 5, 2026
Rollup merge of #160001 - estebank:issue-49839, r=adwinwhite

Suggest mutable method when iterating over binding

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.
RalfJung pushed a commit to rust-lang/miri that referenced this pull request Aug 5, 2026
…uwer

Rollup of 22 pull requests

Successful merges:

 - rust-lang/rust#160426 (`rust-analyzer` subtree update)
 - rust-lang/rust#160372 (Derive the allocator used by tools from rustc's allocator)
 - rust-lang/rust#146882 (fully deprecate the legacy integral modules)
 - rust-lang/rust#158727 (std: use `readdir` on nearly all UNIX platforms)
 - rust-lang/rust#159727 (Various steps in moving away from the big reflection enum to reflection functions)
 - rust-lang/rust#160443 (normalize in relations, not generalize, when relating infer with alias)
 - rust-lang/rust#160457 (implement -Zllvm-target-feature)
 - rust-lang/rust#160480 (Single-pass ASCII lower/upper case conversion)
 - rust-lang/rust#160502 (Reduce number of miri tests executed on PR CI)
 - rust-lang/rust#157430 (std::random: use little-endian for reproducibility)
 - rust-lang/rust#158110 (fix macro attribute feature-gate span)
 - rust-lang/rust#159975 (Use real ThinVec in StmtDebugInfos)
 - rust-lang/rust#160001 (Suggest mutable method when iterating over binding)
 - rust-lang/rust#160024 (Fix mono reachability with no-op landing pads)
 - rust-lang/rust#160154 (Add regression test for HRTB associated type projection closure)
 - rust-lang/rust#160176 (No more `tests/ui/issues`!)
 - rust-lang/rust#160326 (Remove hidden_glob_reexports)
 - rust-lang/rust#160407 (Add regression tests for a number of ICEs and diagnostics issues labelled `E-needs-test`)
 - rust-lang/rust#160430 (bootstrap: Don't produce mutated/filtered PathSets during command-line matching)
 - rust-lang/rust#160472 (Minor fixes to `core::io` & `alloc::io` Documentation)
 - rust-lang/rust#160486 (Remove unused `FreeRegionsVisitor`)
 - rust-lang/rust#160496 (clarify non-determinism docs for algebraic operations)

Failed merges:

 - rust-lang/rust#160501 (Add bootstrap CLI snapshot test for testing miri)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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