Avoid false-positives for break in with (#3032)

This commit is contained in:
Charlie Marsh 2023-02-19 11:17:04 -05:00 committed by GitHub
parent b75663be6d
commit 0f0e7a521a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -113,3 +113,14 @@ def test_break_in_if_orelse():
else:
return True
return False
def test_break_in_with():
"""no false positive for break in with"""
for name in ["demo"]:
with open(__file__) as f:
if name in f.read():
break
else:
return True
return False

View File

@ -22,6 +22,7 @@ impl Violation for UselessElseOnLoop {
fn loop_exits_early(body: &[Stmt]) -> bool {
body.iter().any(|stmt| match &stmt.node {
StmtKind::If { body, orelse, .. } => loop_exits_early(body) || loop_exits_early(orelse),
StmtKind::With { body, .. } | StmtKind::AsyncWith { body, .. } => loop_exits_early(body),
StmtKind::Try {
body,
handlers,