Skip to content

Provide auto-fix suggestion for for..of loop #75311

Closed
@clarfonthey

Description

@clarfonthey

Been working in JavaScript a lot lately and find myself accidentally typing this a lot. It would be nice to automatically detect this and offer a proper fix. For those who don't know, in JavaScript, for..of uses the iterator API, whereas for..in iterates over the keys in an object.

Playground example below.

fn main() {
    for x of &[1, 2, 3] {
        println!("{}", x);
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error: missing `in` in `for` loop
 --> src/main.rs:2:10
  |
2 |     for x of &[1, 2, 3] {
  |          ^ help: try adding `in` here

error[E0425]: cannot find value `of` in this scope
 --> src/main.rs:2:11
  |
2 |     for x of &[1, 2, 3] {
  |           ^^ not found in this scope

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions