diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 0cdd37c5e45..691d83ff9f8 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2242,7 +2242,7 @@ 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 diff --git a/tests/functional/u/undefined/undefined_loop_variable.py b/tests/functional/u/undefined/undefined_loop_variable.py index c18d2275523..3e096472d9d 100644 --- a/tests/functional/u/undefined/undefined_loop_variable.py +++ b/tests/functional/u/undefined/undefined_loop_variable.py @@ -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]