# Buggy code
def visit_for(self, node):
    """Emit a convention whenever range and len are used for indexing."""
    # Verify that we have a `range([start], len(...), [stop])` call and
    # that the object which is iterated is used as a subscript in the
    # body of the for.

    # Is it a proper range call?
    if not isinstance(node.iter, astroid.Call):
        return
    if not self._is_builtin(node.iter.func, "range"):
        return
    is_constant_zero = (
        isinstance(node.iter.args[0], astroid.Const) and node.iter.args[0].value == 0
    )
    if len(node.iter.args) == 2 and not is_constant_zero:
        return
    if len(node.iter.args) > 2:
        return

    # Is it a proper len call?
    if not isinstance(node.iter.args[-1], astroid.Call):
        return
    second_func = node.iter.args[-1].func
    if not self._is_builtin(second_func, "len"):
        return
    len_args = node.iter.args[-1].args
    if not len_args or len(len_args) != 1:
        return
    iterating_object = len_args[0]
    if not isinstance(iterating_object, astroid.Name):
        return
    # If we're defining __iter__ on self, enumerate won't work
    scope = node.scope()
    if iterating_object.name == "self" and scope.name == "__iter__":
        return

    # Verify that the body of the for loop uses a subscript
    # with the object that was iterated. This uses some heuristics
    # in order to make sure that the same object is used in the
    # for body.
    for child in node.body:
        for subscript in child.nodes_of_class(astroid.Subscript):
            if not isinstance(subscript.value, astroid.Name):
                continue

            value = subscript.slice
            if isinstance(value, astroid.Index):
                value = value.value
            if not isinstance(value, astroid.Name):
                continue
            if value.name != node.target.name:
                continue
            if iterating_object.name != subscript.value.name:
                continue
            if subscript.value.scope() != node.scope():
                # Ignore this subscript if it's not in the same
                # scope. This means that in the body of the for
                # loop, another scope was created, where the same
                # name for the iterating object was used.
                continue
            self.add_message("consider-using-enumerate", node=node)
            return


# Traceback (IndexError)
************* Module alectryon.test
test:1:0: C0114: Missing module docstring (missing-module-docstring)
Traceback (most recent call last):
File "/home/clement/.local/bin/pylint", line 8, in <module>
sys.exit(run_pylint())
File "/home/clement/.local/lib/python3.8/site-packages/pylint/__init__.py", line 22, in run_pylint
PylintRun(sys.argv[1:])
File "/home/clement/.local/lib/python3.8/site-packages/pylint/lint/run.py", line 344, in __init__
linter.check(args)
File "/home/clement/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 864, in check
self._check_files(
File "/home/clement/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 904, in _check_files
self._check_file(get_ast, check_astroid_module, name, filepath, modname)
File "/home/clement/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 930, in _check_file
check_astroid_module(ast_node)
File "/home/clement/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1062, in check_astroid_module
retval = self._check_astroid_module(
File "/home/clement/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1107, in _check_astroid_module
walker.walk(ast_node)
File "/home/clement/.local/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 75, in walk
self.walk(child)
File "/home/clement/.local/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 72, in walk
callback(astroid)
File "/home/clement/.local/lib/python3.8/site-packages/pylint/checkers/refactoring.py", line 1346, in visit_for
if not isinstance(node.iter.args[-1], astroid.Call):
IndexError: list index out of range

# Fixed code
def visit_for(self, node):
    """Emit a convention whenever range and len are used for indexing."""
    # Verify that we have a `range([start], len(...), [stop])` call and
    # that the object which is iterated is used as a subscript in the
    # body of the for.

    # Is it a proper range call?
    if not isinstance(node.iter, astroid.Call):
        return
    if not self._is_builtin(node.iter.func, "range"):
        return
    if not node.iter.args:
        return
    is_constant_zero = (
        isinstance(node.iter.args[0], astroid.Const) and node.iter.args[0].value == 0
    )
    if len(node.iter.args) == 2 and not is_constant_zero:
        return
    if len(node.iter.args) > 2:
        return

    # Is it a proper len call?
    if not isinstance(node.iter.args[-1], astroid.Call):
        return
    second_func = node.iter.args[-1].func
    if not self._is_builtin(second_func, "len"):
        return
    len_args = node.iter.args[-1].args
    if not len_args or len(len_args) != 1:
        return
    iterating_object = len_args[0]
    if not isinstance(iterating_object, astroid.Name):
        return
    # If we're defining __iter__ on self, enumerate won't work
    scope = node.scope()
    if iterating_object.name == "self" and scope.name == "__iter__":
        return

    # Verify that the body of the for loop uses a subscript
    # with the object that was iterated. This uses some heuristics
    # in order to make sure that the same object is used in the
    # for body.
    for child in node.body:
        for subscript in child.nodes_of_class(astroid.Subscript):
            if not isinstance(subscript.value, astroid.Name):
                continue

            value = subscript.slice
            if isinstance(value, astroid.Index):
                value = value.value
            if not isinstance(value, astroid.Name):
                continue
            if value.name != node.target.name:
                continue
            if iterating_object.name != subscript.value.name:
                continue
            if subscript.value.scope() != node.scope():
                # Ignore this subscript if it's not in the same
                # scope. This means that in the body of the for
                # loop, another scope was created, where the same
                # name for the iterating object was used.
                continue
            self.add_message("consider-using-enumerate", node=node)
            return