Exempt return with side effects for TRY300 (#3780)

This commit is contained in:
Jonathan Plasse 2023-03-29 01:52:05 +02:00 committed by GitHub
parent 5977862a60
commit 5501fc9572
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View file

@ -40,8 +40,9 @@ def noreturn():
logger.exception("process failed")
def still_good():
def good_return_with_side_effects():
try:
pass
return process()
except MyException:
logger.exception("process failed")

View file

@ -2,6 +2,7 @@ use rustpython_parser::ast::{Excepthandler, Stmt, StmtKind};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::contains_effect;
use ruff_python_ast::types::Range;
use crate::checkers::ast::Checker;
@ -25,7 +26,12 @@ pub fn try_consider_else(
) {
if body.len() > 1 && orelse.is_empty() && !handler.is_empty() {
if let Some(stmt) = body.last() {
if let StmtKind::Return { .. } = &stmt.node {
if let StmtKind::Return { value } = &stmt.node {
if let Some(value) = value {
if contains_effect(&checker.ctx, value) {
return;
}
}
checker
.diagnostics
.push(Diagnostic::new(TryConsiderElse, Range::from(stmt)));