diff --git a/crates/ruff/resources/test/fixtures/tryceratops/TRY301.py b/crates/ruff/resources/test/fixtures/tryceratops/TRY301.py index f9b98b8a0e..84023d2422 100644 --- a/crates/ruff/resources/test/fixtures/tryceratops/TRY301.py +++ b/crates/ruff/resources/test/fixtures/tryceratops/TRY301.py @@ -45,3 +45,10 @@ def good(): logger.exception("a failed") except Exception: logger.exception("something failed") + + +def fine(): + try: + a = process() # This throws the exception now + finally: + print("finally") diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index df17e266d2..075cada62f 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1811,7 +1811,7 @@ where tryceratops::rules::verbose_log_message(self, handlers); } if self.settings.rules.enabled(Rule::RaiseWithinTry) { - tryceratops::rules::raise_within_try(self, body); + tryceratops::rules::raise_within_try(self, body, handlers); } if self.settings.rules.enabled(Rule::ErrorInsteadOfException) { tryceratops::rules::error_instead_of_exception(self, handlers); diff --git a/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs b/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs index 88ad74dbfa..53c4731906 100644 --- a/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs +++ b/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs @@ -1,4 +1,4 @@ -use rustpython_parser::ast::{Stmt, StmtKind}; +use rustpython_parser::ast::{Excepthandler, Stmt, StmtKind}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -72,7 +72,11 @@ where } /// TRY301 -pub fn raise_within_try(checker: &mut Checker, body: &[Stmt]) { +pub fn raise_within_try(checker: &mut Checker, body: &[Stmt], handlers: &[Excepthandler]) { + if handlers.is_empty() { + return; + } + let raises = { let mut visitor = RaiseStatementVisitor::default(); for stmt in body {