diff --git a/crates/ruff/resources/test/fixtures/tryceratops/TRY300.py b/crates/ruff/resources/test/fixtures/tryceratops/TRY300.py index 00a404f54b..c45af4acb1 100644 --- a/crates/ruff/resources/test/fixtures/tryceratops/TRY300.py +++ b/crates/ruff/resources/test/fixtures/tryceratops/TRY300.py @@ -45,3 +45,10 @@ def still_good(): return process() except MyException: logger.exception("process failed") + +def good_noexcept(): + try: + pass + return process() + finally: + logger.exception("process failed") diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 56f616d9fa..99701ee996 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1722,7 +1722,7 @@ where ); } if self.settings.rules.enabled(Rule::TryConsiderElse) { - tryceratops::rules::try_consider_else(self, body, orelse); + tryceratops::rules::try_consider_else(self, body, orelse, handlers); } if self.settings.rules.enabled(Rule::VerboseRaise) { tryceratops::rules::verbose_raise(self, handlers); diff --git a/crates/ruff/src/rules/tryceratops/rules/try_consider_else.rs b/crates/ruff/src/rules/tryceratops/rules/try_consider_else.rs index 3377bfdecf..35cd327539 100644 --- a/crates/ruff/src/rules/tryceratops/rules/try_consider_else.rs +++ b/crates/ruff/src/rules/tryceratops/rules/try_consider_else.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}; @@ -17,8 +17,13 @@ impl Violation for TryConsiderElse { } /// TRY300 -pub fn try_consider_else(checker: &mut Checker, body: &[Stmt], orelse: &[Stmt]) { - if body.len() > 1 && orelse.is_empty() { +pub fn try_consider_else( + checker: &mut Checker, + body: &[Stmt], + orelse: &[Stmt], + handler: &[Excepthandler], +) { + if body.len() > 1 && orelse.is_empty() && !handler.is_empty() { if let Some(stmt) = body.last() { if let StmtKind::Return { .. } = &stmt.node { checker