From 7e3894f5b3573d77b0000bbddf0293fbbb5dc986 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 3 Oct 2024 15:35:05 +0530 Subject: [PATCH] Avoid short circuiting `B017` for multiple context managers (#13609) ## Summary fixes: #13603 --- .../resources/test/fixtures/flake8_bugbear/B017.py | 3 +++ .../flake8_bugbear/rules/assert_raises_exception.rs | 12 ++++++------ ...__rules__flake8_bugbear__tests__B017_B017.py.snap | 9 ++++++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B017.py b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B017.py index 917a848ba1884..99def7a555e13 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B017.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B017.py @@ -53,3 +53,6 @@ def test_pytest_raises(): with pytest.raises(Exception, match="hello"): raise ValueError("This is also fine") + + with contextlib.nullcontext(), pytest.raises(Exception): + raise ValueError("Multiple context managers") diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/assert_raises_exception.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/assert_raises_exception.rs index 18c84a84c19e2..a3da3f9dc69ad 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/assert_raises_exception.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/assert_raises_exception.rs @@ -84,27 +84,27 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem]) range: _, }) = &item.context_expr else { - return; + continue; }; if item.optional_vars.is_some() { - return; + continue; } let [arg] = &*arguments.args else { - return; + continue; }; let semantic = checker.semantic(); let Some(builtin_symbol) = semantic.resolve_builtin_symbol(arg) else { - return; + continue; }; let exception = match builtin_symbol { "Exception" => ExceptionKind::Exception, "BaseException" => ExceptionKind::BaseException, - _ => return, + _ => continue, }; 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]) { AssertionKind::PytestRaises } else { - return; + continue; }; checker.diagnostics.push(Diagnostic::new( diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B017_B017.py.snap b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B017_B017.py.snap index 59e2c1b5794fe..1d4ac20d75a6f 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B017_B017.py.snap +++ b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B017_B017.py.snap @@ -35,4 +35,11 @@ B017.py:48:10: B017 `pytest.raises(Exception)` should be considered evil 49 | raise ValueError("Hello") | - +B017.py:57:36: B017 `pytest.raises(Exception)` should be considered evil + | +55 | raise ValueError("This is also fine") +56 | +57 | with contextlib.nullcontext(), pytest.raises(Exception): + | ^^^^^^^^^^^^^^^^^^^^^^^^ B017 +58 | raise ValueError("Multiple context managers") + |