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:
Ahmed Ilyas 2024-05-27 19:33:36 +02:00 committed by GitHub
parent 34a5063aa2
commit b36c713279
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 82 additions and 3 deletions

View file

@ -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 {