Skip to content

Commit

Permalink
Fix SIM222 and SIM223 false positives and auto-fix (#4063)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed Apr 25, 2023
1 parent 37483f3 commit 1f3b0fd
Show file tree
Hide file tree
Showing 5 changed files with 2,200 additions and 77 deletions.
110 changes: 110 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_simplify/SIM222.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,113 @@ def validate(self, value):

if a and False and f() and b and g(): # OK
pass


a or "" or True # SIM222

a or "foo" or True or "bar" # SIM222

a or 0 or True # SIM222

a or 1 or True or 2 # SIM222

a or 0.0 or True # SIM222

a or 0.1 or True or 0.2 # SIM222

a or [] or True # SIM222

a or list([]) or True # SIM222

a or [1] or True or [2] # SIM222

a or list([1]) or True or list([2]) # SIM222

a or {} or True # SIM222

a or dict() or True # SIM222

a or {1: 1} or True or {2: 2} # SIM222

a or dict({1: 1}) or True or dict({2: 2}) # SIM222

a or set() or True # SIM222

a or set(set()) or True # SIM222

a or {1} or True or {2} # SIM222

a or set({1}) or True or set({2}) # SIM222

a or () or True # SIM222

a or tuple(()) or True # SIM222

a or (1,) or True or (2,) # SIM222

a or tuple((1,)) or True or tuple((2,)) # SIM222

a or frozenset() or True # SIM222

a or frozenset(frozenset()) or True # SIM222

a or frozenset({1}) or True or frozenset({2}) # SIM222

a or frozenset(frozenset({1})) or True or frozenset(frozenset({2})) # SIM222


# Inside test `a` is simplified.

bool(a or [1] or True or [2]) # SIM222

assert a or [1] or True or [2] # SIM222

if (a or [1] or True or [2]) and (a or [1] or True or [2]): # SIM222
pass

0 if a or [1] or True or [2] else 1 # SIM222

while a or [1] or True or [2]: # SIM222
pass

[
0
for a in range(10)
for b in range(10)
if a or [1] or True or [2] # SIM222
if b or [1] or True or [2] # SIM222
]

{
0
for a in range(10)
for b in range(10)
if a or [1] or True or [2] # SIM222
if b or [1] or True or [2] # SIM222
}

{
0: 0
for a in range(10)
for b in range(10)
if a or [1] or True or [2] # SIM222
if b or [1] or True or [2] # SIM222
}

(
0
for a in range(10)
for b in range(10)
if a or [1] or True or [2] # SIM222
if b or [1] or True or [2] # SIM222
)

# Outside test `a` is not simplified.

a or [1] or True or [2] # SIM222

if (a or [1] or True or [2]) == (a or [1]): # SIM222
pass

if f(a or [1] or True or [2]): # SIM222
pass
110 changes: 110 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_simplify/SIM223.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,113 @@

if a or True or f() or b or g(): # OK
pass


a and "" and False # SIM223

a and "foo" and False and "bar" # SIM223

a and 0 and False # SIM223

a and 1 and False and 2 # SIM223

a and 0.0 and False # SIM223

a and 0.1 and False and 0.2 # SIM223

a and [] and False # SIM223

a and list([]) and False # SIM223

a and [1] and False and [2] # SIM223

a and list([1]) and False and list([2]) # SIM223

a and {} and False # SIM223

a and dict() and False # SIM223

a and {1: 1} and False and {2: 2} # SIM223

a and dict({1: 1}) and False and dict({2: 2}) # SIM223

a and set() and False # SIM223

a and set(set()) and False # SIM223

a and {1} and False and {2} # SIM223

a and set({1}) and False and set({2}) # SIM223

a and () and False # SIM222

a and tuple(()) and False # SIM222

a and (1,) and False and (2,) # SIM222

a and tuple((1,)) and False and tuple((2,)) # SIM222

a and frozenset() and False # SIM222

a and frozenset(frozenset()) and False # SIM222

a and frozenset({1}) and False and frozenset({2}) # SIM222

a and frozenset(frozenset({1})) and False and frozenset(frozenset({2})) # SIM222


# Inside test `a` is simplified.

bool(a and [] and False and []) # SIM223

assert a and [] and False and [] # SIM223

if (a and [] and False and []) or (a and [] and False and []): # SIM223
pass

0 if a and [] and False and [] else 1 # SIM222

while a and [] and False and []: # SIM223
pass

[
0
for a in range(10)
for b in range(10)
if a and [] and False and [] # SIM223
if b and [] and False and [] # SIM223
]

{
0
for a in range(10)
for b in range(10)
if a and [] and False and [] # SIM223
if b and [] and False and [] # SIM223
}

{
0: 0
for a in range(10)
for b in range(10)
if a and [] and False and [] # SIM223
if b and [] and False and [] # SIM223
}

(
0
for a in range(10)
for b in range(10)
if a and [] and False and [] # SIM223
if b and [] and False and [] # SIM223
)

# Outside test `a` is not simplified.

a and [] and False and [] # SIM223

if (a and [] and False and []) == (a and []): # SIM223
pass

if f(a and [] and False and []): # SIM223
pass
Loading

0 comments on commit 1f3b0fd

Please sign in to comment.