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

Conversation

blueraft
Copy link
Contributor

Summary

Follow up to #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.

Copy link
Contributor

github-actions bot commented May 27, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

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

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?

@dhruvmanila
Copy link
Member

I think we should just use cases.last:

if let Some(last_case) = cases.last() {
	// ...
}

Comment on lines 103 to 105
if let Some(last_case) = cases.last() {
if let Pattern::MatchAs(match_as_pattern) = &last_case.pattern {
if match_as_pattern.pattern.is_none() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there are more cases to consider here:

  1. Make sure that the guard field is None (last_case.guard.is_none())
  2. Consider sequence pattern where if any pattern in the sequence is _ or named capture, the entire sequence is considered irrefutable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Sorry for sending this review after the approval)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Done.

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I've made is_irrefutable a method on Pattern.

@dhruvmanila dhruvmanila added the rule Implementing or modifying a lint rule label May 27, 2024
@dhruvmanila dhruvmanila changed the title Handle match catch all case for complexity check Consider irrefutable pattern similar to if .. else for C901 May 27, 2024
@dhruvmanila dhruvmanila enabled auto-merge (squash) May 27, 2024 17:31
@dhruvmanila dhruvmanila merged commit b36c713 into astral-sh:main May 27, 2024
17 checks passed
@blueraft blueraft deleted the match-case-catch-all branch May 27, 2024 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants