Skip to content

Commit

Permalink
Fix TRY300 false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed Mar 20, 2023
1 parent fd39ec4 commit 53706af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/tryceratops/TRY300.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions crates/ruff/src/rules/tryceratops/rules/try_consider_else.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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
Expand Down

0 comments on commit 53706af

Please sign in to comment.