Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mumblingdrunkard opened this issue Dec 10, 2022 · 0 comments · Fixed by #10094
Closed
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@mumblingdrunkard
Copy link

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

@mumblingdrunkard mumblingdrunkard added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 10, 2022
ericwu17 added a commit to ericwu17/rust-clippy that referenced this issue Dec 16, 2022
There used to be a logical bug where IncrementVisitor would
completely stop checking an expression/block after seeing a continue
statement. This led to issues such as rust-lang#10058 where a variable incremented
(or otherwise modified) after any continue statement could still be
considered incremented only once.

The solution is to continue scanning the expression after seeing a
`continue` statement, but increment self.depth so that the Visitor
thinks that the rest of the loop is within a conditional.
ericwu17 added a commit to ericwu17/rust-clippy that referenced this issue Dec 16, 2022
There used to be a logical bug where IncrementVisitor would
completely stop checking an expression/block after seeing a continue
statement. This led to issues such as rust-lang#10058 where a variable incremented
(or otherwise modified) after any continue statement could still be
considered incremented only once.

The solution is to continue scanning the expression after seeing a
`continue` statement, but increment self.depth so that the Visitor
thinks that the rest of the loop is within a conditional.
ericwu17 added a commit to ericwu17/rust-clippy that referenced this issue Dec 16, 2022
There used to be a logical bug where IncrementVisitor would
completely stop checking an expression/block after seeing a continue
statement. This led to issues such as rust-lang#10058 where a variable incremented
(or otherwise modified) after any continue statement could still be
considered incremented only once.

The solution is to continue scanning the expression after seeing a
`continue` statement, but increment self.depth so that the Visitor
thinks that the rest of the loop is within a conditional.
ericwu17 added a commit to ericwu17/rust-clippy that referenced this issue Dec 16, 2022
There used to be a logical bug where IncrementVisitor would
completely stop checking an expression/block after seeing a continue
statement. This led to issue rust-lang#10058 where a variable incremented
(or otherwise modified) after any continue statement would still be
considered incremented only once.

The solution is to continue scanning the expression after seeing a
`continue` statement, but increment self.depth so that the Visitor
thinks that the rest of the loop is within a conditional.
@bors bors closed this as completed in b62319c Dec 17, 2022
chansuke pushed a commit to chansuke/rust-clippy that referenced this issue Dec 17, 2022
There used to be a logical bug where IncrementVisitor would
completely stop checking an expression/block after seeing a continue
statement. This led to issue rust-lang#10058 where a variable incremented
(or otherwise modified) after any continue statement would still be
considered incremented only once.

The solution is to continue scanning the expression after seeing a
`continue` statement, but increment self.depth so that the Visitor
thinks that the rest of the loop is within a conditional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant