rust-lang / rust-lang/rust-clippy

Prefer `.iter()` to implicit `IntoIterator` impl of `&(mut) {Collection}`

Open
#8,585 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Essentially, the opposite of explicit_iter_loop

What it does

This lint would forbid cases where the IntoIterator impl for a reference to a collection is used, and recommend using the .iter() or .iter_mut() associated function instead.

Lint Name

implicit_iter_loop

Category

restriction

Advantage
  • Readability
  • More explicit
  • Easier to reason about
  • More refactor friendly .iter() -> .iter_mut() or .iter().enumerate(), etc
  • Works consistently on owned values and references at any indirection level:
let v = vec![1u8];
let r = &v;
let rr = &&v;

for x in v.iter() {}
for x in r.iter() {}
for x in rr.iter() {}

for x in v {} // not a reference iterator
for x in rr {} // error: `&&Vec<u8>` is not an iterator
Drawbacks
  • More verbose
  • Disagreement on whether this is idiomatic
Example
let v = vec![0u8];
for ref_to_elem in &v { }

can be rewritten to

let v = vec![0u8];
for ref_to_elem in v.iter() {}

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

The issue names no source file, test, or entry point. Start by locating the existing explicit_iter_loop lint and its tests, then determine how the proposed opposite lint should recognize reference-based iteration. Done means the implicit_iter_loop restriction is implemented, documented, and covered by tests for the examples described here.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.