diff --git a/crates/ruff/resources/test/fixtures/flake8_return/RET503.py b/crates/ruff/resources/test/fixtures/flake8_return/RET503.py index 0f01cf486d1e7..74a399b0ffe4d 100644 --- a/crates/ruff/resources/test/fixtures/flake8_return/RET503.py +++ b/crates/ruff/resources/test/fixtures/flake8_return/RET503.py @@ -77,7 +77,7 @@ def x(y): # last line in while loop def x(y): - while True: + while i > 0: if y > 0: return 1 y += 1 @@ -259,3 +259,10 @@ def nested(values): for value in values: print(value) + + +def while_true(): + while True: + if y > 0: + return 1 + y += 1 diff --git a/crates/ruff/src/rules/flake8_return/rules.rs b/crates/ruff/src/rules/flake8_return/rules.rs index 3137977db423a..ba43b5397c686 100644 --- a/crates/ruff/src/rules/flake8_return/rules.rs +++ b/crates/ruff/src/rules/flake8_return/rules.rs @@ -220,6 +220,22 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) { checker.diagnostics.push(diagnostic); } } + StmtKind::Assert { test, .. } + if matches!( + test.node, + ExprKind::Constant { + value: Constant::Bool(false), + .. + } + ) => {} + StmtKind::While { test, .. } + if matches!( + test.node, + ExprKind::Constant { + value: Constant::Bool(true), + .. + } + ) => {} StmtKind::For { orelse, .. } | StmtKind::AsyncFor { orelse, .. } | StmtKind::While { orelse, .. } => { @@ -244,14 +260,6 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) { implicit_return(checker, last_stmt); } } - StmtKind::Assert { test, .. } - if matches!( - test.node, - ExprKind::Constant { - value: Constant::Bool(false), - .. - } - ) => {} StmtKind::Return { .. } | StmtKind::Raise { .. } | StmtKind::Try { .. } => {} StmtKind::Expr { value, .. } if matches!(