Skip to content

explicit_counter_loop false positive with continue statement #1219

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
mbrt opened this issue Sep 8, 2016 · 5 comments
Closed

explicit_counter_loop false positive with continue statement #1219

mbrt opened this issue Sep 8, 2016 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good first issue These issues are a good way to get started with Clippy

Comments

@mbrt
Copy link

mbrt commented Sep 8, 2016

Clippy shows a false positive in this example, asking for an explicit_counter_loop refactor:

    let text = "banana";
    let mut count = 0;
    for ch in text.chars() {
        if ch == 'a' {
            continue;
        }
        count += 1
    }
    println!("{}", count);

But since the continue statement skips the counter, the proposed for (count, ch) in text.char_indices() is not equivalent.

@mcarton mcarton added C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Sep 8, 2016
@mcarton
Copy link
Member

mcarton commented Sep 8, 2016

We could suggest to use filter when the first statement in a for loop is if f(var) { continue }.

@mbrt
Copy link
Author

mbrt commented Sep 9, 2016

Actually I think that you can simply suppress the suggestion if a continue
statement is present in the loop body. There could be dependencies (in term
of side effects) among the counter and continue and break statements. It's
not easy to detect when they are effectively equivalent.

@Reylak
Copy link

Reylak commented Mar 16, 2018

I agree with @mbrt, I think this lint in most often wrong whenever the counter update statement can be unreachable because of a continue.

@oli-obk oli-obk added the good first issue These issues are a good way to get started with Clippy label Apr 24, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Apr 24, 2018

Yea let's just bail on continue statements

@JoshMcguigan
Copy link
Contributor

@oli-obk I've added a test case based on the post by @mbrt and submitted PR #3135, but I'm not seeing this example activate a lint, so the test case is passing without any code changes.

Just let me know if I am missing something here.

JoshMcguigan added a commit to JoshMcguigan/rust-clippy that referenced this issue Sep 8, 2018
flip1995 added a commit that referenced this issue Sep 9, 2018
Closes #1219 false positive for explicit_counter_loop
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 C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good first issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

5 participants