-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simplifiable-condition to the refactoring checker
- Loading branch information
1 parent
aca29ac
commit 4c28ea0
Showing
12 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Test that boolean conditions simplify to a constant value""" | ||
# pylint: disable=pointless-statement | ||
from unknown import Unknown # pylint: disable=import-error | ||
|
||
|
||
def func(_): | ||
"""Pointless function""" | ||
|
||
|
||
CONSTANT = 100 | ||
OTHER = 200 | ||
|
||
# Simplifies any boolean expression that is coerced into a True/False value | ||
bool(CONSTANT or True) # [condition-evals-to-constant] | ||
assert CONSTANT or True # [condition-evals-to-constant] | ||
if CONSTANT and False: # [condition-evals-to-constant] | ||
pass | ||
elif CONSTANT and False: # [condition-evals-to-constant] | ||
pass | ||
while CONSTANT and False: # [condition-evals-to-constant] | ||
break | ||
1 if CONSTANT or True else 2 # [condition-evals-to-constant] | ||
z = [x for x in range(10) if x or True] # [condition-evals-to-constant] | ||
|
||
# Simplifies recursively | ||
assert True or CONSTANT or OTHER # [condition-evals-to-constant] | ||
assert (CONSTANT or True) or (CONSTANT or True) # [condition-evals-to-constant] | ||
|
||
# Will try to infer the truthiness of an expression as long as it doesn't contain any variables | ||
assert 3 + 4 or CONSTANT # [condition-evals-to-constant] | ||
assert Unknown or True # [condition-evals-to-constant] | ||
|
||
assert True or True # [condition-evals-to-constant] | ||
assert False or False # [condition-evals-to-constant] | ||
assert True and True # [condition-evals-to-constant] | ||
assert False and False # [condition-evals-to-constant] | ||
|
||
|
||
# A bare constant that's not inside of a boolean operation will emit `using-constant-test` instead | ||
if True: # pylint: disable=using-constant-test | ||
pass | ||
|
||
# Expressions not in one of the above situations will not emit a message | ||
CONSTANT or True | ||
bool(CONSTANT or OTHER) | ||
bool(func(CONSTANT or True)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
condition-evals-to-constant:14::Boolean condition 'CONSTANT or True' will always evaluate to 'True' | ||
condition-evals-to-constant:15::Boolean condition 'CONSTANT or True' will always evaluate to 'True' | ||
condition-evals-to-constant:16::Boolean condition 'CONSTANT and False' will always evaluate to 'False' | ||
condition-evals-to-constant:18::Boolean condition 'CONSTANT and False' will always evaluate to 'False' | ||
condition-evals-to-constant:20::Boolean condition 'CONSTANT and False' will always evaluate to 'False' | ||
condition-evals-to-constant:22::Boolean condition 'CONSTANT or True' will always evaluate to 'True' | ||
condition-evals-to-constant:23::Boolean condition 'x or True' will always evaluate to 'True' | ||
condition-evals-to-constant:26::Boolean condition 'True or CONSTANT or OTHER' will always evaluate to 'True' | ||
condition-evals-to-constant:27::Boolean condition 'CONSTANT or True or CONSTANT or True' will always evaluate to 'True' | ||
condition-evals-to-constant:30::Boolean condition '3 + 4 or CONSTANT' will always evaluate to '3 + 4' | ||
condition-evals-to-constant:31::Boolean condition 'Unknown or True' will always evaluate to 'True' | ||
condition-evals-to-constant:33::Boolean condition 'True or True' will always evaluate to 'True' | ||
condition-evals-to-constant:34::Boolean condition 'False or False' will always evaluate to 'False' | ||
condition-evals-to-constant:35::Boolean condition 'True and True' will always evaluate to 'True' | ||
condition-evals-to-constant:36::Boolean condition 'False and False' will always evaluate to 'False' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Test that boolean conditions can be simplified""" | ||
# pylint: disable=pointless-statement | ||
|
||
|
||
def func(_): | ||
"""Pointless function""" | ||
|
||
|
||
CONSTANT = 100 | ||
OTHER = 200 | ||
|
||
# Simplifies any boolean expression that is coerced into a True/False value | ||
bool(CONSTANT or False) # [simplifiable-condition] | ||
assert CONSTANT or False # [simplifiable-condition] | ||
if CONSTANT and True: # [simplifiable-condition] | ||
pass | ||
elif CONSTANT and True: # [simplifiable-condition] | ||
pass | ||
while CONSTANT and True: # [simplifiable-condition] | ||
break | ||
1 if CONSTANT or False else 2 # [simplifiable-condition] | ||
z = [x for x in range(10) if x or False] # [simplifiable-condition] | ||
|
||
# Simplifies recursively | ||
assert CONSTANT or (True and False) # [simplifiable-condition] | ||
assert True and CONSTANT and OTHER # [simplifiable-condition] | ||
assert (CONSTANT or False) and (OTHER or True) # [simplifiable-condition] | ||
|
||
# Will try to infer the truthiness of an expression as long as it doesn't contain any variables | ||
assert [] or CONSTANT # [simplifiable-condition] | ||
assert {} or CONSTANT # [simplifiable-condition] | ||
|
||
# Expressions not in one of the above situations will not emit a message | ||
CONSTANT or True | ||
bool(CONSTANT or OTHER) | ||
bool(func(CONSTANT or True)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
simplifiable-condition:13::Boolean condition 'CONSTANT or False' may be simplified to 'CONSTANT' | ||
simplifiable-condition:14::Boolean condition 'CONSTANT or False' may be simplified to 'CONSTANT' | ||
simplifiable-condition:15::Boolean condition 'CONSTANT and True' may be simplified to 'CONSTANT' | ||
simplifiable-condition:17::Boolean condition 'CONSTANT and True' may be simplified to 'CONSTANT' | ||
simplifiable-condition:19::Boolean condition 'CONSTANT and True' may be simplified to 'CONSTANT' | ||
simplifiable-condition:21::Boolean condition 'CONSTANT or False' may be simplified to 'CONSTANT' | ||
simplifiable-condition:22::Boolean condition 'x or False' may be simplified to 'x' | ||
simplifiable-condition:25::Boolean condition 'CONSTANT or True and False' may be simplified to 'CONSTANT' | ||
simplifiable-condition:26::Boolean condition 'True and CONSTANT and OTHER' may be simplified to 'CONSTANT and OTHER' | ||
simplifiable-condition:27::Boolean condition '(CONSTANT or False) and (OTHER or True)' may be simplified to 'CONSTANT' | ||
simplifiable-condition:30::Boolean condition '[] or CONSTANT' may be simplified to 'CONSTANT' | ||
simplifiable-condition:31::Boolean condition '{} or CONSTANT' may be simplified to 'CONSTANT' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters