Fix TRY300 false positive (#3634)

This commit is contained in:
Jonathan Plasse
2023-03-20 21:55:28 +01:00
committed by GitHub
parent fd39ec4bdd
commit 169dd72328
3 changed files with 16 additions and 4 deletions

View File

@@ -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")

View File

@@ -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);

View File

@@ -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