rust-lang / rust-lang/rust-clippy

Calling into_iter on an oversized array only triggers into_iter_on_ref

Open
#4,492 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When using into_iter on an array within the size limits of the current impl IntoIterator for &[T; _] implementations you get a deny-by-default lint failure:

error: this .into_iter() call is equivalent to .iter() and will not move the array
 --> src/main.rs:2:22
  |
2 |     for _ in [0; 32].into_iter() {
  |                      ^^^^^^^^^ help: call directly: `iter`
  |
  = note: `#[deny(clippy::into_iter_on_array)]` on by default

If you increase the array size past those provided implementations you instead get a warn-by-default lint:

warning: this .into_iter() call is equivalent to .iter() and will not move the slice
 --> src/main.rs:2:22
  |
2 |     for _ in [0; 33].into_iter() {
  |                      ^^^^^^^^^ help: call directly: `iter`
  |
  = note: `#[warn(clippy::into_iter_on_ref)]` on by default

It seems both of these cases would be affected equally by a future const-generic powered impl<T, const N: usize> IntoIterator for [T; N].

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

Reproduce the two linked Rust Playground examples and compare the diagnostics for array sizes within and beyond the current IntoIterator implementations. Trace the Clippy lint handling for into_iter_on_array and into_iter_on_ref; done means oversized arrays receive behavior consistent with the future const-generic IntoIterator implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.