summaryrefslogtreecommitdiff
path: root/pylint/test/functional/using_constant_test.py
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2015-05-23 20:11:55 +0300
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2015-05-23 20:11:55 +0300
commitd3996d3758f73b789eaa70263f0c03b29f029f7f (patch)
treee08ccfa6d257981cf501fca3e02144df30f3ef70 /pylint/test/functional/using_constant_test.py
parent26b75fb11969a4d56c0161fd0a6e6cbd4cf54c80 (diff)
downloadpylint-git-d3996d3758f73b789eaa70263f0c03b29f029f7f.tar.gz
Add a new warning, 'using-constant-test'.
This new warning is emitted when a conditional statement (If, IfExp) uses a test which is always constant, such as numbers, classes, functions etc. This is most likely an error from the user's part. Closes issue #524.
Diffstat (limited to 'pylint/test/functional/using_constant_test.py')
-rw-r--r--pylint/test/functional/using_constant_test.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/pylint/test/functional/using_constant_test.py b/pylint/test/functional/using_constant_test.py
new file mode 100644
index 000000000..1e7ee0f0a
--- /dev/null
+++ b/pylint/test/functional/using_constant_test.py
@@ -0,0 +1,119 @@
+"""Verify if constant tests are used inside if statements."""
+# pylint: disable=invalid-name, missing-docstring,too-few-public-methods
+# pylint: disable=no-init
+
+
+import collections
+
+
+def function():
+ yield
+
+
+class Class(object):
+
+ def method(self):
+ pass
+
+
+instance = Class()
+
+if collections: # [using-constant-test]
+ pass
+
+# GenExpr
+if (node for node in range(10)): # [using-constant-test]
+ pass
+
+if lambda: None: # [using-constant-test]
+ pass
+
+if function: # [using-constant-test]
+ pass
+
+if Class: # [using-constant-test]
+ pass
+
+if 2: # [using-constant-test]
+ pass
+
+if True: # [using-constant-test]
+ pass
+
+if '': # [using-constant-test]
+ pass
+
+if b'': # [using-constant-test]
+ pass
+
+if 2.0: # [using-constant-test]
+ pass
+
+if {}: # [using-constant-test]
+ pass
+
+if {1, 2, 3}: # [using-constant-test]
+ pass
+
+if (1, 2, 3): # [using-constant-test]
+ pass
+
+if (): # [using-constant-test]
+ pass
+
+# Generator
+generator = function()
+if generator: # [using-constant-test]
+ pass
+
+if 1 if 2 else 3: # [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
+# can be anything else (2 + somefunccall).
+
+name = 42
+if name:
+ pass
+
+# UnboundMethod / Function
+if Class.method:
+ pass
+
+# BoundMethod
+if instance.method:
+ pass
+
+if 3 + 4:
+ pass
+
+if 3 and 4:
+ pass
+
+if not 3:
+ pass
+
+if instance.method():
+ pass
+
+if 2 < 3:
+ pass
+
+if tuple((1, 2, 3)):
+ pass
+
+if dict():
+ pass
+
+if tuple():
+ pass
+
+if [1, 2, 3][:1]:
+ pass
+
+def test(*args):
+ if args:
+ return 42