Skip to content

Commit

Permalink
Avoid useless-else-on-loop for break within match (#3136)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 22, 2023
1 parent 6ced512 commit 4ad4e3e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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 @@ -124,3 +124,14 @@ def test_break_in_with():
else:
return True
return False


def test_break_in_match():
"""no false positive for break in match"""
for name in ["demo"]:
match name:
case "demo":
break
else:
return True
return False
5 changes: 4 additions & 1 deletion crates/ruff/src/rules/pylint/rules/useless_else_on_loop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::{ExcepthandlerKind, Stmt, StmtKind};
use rustpython_parser::ast::{ExcepthandlerKind, MatchCase, Stmt, StmtKind};

use crate::ast::helpers;
use crate::checkers::ast::Checker;
Expand All @@ -23,6 +23,9 @@ 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::Match { cases, .. } => cases
.iter()
.any(|MatchCase { body, .. }| loop_exits_early(body)),
StmtKind::Try {
body,
handlers,
Expand Down

0 comments on commit 4ad4e3e

Please sign in to comment.