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

fix undefined-loop-variable with break and continue statements in else blocks #7318

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7311.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix `undefined-loop-variable` with `break` and `continue` statements in `else` blocks.

Refs #7311
4 changes: 3 additions & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,9 @@ def _loopvar_name(self, node: astroid.Name) -> None:
self.add_message("undefined-loop-variable", args=node.name, node=node)
return
if any(
isinstance(else_stmt, (nodes.Return, nodes.Raise))
isinstance(
else_stmt, (nodes.Return, nodes.Raise, nodes.Break, nodes.Continue)
)
for else_stmt in assign.orelse
):
return
Expand Down
4 changes: 3 additions & 1 deletion script/check_newsfragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def check_file(file: Path, verbose: bool) -> bool:

The standard format is:

<one or more line of text>
<one or more line of text ending with a '.'>
<one blank line>
<issue reference> #<issuenumber>

Where <issue reference> can be one of: {', '.join(VALID_ISSUES_KEYWORDS)}

The regex used is '{VALID_CHANGELOG_COMPILED_PATTERN}'.

For example:

``pylint.x.y`` is now a private API.
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/u/undefined/undefined_loop_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ def for_else_raises(iterable):
print(thing)


def for_else_break(iterable):
while True:
for thing in iterable:
break
else:
break
print(thing)


def for_else_continue(iterable):
while True:
for thing in iterable:
break
else:
continue
print(thing)


lst = []
lst2 = [1, 2, 3]

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/u/undefined/undefined_loop_variable.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
undefined-loop-variable:6:11:6:14:do_stuff:Using possibly undefined loop variable 'var':UNDEFINED
undefined-loop-variable:25:7:25:11::Using possibly undefined loop variable 'var1':UNDEFINED
undefined-loop-variable:75:11:75:14:do_stuff_with_redefined_range:Using possibly undefined loop variable 'var':UNDEFINED
undefined-loop-variable:163:11:163:20:find_even_number:Using possibly undefined loop variable 'something':UNDEFINED
undefined-loop-variable:181:11:181:20:find_even_number:Using possibly undefined loop variable 'something':UNDEFINED