From 5fee33ffcd52a9a078922a3ab881b0d165c32bfa Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 13 May 2022 09:40:25 -0400 Subject: [PATCH] Fix a crash in `unnecessary-list-index-lookup` when incorrectly using `enumerate()` (#6604) --- ChangeLog | 5 +++++ doc/whatsnew/2.13.rst | 4 ++++ pylint/checkers/refactoring/refactoring_checker.py | 1 + .../u/unnecessary/unnecessary_list_index_lookup.py | 4 ++++ 4 files changed, 14 insertions(+) diff --git a/ChangeLog b/ChangeLog index e1d48fd2cc..72ac6c80e6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -344,6 +344,11 @@ Release date: TBA Closes #6557 +* Fix a crash in ``unnecessary-list-index-lookup`` when incorrectly passing no + arguments to ``enumerate()``. + + Closes #6603 + * Fix a crash when accessing ``__code__`` and assigning it to a variable. Closes #6539 diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index 080aa42345..fd5ce73f6f 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -644,6 +644,10 @@ Other Changes Closes #6414 +* Fix a crash in ``unnecessary-list-index-lookup`` when incorrectly passing no + arguments to ``enumerate()``. + + Closes #6603 * Fix false positives for ``no-name-in-module`` and ``import-error`` for ``numpy.distutils`` and ``pydantic``. diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 586564f8c1..e01eaf3b08 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1996,6 +1996,7 @@ def _check_unnecessary_list_index_lookup( not isinstance(node.iter, nodes.Call) or not isinstance(node.iter.func, nodes.Name) or not node.iter.func.name == "enumerate" + or not node.iter.args or not isinstance(node.iter.args[0], nodes.Name) ): return diff --git a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py index abdd64d70a..ba99d3b9ba 100644 --- a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py +++ b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py @@ -49,3 +49,7 @@ def process_list_again(data): pairs = [(0, 0)] for i, (a, b) in enumerate(pairs): print(pairs[i][0]) + +# Regression test for https://github.com/PyCQA/pylint/issues/6603 +for i, num in enumerate(): # raises TypeError, but shouldn't crash pylint + pass