Skip to content
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

Merged
merged 6 commits into from
May 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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};
Expand Down Expand Up @@ -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;
Copy link
Member

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 for match a:. This is not valid python but the parser might soon be able to recover from incomplete statements.

Suggested change
let last_index = cases.len() - 1;
let last_index = cases.len().saturating_sub(1);

Copy link
Contributor Author

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.

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 {
Copy link
Member

Choose a reason for hiding this comment

The 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 _ pattern is last? Can't we always subtract the complexity in that case?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
Expand Down Expand Up @@ -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(())
}
}
Loading