MartinThoma / flake8-simplify

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

[Adjust Rule] SIM113 Use enumerate only when it's unconditionally executed #54

Closed tsx closed 3 years ago

tsx commented 3 years ago

Desired change

Explanation

If you pre-filter the things you iterate on before adding 1 to the total, enumerate will have a different number than current total.

Example

This is an example where the mentioned rule(s) would currently be suboptimal:

odd_x = 0
for x in [1, 2, 3]:
    if x % 2:
        continue
    odd_x += 1
akx commented 3 years ago

I'd say this is a bug in the rule since if one blindly just applies the change, they'll get a program that behaves differently than before.

MartinThoma commented 3 years ago

You are completely right, this is a false-positive. SIM113 triggers way too often.

However, this issue is a duplicate of https://github.com/MartinThoma/flake8-simplify/issues/32 - sadly, I can't see a simple way to fix this issue. Maybe if there is any continue it should not trigger?