MartinThoma / flake8-simplify

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

[New Rule] Don't check list length is > 0 before iterating over it #160

Open rpdelaney opened 2 years ago

rpdelaney commented 2 years ago

Explanation

Looking at one's own old, bad code can be a source of great cringe.

Example

# Bad
incidents = get_ticket_ids(cell.value)
if incidents is not None and len(incidents) > 0:
    for incident in incidents:
        print(incident)

# Good
incidents = get_ticket_ids(cell.value)
if incidents:
    for incident in incidents:
        print(incident)

# Good
if incidents := get_ticket_ids(cell.value):
    for incident in incidents:
        print(incident)
rpdelaney commented 2 years ago

Prior art: