diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index 4e9bbc5397..71e692fb60 100644 --- a/doc/whatsnew/2/2.14/full.rst +++ b/doc/whatsnew/2/2.14/full.rst @@ -5,6 +5,8 @@ What's New in Pylint 2.14.2? ---------------------------- Release date: TBA +* Avoided raising an identical ``undefined-loop-variable`` message twice on the same line. + * Don't crash if ``lint.run._query_cpu()`` is run within a Kubernetes Pod, that has only a fraction of a cpu core assigned. Just go with one process then. diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 839558523b..7eeb1e854e 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1499,7 +1499,6 @@ def _check_consumer( node, nodes.ComprehensionScope ): self._check_late_binding_closure(node) - self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) diff --git a/tests/functional/u/undefined/undefined_loop_variable.py b/tests/functional/u/undefined/undefined_loop_variable.py index fc8640f7a0..a4249a39ff 100644 --- a/tests/functional/u/undefined/undefined_loop_variable.py +++ b/tests/functional/u/undefined/undefined_loop_variable.py @@ -146,3 +146,11 @@ def use_enumerate(): for i, num in enumerate(range(3)): pass print(i, num) + + +def find_even_number(container): + """https://github.com/PyCQA/pylint/pull/6923#discussion_r895134495""" + for something in container: + if something % 2 == 0: + break + return something # [undefined-loop-variable] diff --git a/tests/functional/u/undefined/undefined_loop_variable.txt b/tests/functional/u/undefined/undefined_loop_variable.txt index 9157e4195c..ef9031c576 100644 --- a/tests/functional/u/undefined/undefined_loop_variable.txt +++ b/tests/functional/u/undefined/undefined_loop_variable.txt @@ -1,3 +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:156:11:156:20:find_even_number:Using possibly undefined loop variable 'something':UNDEFINED