mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Consider irrefutable pattern similar to if .. else
for C901
(#11565)
## Summary Follow up to https://github.com/astral-sh/ruff/pull/11521 Removes the extra added complexity for catch all match cases. This matches the implementation of plain `else` statements. ## Test Plan Added new test cases. --------- Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
parent
34a5063aa2
commit
b36c713279
2 changed files with 82 additions and 3 deletions
|
@ -3020,6 +3020,21 @@ pub enum Pattern {
|
|||
MatchOr(PatternMatchOr),
|
||||
}
|
||||
|
||||
impl Pattern {
|
||||
/// Checks if the [`Pattern`] is an [irrefutable pattern].
|
||||
///
|
||||
/// [irrefutable pattern]: https://peps.python.org/pep-0634/#irrefutable-case-blocks
|
||||
pub fn is_irrefutable(&self) -> bool {
|
||||
match self {
|
||||
Pattern::MatchAs(PatternMatchAs { pattern: None, .. }) => true,
|
||||
Pattern::MatchOr(PatternMatchOr { patterns, .. }) => {
|
||||
patterns.iter().any(Pattern::is_irrefutable)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [MatchValue](https://docs.python.org/3/library/ast.html#ast.MatchValue)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PatternMatchValue {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue