Skip to content

Commit 5ecf5b0

Browse files
authored
Add B018 check to find useless string declarations (#196)
* Add B018 check to find useless string declarations This will find all sorts of useless string declarations in functions and classes. It will also prevent to use multiline strings as comments Resolves #195 * Better error message * Fix black
1 parent a29a583 commit 5ecf5b0

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ Either assert for a more specific exception (builtin or custom), use
130130
(``with self.assertRaises(Exception) as ex:``) with an assertion against the
131131
data available in ``ex``.
132132

133+
**B018**: Found useless expression. Either assign it to a variable or remove it.
134+
133135

134136
Opinionated warnings
135137
~~~~~~~~~~~~~~~~~~~~

bugbear.py

+12
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,12 @@ def visit_FunctionDef(self, node):
278278
self.check_for_b901(node)
279279
self.check_for_b902(node)
280280
self.check_for_b006(node)
281+
self.check_for_b018(node)
281282
self.generic_visit(node)
282283

283284
def visit_ClassDef(self, node):
284285
self.check_for_b903(node)
286+
self.check_for_b018(node)
285287
self.generic_visit(node)
286288

287289
def visit_Try(self, node):
@@ -575,6 +577,11 @@ def check_for_b903(self, node):
575577

576578
self.errors.append(B903(node.lineno, node.col_offset))
577579

580+
def check_for_b018(self, node):
581+
for subnode in node.body[1:]:
582+
if isinstance(subnode, ast.Expr) and isinstance(subnode.value, ast.Str):
583+
self.errors.append(B018(subnode.lineno, subnode.col_offset))
584+
578585

579586
@attr.s
580587
class NameFinder(ast.NodeVisitor):
@@ -767,6 +774,11 @@ def visit(self, node):
767774
"context manager form of assertRaises."
768775
)
769776
)
777+
B018 = Error(
778+
message=(
779+
"B018 Found useless expression. Either assign it to a variable or remove it."
780+
)
781+
)
770782

771783
# Warnings disabled by default.
772784
B901 = Error(

tests/b018.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Should emit:
3+
B018 - on lines 14, 19, 30, 35
4+
"""
5+
6+
7+
def foo1():
8+
"""my docstring"""
9+
10+
11+
def foo2():
12+
"""my docstring"""
13+
a = 2
14+
"str"
15+
16+
17+
def foo3():
18+
a = 2
19+
"str"
20+
21+
22+
class Foo1:
23+
"""abc"""
24+
25+
26+
class Foo2:
27+
"""abc"""
28+
29+
a = 2
30+
"str"
31+
32+
33+
class Foo3:
34+
a = 2
35+
"str"

tests/test_bugbear.py

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
B015,
2929
B016,
3030
B017,
31+
B018,
3132
B904,
3233
B901,
3334
B902,
@@ -211,6 +212,14 @@ def test_b017(self):
211212
expected = self.errors(B017(22, 8))
212213
self.assertEqual(errors, expected)
213214

215+
def test_b018(self):
216+
filename = Path(__file__).absolute().parent / "b018.py"
217+
bbc = BugBearChecker(filename=str(filename))
218+
errors = list(bbc.run())
219+
self.assertEqual(
220+
errors, self.errors(B018(14, 4), B018(19, 4), B018(30, 4), B018(35, 4))
221+
)
222+
214223
def test_b901(self):
215224
filename = Path(__file__).absolute().parent / "b901.py"
216225
bbc = BugBearChecker(filename=str(filename))

0 commit comments

Comments
 (0)