Skip to content

explicit_counter_loop emitted incorrectly when using guard clauses with continue. #10058

Closed
@mumblingdrunkard

Description

@mumblingdrunkard

Summary

Just came across this. Not a huge issue, but kind of annoying.
I'll often use continue to build guard clauses in my for loops and it seems clippy doesn't realise that this makes everything below it conditional.

Clippy version: clippy 0.1.65 (897e375 2022-11-02)

Lint Name

explicit_counter_loop

Reproducer

This is the simplest code I could make to reproduce it.

let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1];
let mut counter = 0;
for value in values {
    counter += 1;

    if value == 0 {
        continue;
    }

    counter += 1;
}

It does not happen for this code:

let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1];
let mut counter = 0;
for value in values {
    counter += 1;

    if value != 0 {
        counter += 1;
    }
}

Version

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions