diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2018-12-14 11:15:53 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-12-14 11:15:53 +0100 |
commit | 32f566f6c06dd9637b5471ee2316ce6aaad11a70 (patch) | |
tree | 617ea5ba73a6f23a591851f87def3c087c5ed590 | |
parent | 09a86d3a224925acb99f78fb0c2955199da9ceb0 (diff) | |
download | pylint-git-32f566f6c06dd9637b5471ee2316ce6aaad11a70.tar.gz |
Make ``compare-to-zero`` less zealous by checking against equality and identity
Close #2645
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/extensions/comparetozero.py | 4 | ||||
-rw-r--r-- | pylint/test/extensions/data/compare_to_zero.py | 8 | ||||
-rw-r--r-- | pylint/test/extensions/test_comparetozero.py | 4 |
4 files changed, 11 insertions, 9 deletions
@@ -7,6 +7,10 @@ What's New in Pylint 2.3.0? Release date: TBA +* Make ``compare-to-zero`` less zealous by checking against equality and identity + + Close #2645 + * Add ``no-else-raise`` warning (R1720) Close #2558 diff --git a/pylint/extensions/comparetozero.py b/pylint/extensions/comparetozero.py index 16c94280f..92063ab18 100644 --- a/pylint/extensions/comparetozero.py +++ b/pylint/extensions/comparetozero.py @@ -60,10 +60,10 @@ class CompareToZeroChecker(checkers.BaseChecker): error_detected = False # 0 ?? X - if _is_constant_zero(op_1) and op_2 in _operators + ["<"]: + if _is_constant_zero(op_1) and op_2 in _operators: error_detected = True # X ?? 0 - elif op_2 in _operators + [">"] and _is_constant_zero(op_3): + elif op_2 in _operators and _is_constant_zero(op_3): error_detected = True if error_detected: diff --git a/pylint/test/extensions/data/compare_to_zero.py b/pylint/test/extensions/data/compare_to_zero.py index 06f3475dc..dfe6bbcb5 100644 --- a/pylint/test/extensions/data/compare_to_zero.py +++ b/pylint/test/extensions/data/compare_to_zero.py @@ -15,14 +15,14 @@ if X == 0: # [compare-to-zero] if Y != 0: # [compare-to-zero] pass -if X > 0: # [compare-to-zero] +if X > 0: pass -if X < 0: # this is allowed +if X < 0: pass -if 0 < X: # [compare-to-zero] +if 0 < X: pass -if 0 > X: # this is allowed +if 0 > X: pass diff --git a/pylint/test/extensions/test_comparetozero.py b/pylint/test/extensions/test_comparetozero.py index efd206c54..ebe4a466a 100644 --- a/pylint/test/extensions/test_comparetozero.py +++ b/pylint/test/extensions/test_comparetozero.py @@ -43,7 +43,7 @@ class CompareToZeroUsedTC(unittest.TestCase): 'compare_to_zero.py') self._linter.check([elif_test]) msgs = self._linter.reporter.messages - self.assertEqual(len(msgs), 6) + self.assertEqual(len(msgs), 4) for msg in msgs: self.assertEqual(msg.symbol, 'compare-to-zero') self.assertEqual(msg.msg, 'Avoid comparisons to zero') @@ -51,8 +51,6 @@ class CompareToZeroUsedTC(unittest.TestCase): self.assertEqual(msgs[1].line, 9) self.assertEqual(msgs[2].line, 12) self.assertEqual(msgs[3].line, 15) - self.assertEqual(msgs[4].line, 18) - self.assertEqual(msgs[5].line, 24) if __name__ == '__main__': |