Skip to content

Commit

Permalink
Add B015: Pointless comparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
kalekseev committed Jul 10, 2020
1 parent 41c788b commit 0782e4b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ instead of ``except (SomeError,):``.
**B014**: Redundant exception types in ``except (Exception, TypeError):``.
Write ``except Exception:``, which catches exactly the same exceptions.

**B015**: Pointless comparison. This comparison does nothing but
wastes CPU instructions. Remove it.


Python 3 compatibility warnings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
12 changes: 12 additions & 0 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ def visit_Try(self, node):
self.check_for_b012(node)
self.generic_visit(node)

def visit_Compare(self, node):
self.check_for_b015(node)

def compose_call_path(self, node):
if isinstance(node, ast.Attribute):
yield from self.compose_call_path(node.value)
Expand Down Expand Up @@ -371,6 +374,10 @@ def _loop(node, bad_node_types):
for child in node.finalbody:
_loop(child, (ast.Return, ast.Continue, ast.Break))

def check_for_b015(self, node):
if isinstance(self.node_stack[-2], ast.Expr):
self.errors.append(B015(node.lineno, node.col_offset))

def walk_function_body(self, node):
def _loop(parent, node):
if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef)):
Expand Down Expand Up @@ -658,6 +665,11 @@ def visit(self, node):
"select.error",
}
}
B015 = Error(
message=(
"B015 Pointless comparison. This comparison does nothing but wastes CPU instructions. Remove it."
)
)

# Those could be false positives but it's more dangerous to let them slip
# through if they're not.
Expand Down
29 changes: 29 additions & 0 deletions tests/b015.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Should emit:
B015 - on lines 8, 12, 22, 29
"""

assert 1 == 1

1 == 1

assert 1 in (1, 2)

1 in (1, 2)


if 1 == 2:
pass


def test():
assert 1 in (1, 2)

1 in (1, 2)


data = [x for x in [1, 2, 3] if x in (1, 2)]


class TestClass:
1 == 1
8 changes: 8 additions & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
B012,
B013,
B014,
B015,
B301,
B302,
B303,
Expand Down Expand Up @@ -182,6 +183,13 @@ def test_b014(self):
)
self.assertEqual(errors, expected)

def test_b015(self):
filename = Path(__file__).absolute().parent / "b015.py"
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())
expected = self.errors(B015(8, 0), B015(12, 0), B015(22, 4), B015(29, 4))
self.assertEqual(errors, expected)

def test_b301_b302_b305(self):
filename = Path(__file__).absolute().parent / "b301_b302_b305.py"
bbc = BugBearChecker(filename=str(filename))
Expand Down

0 comments on commit 0782e4b

Please sign in to comment.