Skip to content

IndexError: list index out of range in unnecessary-list-index-lookup checker #10627

@correctmost

Description

@correctmost

Bug description

This test case from ruff's unnecessary-list-index-lookup checker crashes Pylint:

for index, letter in enumerate(letters):
    del index
    print(letters[index])

Configuration

Command used

pylint crash.py

Pylint output

Traceback (most recent call last):
  File "pylint/pylint/utils/ast_walker.py", line 87, in walk
    callback(astroid)
    ~~~~~~~~^^^^^^^^^
  File "pylint/pylint/checkers/refactoring/refactoring_checker.py", line 763, in visit_for
    self._check_unnecessary_list_index_lookup(node)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "pylint/pylint/checkers/refactoring/refactoring_checker.py", line 2361, in _check_unnecessary_list_index_lookup
    and index.lookup(index.name)[1][-1].lineno > node.lineno
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

Expected behavior

No crash

Pylint version

pylint: 5f0298d
astroid: pylint-dev/astroid@2ce47a2cc046
Python 3.13.7

OS / Environment

Arch Linux

Additional dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    Crash 💥A bug that makes pylint crash

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions