Skip to content
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

lint redundant continue in loop #9552

Open
matthiaskrgr opened this issue Sep 28, 2022 · 3 comments
Open

lint redundant continue in loop #9552

matthiaskrgr opened this issue Sep 28, 2022 · 3 comments
Labels
A-lint Area: New lints

Comments

@matthiaskrgr
Copy link
Member

What it does

suggest replacing if cond { continue } <code> inside a loop with just if !cond { <code> }

Lint Name

?

Category

suspicious, complexity

Advantage

simplify code

Drawbacks

No response

Example

pub fn test(v: Vec<bool>) {
    for i in v {
        if !i {
            continue;
        }
        println!("{}", i);
    }
}

Could be written as:

pub fn test(v: Vec<bool>) {
    for i in v {
        if i {
              println!("{}", i);
            }
       }
}
@matthiaskrgr matthiaskrgr added the A-lint Area: New lints label Sep 28, 2022
@nyurik
Copy link
Contributor

nyurik commented Sep 28, 2022

I am not sure this is a good lint - in many cases I do this to reduce the level of nesting. Also, having early breaks/continues makes the code easier to follow.

@matthiaskrgr
Copy link
Member Author

Right the situation is different if you have a lot of code afterwards, but if the loop-body is very short, having the one statement inside the if or outside of it makes no difference in regards of the amount of nesting imo. ^^

@kraktus
Copy link
Contributor

kraktus commented Sep 29, 2022

I feel this would be the kind of lint easily ignored by users since it's hard to nail down what's a "big" loop (even if we make the number of line a user setting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants