mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Avoid short-circuiting when detecting RET rules (#4002)
This commit is contained in:
parent
be87a29a9d
commit
79c47e29ee
1 changed files with 33 additions and 47 deletions
|
@ -463,34 +463,26 @@ fn superfluous_else_node(checker: &mut Checker, stmt: &Stmt, branch: Branch) ->
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RET505, RET506, RET507, RET508
|
/// RET505, RET506, RET507, RET508
|
||||||
fn superfluous_elif(checker: &mut Checker, stack: &Stack) -> bool {
|
fn superfluous_elif(checker: &mut Checker, stack: &Stack) {
|
||||||
for stmt in &stack.elifs {
|
for stmt in &stack.elifs {
|
||||||
if superfluous_else_node(checker, stmt, Branch::Elif) {
|
superfluous_else_node(checker, stmt, Branch::Elif);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RET505, RET506, RET507, RET508
|
/// RET505, RET506, RET507, RET508
|
||||||
fn superfluous_else(checker: &mut Checker, stack: &Stack) -> bool {
|
fn superfluous_else(checker: &mut Checker, stack: &Stack) {
|
||||||
for stmt in &stack.elses {
|
for stmt in &stack.elses {
|
||||||
if superfluous_else_node(checker, stmt, Branch::Else) {
|
superfluous_else_node(checker, stmt, Branch::Else);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run all checks from the `flake8-return` plugin.
|
/// Run all checks from the `flake8-return` plugin.
|
||||||
pub fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Expr>) {
|
pub fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Expr>) {
|
||||||
// Skip empty functions.
|
|
||||||
if body.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the last statement in the function.
|
// Find the last statement in the function.
|
||||||
let last_stmt = body.last().unwrap();
|
let Some(last_stmt) = body.last() else {
|
||||||
|
// Skip empty functions.
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
// Skip functions that consist of a single return statement.
|
// Skip functions that consist of a single return statement.
|
||||||
if body.len() == 1 && matches!(last_stmt.node, StmtKind::Return { .. }) {
|
if body.len() == 1 && matches!(last_stmt.node, StmtKind::Return { .. }) {
|
||||||
|
@ -511,20 +503,14 @@ pub fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Expr>) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if checker.settings.rules.enabled(Rule::SuperfluousElseReturn)
|
if checker.settings.rules.any_enabled(&[
|
||||||
|| checker.settings.rules.enabled(Rule::SuperfluousElseRaise)
|
Rule::SuperfluousElseReturn,
|
||||||
|| checker
|
Rule::SuperfluousElseRaise,
|
||||||
.settings
|
Rule::SuperfluousElseContinue,
|
||||||
.rules
|
Rule::SuperfluousElseBreak,
|
||||||
.enabled(Rule::SuperfluousElseContinue)
|
]) {
|
||||||
|| checker.settings.rules.enabled(Rule::SuperfluousElseBreak)
|
superfluous_elif(checker, &stack);
|
||||||
{
|
superfluous_else(checker, &stack);
|
||||||
if superfluous_elif(checker, &stack) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if superfluous_else(checker, &stack) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip any functions without return statements.
|
// Skip any functions without return statements.
|
||||||
|
@ -532,28 +518,28 @@ pub fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Expr>) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !result_exists(&stack.returns) {
|
// If we have at least one non-`None` return...
|
||||||
|
if result_exists(&stack.returns) {
|
||||||
|
if checker.settings.rules.enabled(Rule::ImplicitReturnValue) {
|
||||||
|
implicit_return_value(checker, &stack);
|
||||||
|
}
|
||||||
|
if checker.settings.rules.enabled(Rule::ImplicitReturn) {
|
||||||
|
implicit_return(checker, last_stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
if checker.settings.rules.enabled(Rule::UnnecessaryAssign) {
|
||||||
|
for (_, expr) in &stack.returns {
|
||||||
|
if let Some(expr) = expr {
|
||||||
|
unnecessary_assign(checker, &stack, expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if checker.settings.rules.enabled(Rule::UnnecessaryReturnNone) {
|
if checker.settings.rules.enabled(Rule::UnnecessaryReturnNone) {
|
||||||
// Skip functions that have a return annotation that is not `None`.
|
// Skip functions that have a return annotation that is not `None`.
|
||||||
if returns.map_or(true, is_const_none) {
|
if returns.map_or(true, is_const_none) {
|
||||||
unnecessary_return_none(checker, &stack);
|
unnecessary_return_none(checker, &stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if checker.settings.rules.enabled(Rule::ImplicitReturnValue) {
|
|
||||||
implicit_return_value(checker, &stack);
|
|
||||||
}
|
|
||||||
if checker.settings.rules.enabled(Rule::ImplicitReturn) {
|
|
||||||
implicit_return(checker, last_stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
if checker.settings.rules.enabled(Rule::UnnecessaryAssign) {
|
|
||||||
for (_, expr) in &stack.returns {
|
|
||||||
if let Some(expr) = expr {
|
|
||||||
unnecessary_assign(checker, &stack, expr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue