Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: flake8-eyeo/flake8_eyeo.py

Issue 29634555: NoIssue - Detect redundant comparisons Base URL: https://hg.adblockplus.org/codingtools
Patch Set: Created Dec. 10, 2017, 6:10 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: flake8-eyeo/flake8_eyeo.py
===================================================================
--- a/flake8-eyeo/flake8_eyeo.py
+++ b/flake8-eyeo/flake8_eyeo.py
@@ -287,6 +287,36 @@
self.generic_visit(node)
+ def visit_BoolOp(self, node):
+ if isinstance(node.op, ast.Or):
+ left = node.values[0]
+ right = node.values[1]
Sebastian Noack 2017/12/10 23:19:57 What if there is another condition in between? E.g
+ if (isinstance(left, ast.Compare) and
+ isinstance(right, ast.Compare)):
+ if (isinstance(left.ops[0], ast.Eq) and
+ isinstance(left.ops[0], ast.Eq) or
Sebastian Noack 2017/12/10 23:19:57 This expression is the same as the left-hand side
+ (isinstance(left.ops[0], ast.NotEq) and
+ isinstance(left.ops[0], ast.NotEq))):
+ value1names = []
+ value2names = []
+ # Gather variable names from the left comparison
+ if isinstance(left.left, ast.Name):
Sebastian Noack 2017/12/10 23:19:57 What if it is an Attribute node? E.g: x.a == 1 or
+ value1names.append(left.left.id)
+ if isinstance(left.comparators[0], ast.Name):
+ value1names.append(left.comparators[0].id)
+ # Gather variable names from the right comparison
+ if isinstance(right.left, ast.Name):
+ value2names.append(right.left.id)
+ if isinstance(right.comparators[0], ast.Name):
+ value2names.append(right.comparators[0].id)
+ # Check for duplicates
+ for i in value1names:
+ if i in value2names:
+ self.errors.append((node, 'A208 `{} == x or {} == '
+ 'y` should be `{} in {{x'
+ ', y}}`'.format(i, i,
+ i)))
+
def _check_deprecated(self, node, name):
substitute = DISCOURAGED_APIS.get(name)
if substitute:
@@ -391,7 +421,7 @@
checker_state['has_unicode_literals'] = True
for kind, token, start, end, _ in tokens:
- if kind == tokenize.INDENT or kind == tokenize.DEDENT:
+ if kind in {tokenize.INDENT, tokenize.DEDENT}:
continue
if kind == tokenize.STRING:
« no previous file with comments | « flake8-eyeo/README.md ('k') | flake8-eyeo/tests/A208.py » ('j') | flake8-eyeo/tests/A208.py » ('J')

Powered by Google App Engine
This is Rietveld