mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Exempt return with side effects for TRY300 (#3780)
This commit is contained in:
parent
5977862a60
commit
5501fc9572
2 changed files with 9 additions and 2 deletions
|
@ -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")
|
||||
|
|
|
@ -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)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue