summaryrefslogtreecommitdiff
path: root/pylint/extensions
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2022-11-06 17:10:22 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-11-07 21:30:49 +0100
commit472064a9ee85973f0f1d7b87c720199e1c147599 (patch)
treefe1f0c755134ea2210856a4c03d2b2349c2ccc05 /pylint/extensions
parenta04f641c9d9d95c232db81aa4649a00e7e7e87e1 (diff)
downloadpylint-git-472064a9ee85973f0f1d7b87c720199e1c147599.tar.gz
[compare-to-zero] More actionnable and understandable message
Originally done in order to normalize the use-implicit-booleaness check. This make the message more understandable and permit to make #6870 more reviewable.
Diffstat (limited to 'pylint/extensions')
-rw-r--r--pylint/extensions/comparetozero.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/pylint/extensions/comparetozero.py b/pylint/extensions/comparetozero.py
index f418c1833..c420939d3 100644
--- a/pylint/extensions/comparetozero.py
+++ b/pylint/extensions/comparetozero.py
@@ -36,7 +36,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 +66,25 @@ class CompareToZeroChecker(checkers.BaseChecker):
# 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: