Skip to content

Commit

Permalink
Avoid false-positives for break in with (#3032)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 19, 2023
1 parent b75663b commit 0f0e7a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/ruff/resources/test/fixtures/pylint/useless_else_on_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions crates/ruff/src/rules/pylint/rules/useless_else_on_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0f0e7a5

Please sign in to comment.