diff --git a/bugbear.py b/bugbear.py index 27ca894..cf7acd9 100644 --- a/bugbear.py +++ b/bugbear.py @@ -285,18 +285,21 @@ def check_for_b011(self, node): self.errors.append(B011(node.lineno, node.col_offset)) def check_for_b012(self, node): - def _loop(node): + def _loop(node, bad_node_types): if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef)): return - if isinstance(node, (ast.Return, ast.Continue, ast.Break)): + if isinstance(node, (ast.While, ast.For)): + bad_node_types = (ast.Return,) + + elif isinstance(node, bad_node_types): self.errors.append(B012(node.lineno, node.col_offset)) for child in ast.iter_child_nodes(node): - _loop(child) + _loop(child, bad_node_types) for child in node.finalbody: - _loop(child) + _loop(child, (ast.Return, ast.Continue, ast.Break)) def walk_function_body(self, node): def _loop(parent, node): diff --git a/tests/b012.py b/tests/b012.py index 619780f..c03ff4e 100755 --- a/tests/b012.py +++ b/tests/b012.py @@ -82,6 +82,18 @@ def j(): continue # no warning +def k(): + try: + pass + finally: + while True: + break # no warning + while True: + continue # no warning + while True: + return # warning + + while True: try: pass diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 9d3fa56..e687e0c 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -132,8 +132,9 @@ def test_b012(self): B012(44, 20), B012(66, 12), B012(78, 12), - B012(89, 8), - B012(95, 8), + B012(94, 12), + B012(101, 8), + B012(107, 8), ] self.assertEqual(errors, self.errors(*all_errors))