Skip to content

Commit 89f950c

Browse files
authored
B018 to detect useless-statements at all levels (#434)
* add unit test for b018 at all levels * fix b018 to detect useless statements at all levels
1 parent 4ca0e6b commit 89f950c

File tree

3 files changed

+90
-27
lines changed

3 files changed

+90
-27
lines changed

bugbear.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ def visit(self, node):
370370
if is_contextful:
371371
self.contexts.pop()
372372

373+
self.check_for_b018(node)
374+
373375
def visit_ExceptHandler(self, node):
374376
if node.type is None:
375377
self.errors.append(B001(node.lineno, node.col_offset))
@@ -447,7 +449,6 @@ def visit_Call(self, node):
447449
self.generic_visit(node)
448450

449451
def visit_Module(self, node):
450-
self.check_for_b018(node)
451452
self.generic_visit(node)
452453

453454
def visit_Assign(self, node):
@@ -503,15 +504,13 @@ def visit_FunctionDef(self, node):
503504
self.check_for_b901(node)
504505
self.check_for_b902(node)
505506
self.check_for_b006_and_b008(node)
506-
self.check_for_b018(node)
507507
self.check_for_b019(node)
508508
self.check_for_b021(node)
509509
self.check_for_b906(node)
510510
self.generic_visit(node)
511511

512512
def visit_ClassDef(self, node):
513513
self.check_for_b903(node)
514-
self.check_for_b018(node)
515514
self.check_for_b021(node)
516515
self.check_for_b024_and_b027(node)
517516
self.generic_visit(node)
@@ -1164,31 +1163,30 @@ def check_for_b903(self, node):
11641163
self.errors.append(B903(node.lineno, node.col_offset))
11651164

11661165
def check_for_b018(self, node):
1167-
for subnode in node.body:
1168-
if not isinstance(subnode, ast.Expr):
1169-
continue
1170-
if isinstance(
1171-
subnode.value,
1172-
(
1173-
ast.List,
1174-
ast.Set,
1175-
ast.Dict,
1176-
ast.Tuple,
1177-
),
1178-
) or (
1179-
isinstance(subnode.value, ast.Constant)
1180-
and (
1181-
isinstance(subnode.value.value, (int, float, complex, bytes, bool))
1182-
or subnode.value.value is None
1183-
)
1184-
):
1185-
self.errors.append(
1186-
B018(
1187-
subnode.lineno,
1188-
subnode.col_offset,
1189-
vars=(subnode.value.__class__.__name__,),
1190-
)
1166+
if not isinstance(node, ast.Expr):
1167+
return
1168+
if isinstance(
1169+
node.value,
1170+
(
1171+
ast.List,
1172+
ast.Set,
1173+
ast.Dict,
1174+
ast.Tuple,
1175+
),
1176+
) or (
1177+
isinstance(node.value, ast.Constant)
1178+
and (
1179+
isinstance(node.value.value, (int, float, complex, bytes, bool))
1180+
or node.value.value is None
1181+
)
1182+
):
1183+
self.errors.append(
1184+
B018(
1185+
node.lineno,
1186+
node.col_offset,
1187+
vars=(node.value.__class__.__name__,),
11911188
)
1189+
)
11921190

11931191
def check_for_b021(self, node):
11941192
if (

tests/b018_nested.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
X = 1
3+
False # bad
4+
5+
6+
def func(y):
7+
a = y + 1
8+
5.5 # bad
9+
return a
10+
11+
12+
class TestClass:
13+
GOOD = [1, 3]
14+
[5, 6] # bad
15+
16+
def method(self, xx, yy=5):
17+
t = (xx,)
18+
(yy,) # bad
19+
20+
while 1:
21+
i = 3
22+
4 # bad
23+
for n in range(i):
24+
j = 5
25+
1.5 # bad
26+
if j < n:
27+
u = {1, 2}
28+
{4, 5} # bad
29+
elif j == n:
30+
u = {1, 2, 3}
31+
{4, 5, 6} # bad
32+
else:
33+
u = {2, 3}
34+
{4, 6} # bad
35+
try:
36+
1j # bad
37+
r = 2j
38+
except Exception:
39+
r = 3j
40+
5 # bad
41+
finally:
42+
4j # bad
43+
r += 1
44+
return u + t

tests/test_bugbear.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,27 @@ def test_b018_modules(self):
331331
]
332332
self.assertEqual(errors, self.errors(*expected))
333333

334+
def test_b018_nested(self):
335+
filename = Path(__file__).absolute().parent / "b018_nested.py"
336+
bbc = BugBearChecker(filename=str(filename))
337+
errors = list(bbc.run())
338+
339+
expected = [
340+
B018(3, 0, vars=("Constant",)),
341+
B018(8, 4, vars=("Constant",)),
342+
B018(14, 4, vars=("List",)),
343+
B018(18, 8, vars=("Tuple",)),
344+
B018(22, 12, vars=("Constant",)),
345+
B018(25, 16, vars=("Constant",)),
346+
B018(28, 20, vars=("Set",)),
347+
B018(31, 20, vars=("Set",)),
348+
B018(34, 20, vars=("Set",)),
349+
B018(36, 24, vars=("Constant",)),
350+
B018(40, 24, vars=("Constant",)),
351+
B018(42, 24, vars=("Constant",)),
352+
]
353+
self.assertEqual(errors, self.errors(*expected))
354+
334355
def test_b019(self):
335356
filename = Path(__file__).absolute().parent / "b019.py"
336357
bbc = BugBearChecker(filename=str(filename))

0 commit comments

Comments
 (0)