Skip to content

Raise a convention message when an index is initialized outside of a for loop instead of using enumerate #8430

Open
@Pierre-Sassoulas

Description

@Pierre-Sassoulas

Current problem

The following code:

def outer_definition():
    i = 0
    all_possible_cases = [1, 2, 3, "4"]
    for case in all_possible_cases:  # [consider-using-enumerate]
        i += 1
        print(i, case)

Can use enumerate to become the following:

def outer_definition():
    all_possible_cases = [1, 2, 3, "4"]
    for i, case in enumerate(all_possible_cases, start=1):
        print(i, case)

Desired solution

Expand consider-using-enumerate for this use case, or create a new message (right now consider-using-enumerate is well defined to target combination of len/range in iterator so changing it might bring false positive to a long lasting and battle tested check).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions