rust-lang / rust-lang/rust

Detect when `for` loop was written when a `while let` was meant

Open
#136,893 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Current output:

error[E0382]: borrow of moved value: `iter`
    --> compiler/rustc_resolve/src/diagnostics.rs:1797:50
     |
1786 |             let mut iter = struct_expr.fields.iter().peekable();
     |                 -------- move occurs because `iter` has type `Peekable<std::slice::Iter<'_, rustc_ast::ExprField>>`, which does not implement the `Copy` trait
1787 |             let mut prev: Option<Span> = None;
1788 |             for field in iter {
     |                          ---- `iter` moved due to this implicit call to `.into_iter()`
...
1797 |                                     match (prev, iter.peek()) {
     |                                                  ^^^^ value borrowed here after move
     |
note: `std::iter::IntoIterator::into_iter` takes ownership of the receiver `self`, which moves `iter`
    --> /home/gh-estebank/rust/library/core/src/iter/traits/collect.rs:313:18
     |
313  |     fn into_iter(self) -> Self::IntoIter;
     |                  ^^^^

It should suggest while let Some(field) = iter { on line 1788.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at compiler/rustc_resolve/src/diagnostics.rs:1788, where the for field in iter loop produces the shown moved-value diagnostic. Trace the surrounding diagnostic handling and verify that this case suggests while let Some(field) = iter {. Done means the compiler emits that suggestion for the reported pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.