Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compare-to-zero] More actionnable and understandable message #7723

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/user_guide/checkers/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
23 changes: 20 additions & 3 deletions pylint/extensions/comparetozero.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.",
)
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
# pylint: disable=literal-comparison,missing-docstring
# pylint: disable=literal-comparison,missing-docstring, singleton-comparison

X = 123
Y = len('test')

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

Expand Down
6 changes: 6 additions & 0 deletions tests/functional/ext/comparetozero/compare_to_zero.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions tests/functional/ext/comparetozero/comparetozero.txt

This file was deleted.