MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

[SIM909] Avoid reflexive assignment #114

Closed rpdelaney closed 2 years ago

rpdelaney commented 2 years ago

Explanation

Assigning a name to itself is a no-op and should be eliminated.

Example

# Bad
foo = foo

# Good
# (nothing)

# Bad
foo = foo = 42

# Good
foo = 42
MartinThoma commented 2 years ago

Thank you for the idea! This will be called SIM909 for the trial period. Once it's through the trial period, it will be called SIM124.

MartinThoma commented 2 years ago

Example 1: foo = foo

$ python -m astpretty --no-show-offsets /dev/stdin <<< `cat example.py`
Module(
    body=[
        Assign(
            targets=[Name(id='foo', ctx=Store())],
            value=Name(id='foo', ctx=Load()),
            type_comment=None,
        ),
    ],
    type_ignores=[],
)
MartinThoma commented 2 years ago

Example 2: foo = foo = 42

$ python -m astpretty --no-show-offsets /dev/stdin <<< `cat example.py`
Module(
    body=[
        Assign(
            targets=[
                Name(id='foo', ctx=Store()),
                Name(id='foo', ctx=Store()),
            ],
            value=Constant(value=42, kind=None),
            type_comment=None,
        ),
    ],
    type_ignores=[],
)