-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Consider irrefutable pattern similar to if .. else
for C901
#11565
Changes from 1 commit
c4c54dd
54ddcae
0d5b57b
a5afa76
2248ca8
264d659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
use ast::Pattern; | ||
use ruff_python_ast::{self as ast, ExceptHandler, Stmt}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
|
@@ -96,8 +97,16 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize { | |
complexity += get_complexity_number(orelse); | ||
} | ||
Stmt::Match(ast::StmtMatch { cases, .. }) => { | ||
for case in cases { | ||
let last_index = cases.len() - 1; | ||
for (i, case) in cases.iter().enumerate() { | ||
complexity += 1; | ||
if let Pattern::MatchAs(match_as_pattern) = &case.pattern { | ||
if match_as_pattern.pattern.is_none() && i == last_index { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason that we restrict the logic to only when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because an irrefutable pattern can only occur in the last case block otherwise it's a syntax error (checked by the compiler). We don't raise this currently, but we should start doing so. I'm going to make an umbrella issue with such syntax errors raised by the compiler. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. But that also implies that it would be safe to ignore the check here and simply assume it is the last? |
||
// the catch all `_` or named catch all case doesn't increase | ||
// the complexity similar to a plain `else` stmt. | ||
complexity -= 1; | ||
} | ||
} | ||
complexity += get_complexity_number(&case.body); | ||
} | ||
} | ||
|
@@ -431,17 +440,49 @@ def with_lock(): | |
} | ||
|
||
#[test] | ||
fn match_case() -> Result<()> { | ||
fn simple_match_case() -> Result<()> { | ||
let source = r" | ||
def f(): | ||
match a: | ||
case 30: | ||
match subject: | ||
case 2: | ||
print('foo') | ||
case _: | ||
print('bar') | ||
"; | ||
let stmts = parse_suite(source)?; | ||
assert_eq!(get_complexity_number(&stmts), 2); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn multiple_match_case() -> Result<()> { | ||
let source = r" | ||
def f(): | ||
match subject: | ||
case 2: | ||
print('foo') | ||
case 2: | ||
print('bar') | ||
case _: | ||
print('baz') | ||
"; | ||
let stmts = parse_suite(source)?; | ||
assert_eq!(get_complexity_number(&stmts), 3); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn named_catch_all_match_case() -> Result<()> { | ||
let source = r" | ||
def f(): | ||
match subject: | ||
case 2: | ||
print('hello') | ||
case x: | ||
print(x) | ||
"; | ||
let stmts = parse_suite(source)?; | ||
assert_eq!(get_complexity_number(&stmts), 2); | ||
Ok(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
saturating_sub
here to avoid a panic formatch a:
. This is not valid python but the parser might soon be able to recover from incomplete statements.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went with Dhurv's suggestion here.