Skip to content

Commit 7e3894f

Browse files
authored
Avoid short circuiting B017 for multiple context managers (#13609)
## Summary fixes: #13603
1 parent c3b40da commit 7e3894f

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B017.py

+3
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ def test_pytest_raises():
5353

5454
with pytest.raises(Exception, match="hello"):
5555
raise ValueError("This is also fine")
56+
57+
with contextlib.nullcontext(), pytest.raises(Exception):
58+
raise ValueError("Multiple context managers")

crates/ruff_linter/src/rules/flake8_bugbear/rules/assert_raises_exception.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,27 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
8484
range: _,
8585
}) = &item.context_expr
8686
else {
87-
return;
87+
continue;
8888
};
8989

9090
if item.optional_vars.is_some() {
91-
return;
91+
continue;
9292
}
9393

9494
let [arg] = &*arguments.args else {
95-
return;
95+
continue;
9696
};
9797

9898
let semantic = checker.semantic();
9999

100100
let Some(builtin_symbol) = semantic.resolve_builtin_symbol(arg) else {
101-
return;
101+
continue;
102102
};
103103

104104
let exception = match builtin_symbol {
105105
"Exception" => ExceptionKind::Exception,
106106
"BaseException" => ExceptionKind::BaseException,
107-
_ => return,
107+
_ => continue,
108108
};
109109

110110
let assertion = if matches!(func.as_ref(), Expr::Attribute(ast::ExprAttribute { attr, .. }) if attr == "assertRaises")
@@ -117,7 +117,7 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
117117
{
118118
AssertionKind::PytestRaises
119119
} else {
120-
return;
120+
continue;
121121
};
122122

123123
checker.diagnostics.push(Diagnostic::new(

crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B017_B017.py.snap

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ B017.py:48:10: B017 `pytest.raises(Exception)` should be considered evil
3535
49 | raise ValueError("Hello")
3636
|
3737

38-
38+
B017.py:57:36: B017 `pytest.raises(Exception)` should be considered evil
39+
|
40+
55 | raise ValueError("This is also fine")
41+
56 |
42+
57 | with contextlib.nullcontext(), pytest.raises(Exception):
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^ B017
44+
58 | raise ValueError("Multiple context managers")
45+
|

0 commit comments

Comments
 (0)