MartinThoma / flake8-simplify

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

[New Rule] Merge duplicate startswith / endswith calls #169

Open Skylion007 opened 1 year ago

Skylion007 commented 1 year ago

Explanation

Explain briefly why you think this makes the code simpler. This is similar to the isinstance call merging. You can also merge startswith, endswith calls on strings. This means the builtin method only needs to iterate through the string once. flake8-PIE already enables this rule as PIE810.

Example

# Bad
a = "abcdef"
if a.startswith("abc") or a.startswith("xyz"):
     print("alphabet found")

# Good
a = "abcdef"
if a.startswith("abc", "xyz"):
     print("alphabet found")

can be repeated with endswith as well

janosh commented 1 year ago

Came here to suggest the same rule. 👍

Note that the good example is missing parens:

- if a.startswith("abc", "xyz"):
+ if a.startswith(("abc", "xyz")):