Skip to content

Unneded iter for loop #14430

Open
Open
@Snowiiii

Description

@Snowiiii

Summary

Hello, While coding on my Rust project i encountered something that would definitely make sense to have as a clippy lint.

Problematic code

let array = [1, 2, 3];
for (i, _) in array.iter().enumerate() {
     // the array data is not used we only need the len 
}

Better alternative

let array = [1, 2, 3];
for i in 0..array.len() {
     // we only use the len
}

This is cleaner and also may faster since we don't have to iterate over the array anymore

Lint Name

uneeded_iter_loop

Reproducer

I tried this code:

let array = [1, 2, 3];
for (i, _) in array.iter().enumerate() {
     // the array data is not used we only need the len 
}

I expected to see this happen:
A clippy warn

Instead, this happened:
No clippy warn

Version

rustc: 1.85-stable

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions