From cb68a81eee457947b9750013c90bcfeb4eb22bcf Mon Sep 17 00:00:00 2001 From: Laura M?dioni Date: Thu, 29 Oct 2015 09:37:06 +0100 Subject: Fix crash with 'in' operator on unneeded-not --- pylint/checkers/base.py | 4 +++- pylint/test/functional/unneeded_not.py | 6 +++++- pylint/test/functional/unneeded_not.txt | 1 + 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]" -- cgit v1.2.1