diff options
author | Laura M?dioni <laura.medioni@logilab.fr> | 2015-10-26 09:35:59 +0100 |
---|---|---|
committer | Laura M?dioni <laura.medioni@logilab.fr> | 2015-10-26 09:35:59 +0100 |
commit | 3820ddea785c532428642ae2f96d9e8bff244fbf (patch) | |
tree | d391102c5ff6157bae0b50392c5acdbf51d0a6c2 /pylint/checkers/base.py | |
parent | 6f59c12b55765a3cc721864e8882f1e8e49bcb58 (diff) | |
download | pylint-3820ddea785c532428642ae2f96d9e8bff244fbf.tar.gz |
Check for nots too many in comparison expressions
Diffstat (limited to 'pylint/checkers/base.py')
-rw-r--r-- | pylint/checkers/base.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 74a1f64..e8a4396 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -1843,6 +1843,36 @@ class ElifChecker(BaseTokenChecker): (len(self._nested_blocks), self.config.max_nested_blocks)) +class NotChecker(_BasicChecker): + """checks for nots too many in comparison expressions + + - "not not" should trigger a warning + - "not" followed by a comparison should trigger a warning + """ + msgs = {'W0126': ('One not too many in "%s", should be "%s"', + 'unneeded-not', + 'Used when a boolean expression contains an unneeded ' + 'negation.'), + } + + @check_messages('unneeded-not') + def visit_unaryop(self, node): + if node.op != 'not': + return + operand = node.operand + if isinstance(operand, astroid.UnaryOp) and operand.op == 'not': + self.add_message('unneeded-not', node=node, + args=(node.as_string(), + operand.operand.as_string())) + elif isinstance(operand, astroid.Compare): + operator, _ = operand.ops[0] + reverse_op = {'<': '>=', '<=': '>', '>': '<=', '>=': '<', + '==': '!=', '!=': '=='} + suggestion = node.as_string().replace('not ', '').replace( + operator, reverse_op[operator]) + self.add_message('unneeded-not', node=node, + args=(node.as_string(), suggestion)) + def register(linter): """required method to auto register this checker""" @@ -1853,5 +1883,6 @@ def register(linter): linter.register_checker(PassChecker(linter)) linter.register_checker(LambdaForComprehensionChecker(linter)) linter.register_checker(ComparisonChecker(linter)) + linter.register_checker(NotChecker(linter)) linter.register_checker(RecommandationChecker(linter)) linter.register_checker(ElifChecker(linter)) |