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

Handle while True loops without break statements #378

Merged

Conversation

kreathon
Copy link
Contributor

Description

In the following example, the print statement is not recognized as unreachable code by vulture.

while True:
    raise Exception()
print("Currently not recognized!")

More general: If a while True statement has no break, then the statement after the while True is unreachable.

Implementation

Introduce self._current_loop_has_break_statement in order to keep track of break statements found while traversing the AST.

We cannot validate if the break statements only occur in loops (so we need to assume the code is correct). In order to check this properly, we would need to visit the parent first (and push some kind of context or scope; pop if after visiting the children).

Checklist

  • I have updated the documentation in the README.md file or my changes don't require an update.
  • I have added an entry in CHANGELOG.md.
  • I have added or adapted tests to cover my changes.
  • I have run pre-commit run --all-files to format and lint my code.

@jendrikseipp jendrikseipp merged commit 2d147f5 into jendrikseipp:main Dec 12, 2024
19 checks passed
@jendrikseipp
Copy link
Owner

The code is flawless. Thanks for the PR!

Joko013 pushed a commit to Joko013/vulture that referenced this pull request Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants