Skip to content

[syntax-errors] Irrefutable case pattern before final case #16905

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

Merged
merged 5 commits into from
Mar 26, 2025

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Mar 21, 2025

Summary

Detects irrefutable match cases before the final case using a modified version
of the existing Pattern::is_irrefutable method from the AST crate. The
modified method helps to retrieve a more precise diagnostic range to match what
Python 3.13 shows in the REPL.

Test Plan

New inline tests, as well as some updates to existing tests that had irrefutable
patterns before the last block.

ntBre added 2 commits March 21, 2025 17:32
Summary
--

Detects irrefutable `match` cases before the final case using a modified version
of the existing `Pattern::is_irrefutable` method from the AST crate. The
modified method helps to retrieve a more precise diagnostic range to match what
Python 3.13 shows in the REPL.

Test Plan
--

New inline tests, as well as some updates to existing tests that had irrefutable
patterns before the last block.
@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Mar 21, 2025
Copy link
Contributor

github-actions bot commented Mar 21, 2025

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.

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.

Nice, this looks good!

We could improve the error messages as I think there are only two cases where this can occur - name and wildcard pattern. CPython provides the following error messages for the above cases:

  • SyntaxError: name capture 'foo' makes remaining patterns unreachable
  • SyntaxError: wildcard makes remaining patterns unreachable

Comment on lines 114 to 116
let Stmt::Match(ast::StmtMatch { cases, .. }) = stmt else {
return;
};
Copy link
Member

Choose a reason for hiding this comment

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

Should we move the match expression in check_stmt above now that we have multiple checks? That would also require updating the methods like irrefutable_match_case to accept the relevant AST node which needs to be checked like in this case it would accept StmtMatch which would also lead to a safer typed API.

@ntBre ntBre merged commit 5697d21 into main Mar 26, 2025
22 checks passed
@ntBre ntBre deleted the brent/syn-irrefutable-match-cases branch March 26, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants