summaryrefslogtreecommitdiff
path: root/tests/functional
diff options
context:
space:
mode:
authorethan-leba <ethanleba5@gmail.com>2020-05-16 14:34:12 -0400
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2020-08-31 21:16:25 +0200
commit5b6844186f94e5fb4bdc63badbbf49b354912bfd (patch)
tree098dee527d13c3b700f5dd8485552ff72d8c38ac /tests/functional
parent5c84f0dafe79ca1631f70cf8355a5ccc7f9ed232 (diff)
downloadpylint-git-5b6844186f94e5fb4bdc63badbbf49b354912bfd.tar.gz
Add simplifiable-condition and condition-evals-to-constant to the refactoring checker
Diffstat (limited to 'tests/functional')
-rw-r--r--tests/functional/c/condition_evals_to_constant.py46
-rw-r--r--tests/functional/c/condition_evals_to_constant.txt15
-rw-r--r--tests/functional/c/consider_merging_isinstance.py2
-rw-r--r--tests/functional/l/len_checks.py2
-rw-r--r--tests/functional/s/simplifiable_condition.py36
-rw-r--r--tests/functional/s/simplifiable_condition.txt12
-rw-r--r--tests/functional/too/too_many_boolean_expressions.py2
-rw-r--r--tests/functional/u/using_constant_test.py3
8 files changed, 113 insertions, 5 deletions
diff --git a/tests/functional/c/condition_evals_to_constant.py b/tests/functional/c/condition_evals_to_constant.py
new file mode 100644
index 000000000..cfd0b00f4
--- /dev/null
+++ b/tests/functional/c/condition_evals_to_constant.py
@@ -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))
diff --git a/tests/functional/c/condition_evals_to_constant.txt b/tests/functional/c/condition_evals_to_constant.txt
new file mode 100644
index 000000000..5ff64887d
--- /dev/null
+++ b/tests/functional/c/condition_evals_to_constant.txt
@@ -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'
diff --git a/tests/functional/c/consider_merging_isinstance.py b/tests/functional/c/consider_merging_isinstance.py
index 34068d9b7..d3387bd5c 100644
--- a/tests/functional/c/consider_merging_isinstance.py
+++ b/tests/functional/c/consider_merging_isinstance.py
@@ -1,5 +1,5 @@
"""Checks use of consider-merging-isinstance"""
-# pylint:disable=line-too-long
+# pylint:disable=line-too-long, simplifiable-condition
def isinstances():
diff --git a/tests/functional/l/len_checks.py b/tests/functional/l/len_checks.py
index d7db14ddd..216a7e672 100644
--- a/tests/functional/l/len_checks.py
+++ b/tests/functional/l/len_checks.py
@@ -1,5 +1,5 @@
# pylint: disable=too-few-public-methods,import-error, no-absolute-import,missing-docstring, misplaced-comparison-constant
-# pylint: disable=useless-super-delegation,wrong-import-position,invalid-name, wrong-import-order
+# pylint: disable=useless-super-delegation,wrong-import-position,invalid-name, wrong-import-order, condition-evals-to-constant
if len('TEST'): # [len-as-condition]
pass
diff --git a/tests/functional/s/simplifiable_condition.py b/tests/functional/s/simplifiable_condition.py
new file mode 100644
index 000000000..32bf36aca
--- /dev/null
+++ b/tests/functional/s/simplifiable_condition.py
@@ -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))
diff --git a/tests/functional/s/simplifiable_condition.txt b/tests/functional/s/simplifiable_condition.txt
new file mode 100644
index 000000000..3649550c2
--- /dev/null
+++ b/tests/functional/s/simplifiable_condition.txt
@@ -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'
diff --git a/tests/functional/too/too_many_boolean_expressions.py b/tests/functional/too/too_many_boolean_expressions.py
index 68214ab8e..e8753859c 100644
--- a/tests/functional/too/too_many_boolean_expressions.py
+++ b/tests/functional/too/too_many_boolean_expressions.py
@@ -1,6 +1,6 @@
"""Checks for if statements containing too many boolean expressions"""
-# pylint: disable=invalid-name, comparison-with-itself, chained-comparison
+# pylint: disable=invalid-name, comparison-with-itself, chained-comparison, condition-evals-to-constant
x = y = z = 5
if x > -5 and x < 5 and y > -5 and y < 5 and z > -5 and z < 5: # [too-many-boolean-expressions]
diff --git a/tests/functional/u/using_constant_test.py b/tests/functional/u/using_constant_test.py
index d6624daa6..7e902e021 100644
--- a/tests/functional/u/using_constant_test.py
+++ b/tests/functional/u/using_constant_test.py
@@ -1,7 +1,7 @@
"""Verify if constant tests are used inside if statements."""
# pylint: disable=invalid-name, missing-docstring,too-few-public-methods
# pylint: disable=no-init,expression-not-assigned, useless-object-inheritance
-# pylint: disable=missing-parentheses-for-call-in-test, unnecessary-comprehension
+# pylint: disable=missing-parentheses-for-call-in-test, unnecessary-comprehension, condition-evals-to-constant
import collections
@@ -87,7 +87,6 @@ if Class.method: # [using-constant-test]
if instance.method: # [using-constant-test]
pass
-
# For these, we require to do inference, even though the result can be a
# constant value. For some of them, we could determine that the test
# is constant, such as 2 + 3, but the components of the BinOp