diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM105_5.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM105_5.py new file mode 100644 index 0000000000000..3bb9c0ea04d5c --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM105_5.py @@ -0,0 +1,19 @@ +# OK: `except*` should not trigger SIM105 because `contextlib.suppress` doesn't +# support `BaseExceptionGroup` until Python 3.12, so the fix would be incorrect +# for Python 3.11. See https://github.com/astral-sh/ruff/issues/23798 + + +def foo(): + pass + + +try: + foo() +except* ValueError: + pass + + +try: + foo() +except* (ValueError, OSError): + pass diff --git a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs index 3c4a20437ed92..2a131a29ed45d 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs @@ -1316,7 +1316,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) { handlers, orelse, finalbody, - .. + is_star, }, ) => { if checker.is_rule_enabled(Rule::TooManyNestedBlocks) { @@ -1355,7 +1355,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) { } if checker.is_rule_enabled(Rule::SuppressibleException) { flake8_simplify::rules::suppressible_exception( - checker, stmt, body, handlers, orelse, finalbody, + checker, stmt, body, handlers, orelse, finalbody, *is_star, ); } if checker.is_rule_enabled(Rule::ReturnInTryExceptFinally) { diff --git a/crates/ruff_linter/src/rules/flake8_simplify/mod.rs b/crates/ruff_linter/src/rules/flake8_simplify/mod.rs index 408a86fe49c56..ac752b49a5896 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/mod.rs @@ -22,6 +22,7 @@ mod tests { #[test_case(Rule::SuppressibleException, Path::new("SIM105_2.py"))] #[test_case(Rule::SuppressibleException, Path::new("SIM105_3.py"))] #[test_case(Rule::SuppressibleException, Path::new("SIM105_4.py"))] + #[test_case(Rule::SuppressibleException, Path::new("SIM105_5.py"))] #[test_case(Rule::ReturnInTryExceptFinally, Path::new("SIM107.py"))] #[test_case(Rule::IfElseBlockInsteadOfIfExp, Path::new("SIM108.py"))] #[test_case(Rule::CompareWithTuple, Path::new("SIM109.py"))] diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs index 3c803ae6089ac..4bfd57cce460f 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs @@ -87,7 +87,11 @@ pub(crate) fn suppressible_exception( handlers: &[ExceptHandler], orelse: &[Stmt], finalbody: &[Stmt], + is_star: bool, ) { + if is_star { + return; + } if !matches!( try_body, [Stmt::Delete(_) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_5.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_5.py.snap new file mode 100644 index 0000000000000..c2c1ead635db5 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_5.py.snap @@ -0,0 +1,3 @@ +--- +source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs +---