diff --git a/doc/user_guide/checkers/extensions.rst b/doc/user_guide/checkers/extensions.rst index 0e28d1d579..ec973dac07 100644 --- a/doc/user_guide/checkers/extensions.rst +++ b/doc/user_guide/checkers/extensions.rst @@ -110,7 +110,7 @@ Verbatim name of the checker is ``compare-to-zero``. Compare-To-Zero checker Messages ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -:compare-to-zero (C2001): *Avoid comparisons to zero* +:compare-to-zero (C2001): *"%s" can be simplified to "%s" as 0 is falsey* Used when Pylint detects comparison to a 0 constant. diff --git a/pylint/extensions/comparetozero.py b/pylint/extensions/comparetozero.py index f418c1833b..116bf229a3 100644 --- a/pylint/extensions/comparetozero.py +++ b/pylint/extensions/comparetozero.py @@ -21,7 +21,11 @@ def _is_constant_zero(node: str | nodes.NodeNG) -> bool: - return isinstance(node, astroid.Const) and node.value == 0 + # We have to check that node.value is not False because node.value == 0 is True + # when node.value is False + return ( + isinstance(node, astroid.Const) and node.value == 0 and node.value is not False + ) class CompareToZeroChecker(checkers.BaseChecker): @@ -36,7 +40,7 @@ class CompareToZeroChecker(checkers.BaseChecker): name = "compare-to-zero" msgs = { "C2001": ( - "Avoid comparisons to zero", + '"%s" can be simplified to "%s" as 0 is falsey', "compare-to-zero", "Used when Pylint detects comparison to a 0 constant.", ) @@ -66,12 +70,25 @@ def visit_compare(self, node: nodes.Compare) -> None: # 0 ?? X if _is_constant_zero(op_1) and op_2 in _operators: error_detected = True + op = op_3 # X ?? 0 elif op_2 in _operators and _is_constant_zero(op_3): error_detected = True + op = op_1 if error_detected: - self.add_message("compare-to-zero", node=node, confidence=HIGH) + original = f"{op_1.as_string()} {op_2} {op_3.as_string()}" + suggestion = ( + op.as_string() + if op_2 in {"!=", "is not"} + else f"not {op.as_string()}" + ) + self.add_message( + "compare-to-zero", + args=(original, suggestion), + node=node, + confidence=HIGH, + ) def register(linter: PyLinter) -> None: diff --git a/tests/functional/ext/comparetozero/comparetozero.py b/tests/functional/ext/comparetozero/compare_to_zero.py similarity index 51% rename from tests/functional/ext/comparetozero/comparetozero.py rename to tests/functional/ext/comparetozero/compare_to_zero.py index 29fd13994a..6a14b8bc95 100644 --- a/tests/functional/ext/comparetozero/comparetozero.py +++ b/tests/functional/ext/comparetozero/compare_to_zero.py @@ -1,4 +1,4 @@ -# pylint: disable=literal-comparison,missing-docstring +# pylint: disable=literal-comparison,missing-docstring, singleton-comparison X = 123 Y = len('test') @@ -6,15 +6,33 @@ if X is 0: # [compare-to-zero] pass +if X is False: + pass + if Y is not 0: # [compare-to-zero] pass +if Y is not False: + pass + if X == 0: # [compare-to-zero] pass +if X == False: + pass + +if 0 == Y: # [compare-to-zero] + pass + if Y != 0: # [compare-to-zero] pass +if 0 != X: # [compare-to-zero] + pass + +if Y != False: + pass + if X > 0: pass diff --git a/tests/functional/ext/comparetozero/comparetozero.rc b/tests/functional/ext/comparetozero/compare_to_zero.rc similarity index 100% rename from tests/functional/ext/comparetozero/comparetozero.rc rename to tests/functional/ext/comparetozero/compare_to_zero.rc diff --git a/tests/functional/ext/comparetozero/compare_to_zero.txt b/tests/functional/ext/comparetozero/compare_to_zero.txt new file mode 100644 index 0000000000..a413a32682 --- /dev/null +++ b/tests/functional/ext/comparetozero/compare_to_zero.txt @@ -0,0 +1,6 @@ +compare-to-zero:6:3:6:9::"""X is 0"" can be simplified to ""not X"" as 0 is falsey":HIGH +compare-to-zero:12:3:12:13::"""Y is not 0"" can be simplified to ""Y"" as 0 is falsey":HIGH +compare-to-zero:18:3:18:9::"""X == 0"" can be simplified to ""not X"" as 0 is falsey":HIGH +compare-to-zero:24:3:24:9::"""0 == Y"" can be simplified to ""not Y"" as 0 is falsey":HIGH +compare-to-zero:27:3:27:9::"""Y != 0"" can be simplified to ""Y"" as 0 is falsey":HIGH +compare-to-zero:30:3:30:9::"""0 != X"" can be simplified to ""X"" as 0 is falsey":HIGH diff --git a/tests/functional/ext/comparetozero/comparetozero.txt b/tests/functional/ext/comparetozero/comparetozero.txt deleted file mode 100644 index f29aabae50..0000000000 --- a/tests/functional/ext/comparetozero/comparetozero.txt +++ /dev/null @@ -1,4 +0,0 @@ -compare-to-zero:6:3:6:9::Avoid comparisons to zero:HIGH -compare-to-zero:9:3:9:13::Avoid comparisons to zero:HIGH -compare-to-zero:12:3:12:9::Avoid comparisons to zero:HIGH -compare-to-zero:15:3:15:9::Avoid comparisons to zero:HIGH