Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid false-positives for break in with #3032

Merged
merged 1 commit into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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