tonybaloney / perflint

Python Linter for performance anti patterns
MIT License
663 stars 10 forks source link

loop-invariant-statement triggered by dict creation in `else` branch of `for-else` #38

Open jakkanen opened 1 year ago

jakkanen commented 1 year ago

The following incorrectly results in perflint_issue.py:6:15: W8201: Consider moving this expression outside of the loop. (loop-invariant-statement)

def findfirstkey(items, key):
    for item in items:
        if item["key"] == key:
            return item
    else:
        return {"key": key}

Interestingly, the diagnostic is printed twice for each key-value pair if the dict is created with {"key": key, "key2": None} and once for each key-value if it's created with dict(key=key, key2=None)