-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 - enforce that loop variables are not used outside the loop #13742
Comments
you would get an error in bpr |
This comment was marked as outdated.
This comment was marked as outdated.
DetachHead
changed the title
new rule - enforce that loop variables are deleted at the end of the loop
new rule - enforce that loop variables are not used outside the loop
Oct 14, 2024
could this also apply to if statements with walrus: if match := re.search(...):
print(match)
print(match) # error, don't do that |
This would be Pylint's https://pylint.readthedocs.io/en/stable/user_guide/messages/warning/undefined-loop-variable.html |
oh right, closing in favor of #970 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the fact that loop variables are still in scope outside of the loop is a very common source of bugs:
i know there are cases where this is intentional, for example:
but imo it's a bad idea to rely on this functionality. what if
values
was empty?value
will be undefined which will cause a runtime error.in most other languages, loop variables are only in scope within the loop, which is much less error-prone. it would be nice if there was a rule that enforced this:
The text was updated successfully, but these errors were encountered: