summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaura M?dioni <laura.medioni@logilab.fr>2015-10-29 09:37:06 +0100
committerLaura M?dioni <laura.medioni@logilab.fr>2015-10-29 09:37:06 +0100
commitcb68a81eee457947b9750013c90bcfeb4eb22bcf (patch)
tree8b8cf0396b17cdadb2a3a73f72425f6c9e498c6a
parentf17f2d9546c42bbf53bb75a77734df33d0c2e182 (diff)
downloadpylint-cb68a81eee457947b9750013c90bcfeb4eb22bcf.tar.gz
Fix crash with 'in' operator on unneeded-not
-rw-r--r--pylint/checkers/base.py4
-rw-r--r--pylint/test/functional/unneeded_not.py6
-rw-r--r--pylint/test/functional/unneeded_not.txt1
3 files changed, 9 insertions, 2 deletions
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py
index df76c60..aba648c 100644
--- a/pylint/checkers/base.py
+++ b/pylint/checkers/base.py
@@ -1856,7 +1856,7 @@ class NotChecker(_BasicChecker):
}
reverse_op = {'<': '>=', '<=': '>', '>': '<=', '>=': '<', '==': '!=',
- '!=': '=='}
+ '!=': '==', 'in': 'not in'}
@check_messages('unneeded-not')
def visit_unaryop(self, node):
@@ -1870,6 +1870,8 @@ class NotChecker(_BasicChecker):
elif isinstance(operand, astroid.Compare):
left = operand.left
operator, right = operand.ops[0]
+ if operator not in self.reverse_op:
+ return
suggestion = '%s %s %s' % (left.as_string(),
self.reverse_op[operator],
right.as_string())
diff --git a/pylint/test/functional/unneeded_not.py b/pylint/test/functional/unneeded_not.py
index 882a238..78bf87c 100644
--- a/pylint/test/functional/unneeded_not.py
+++ b/pylint/test/functional/unneeded_not.py
@@ -1,6 +1,6 @@
"""Check exceeding negations in boolean expressions trigger warnings"""
-# pylint: disable=singleton-comparison
+# pylint: disable=singleton-comparison, too-many-branches
def unneeded_not():
"""This is not ok
@@ -31,6 +31,8 @@ def unneeded_not():
pass
if not True == True: # [unneeded-not]
pass
+ if not 2 in [3, 4]: # [unneeded-not]
+ pass
def not_checked():
@@ -39,3 +41,5 @@ def not_checked():
someint = 2
if not(bool_var == False and someint == 1):
pass
+ if 2 not in [3, 4]:
+ pass
diff --git a/pylint/test/functional/unneeded_not.txt b/pylint/test/functional/unneeded_not.txt
index cf27618..b962293 100644
--- a/pylint/test/functional/unneeded_not.txt
+++ b/pylint/test/functional/unneeded_not.txt
@@ -10,3 +10,4 @@ unneeded-not:26:unneeded_not:Consider changing "not bool_var == True" to "bool_v
unneeded-not:28:unneeded_not:Consider changing "not bool_var == False" to "bool_var != False"
unneeded-not:30:unneeded_not:Consider changing "not bool_var != True" to "bool_var == True"
unneeded-not:32:unneeded_not:Consider changing "not True == True" to "True != True"
+unneeded-not:34:unneeded_not:Consider changing "not 2 in [3, 4]" to "2 not in [3, 4]"