Omit while-True loops from implicit return enforcement (#3076)

This commit is contained in:
Charlie Marsh 2023-02-20 18:22:28 -05:00 committed by GitHub
parent 35f7f7b66d
commit 7d4e513a82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View file

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

View file

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