summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2023-05-02 22:24:58 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2023-05-03 22:01:26 +0200
commit584f211ac725cbf07153fed7c7d5ae761f3ef913 (patch)
treeb0070355b57bca23d78fc9ce5fed3c191b6ced16
parentd3c14c645e4ce450393390992a68ab9d7588ec75 (diff)
downloadpylint-git-584f211ac725cbf07153fed7c7d5ae761f3ef913.tar.gz
[use-implicit-booleaness] Optimization for unknown operators
-rw-r--r--pylint/checkers/refactoring/implicit_booleaness_checker.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/pylint/checkers/refactoring/implicit_booleaness_checker.py b/pylint/checkers/refactoring/implicit_booleaness_checker.py
index 4b1d07d27..6eb516699 100644
--- a/pylint/checkers/refactoring/implicit_booleaness_checker.py
+++ b/pylint/checkers/refactoring/implicit_booleaness_checker.py
@@ -106,6 +106,7 @@ class ImplicitBooleanessChecker(checkers.BaseChecker):
}
options = ()
+ _operators = {"!=", "==", "is not", "is"}
@utils.only_required_for_messages("use-implicit-booleaness-not-len")
def visit_call(self, node: nodes.Call) -> None:
@@ -189,7 +190,6 @@ class ImplicitBooleanessChecker(checkers.BaseChecker):
self._check_compare_to_str_or_zero(node)
def _check_compare_to_str_or_zero(self, node: nodes.Compare) -> None:
- _operators = {"!=", "==", "is not", "is"}
# note: astroid.Compare has the left most operand in node.left
# while the rest are a list of tuples in node.ops
# the format of the tuple is ('compare operator sign', node)
@@ -199,19 +199,21 @@ class ImplicitBooleanessChecker(checkers.BaseChecker):
iter_ops = iter(ops)
all_ops = list(itertools.chain(*iter_ops))
for ops_idx in range(len(all_ops) - 2):
- op_1 = all_ops[ops_idx]
op_2 = all_ops[ops_idx + 1]
+ if op_2 not in self._operators:
+ continue
+ op_1 = all_ops[ops_idx]
op_3 = all_ops[ops_idx + 2]
error_detected = False
if self.linter.is_message_enabled(
"use-implicit-booleaness-not-comparison-to-zero"
):
# 0 ?? X
- if _is_constant_zero(op_1) and op_2 in _operators:
+ if _is_constant_zero(op_1):
error_detected = True
op = op_3
# X ?? 0
- elif op_2 in _operators and _is_constant_zero(op_3):
+ elif _is_constant_zero(op_3):
error_detected = True
op = op_1
if error_detected:
@@ -231,7 +233,7 @@ class ImplicitBooleanessChecker(checkers.BaseChecker):
if self.linter.is_message_enabled(
"use-implicit-booleaness-not-comparison-to-str"
):
- if op_1 is None or op_3 is None or op_2 not in _operators:
+ if op_1 is None or op_3 is None:
continue
node_name = ""
# x ?? ""