diff --git a/crates/ruff/resources/test/fixtures/pylint/useless_else_on_loop.py b/crates/ruff/resources/test/fixtures/pylint/useless_else_on_loop.py index 6333c7d2d545c..5d2245e69ac7f 100644 --- a/crates/ruff/resources/test/fixtures/pylint/useless_else_on_loop.py +++ b/crates/ruff/resources/test/fixtures/pylint/useless_else_on_loop.py @@ -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 diff --git a/crates/ruff/src/rules/pylint/rules/useless_else_on_loop.rs b/crates/ruff/src/rules/pylint/rules/useless_else_on_loop.rs index 4df33a31d3582..f370a7142ad2f 100644 --- a/crates/ruff/src/rules/pylint/rules/useless_else_on_loop.rs +++ b/crates/ruff/src/rules/pylint/rules/useless_else_on_loop.rs @@ -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; @@ -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,