Skip to content

explicit_counter_loop false negative when loop counter starts at non‑zero #16612

@xtqqczze

Description

@xtqqczze

Summary

The lint currently fails to trigger when a manually maintained loop counter does not start at 0.

Lint Name

explicit_counter_loop

Reproducer

I tried this code:

pub fn m1(v: Vec<u8>) {
    let mut i = 1;
    for item in &v {
        black_box((i, *item));
        i += 1;
    }
}

https://rust.godbolt.org/z/oG1h3f1nY

I expected to see this happen:

warning: the variable `i` is used as a loop counter
 --> <source>:5:5
  |
5 |     for item in &v {
  |     ^^^^^^^^^^^^^^ help: consider using: `for (i, item) in (1..).zip(v.iter())`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop
  = note: `#[warn(clippy::explicit_counter_loop)]` on by default

warning: 1 warning emitted

Instead, this happened:

Lint was not triggered.

Version

nightly

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions