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

(🎁) new rule: unconditional-break/unconditional-continue within loop #6587

Open
KotlinIsland opened this issue Aug 15, 2023 · 2 comments
Open
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule

Comments

@KotlinIsland
Copy link
Contributor

for a in [1, 2, 3]:
    print(a)
    break  # error: unconditional-break

The break here is completely invalid and is most certainly a developer mistake.

@KotlinIsland KotlinIsland changed the title (🎁) new rule: unconditional-break within for loop (🎁) new rule: unconditional-break within loop Aug 15, 2023
@KotlinIsland KotlinIsland changed the title (🎁) new rule: unconditional-break within loop (🎁) new rule: unconditional-break/unconditional-continue within loop Aug 15, 2023
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Aug 16, 2023
@charliermarsh
Copy link
Member

It makes sense, though my concern is that the rule may have low precision. It's probably not a very common error, and when it does appear in code, I can see situations in which it may be intentional. For example:

for a in some_list():
  use_the_first_element(a)
  break

I guess you could do this by slicing, or with next, but I'm worried that we'll find other such patterns that won't outweigh the usefulness.

@dosisod
Copy link
Contributor

dosisod commented Aug 17, 2023

For reference this is covered by FURB133 in Refurb (see #1348). FURB133 only covers continue though, whereas this would cover break as well. I'd agree that break changes the control flow (and may be intentional), whereas continue is harmless to remove.

I would say that a single continue at the end of a short for loop is not very useful to detect. But, if there is an if/else statement at the end of a for loop, the continue might go unnoticed. The test data for FURB133 gives some examples of continue statements that may go unnoticed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants