Skip to content

[Parse] Add diagnostic for extraneous case keyword when multiple patterns. #74071

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 1 commit into from
Jun 1, 2024

Conversation

gregomni
Copy link
Contributor

@gregomni gregomni commented Jun 1, 2024

This change adds a diagnostic for code that tries to use a second case keyword when writing a case statement with multiple patterns. E.g. case .a, case .b: instead of case .a, .b:. I've seen several people do this while pair programming and a recent question in the iOS-dev Slack on how to do this made me realize the current diagnostics for this (IMHO) common error are pretty terrible:

test.swift:8:12: error: expected pattern
  case .a, case .b:
           ^
test.swift:8:12: error: expected ':' after 'case'
  case .a, case .b:
           ^
test.swift:8:17: warning: case is already handled by previous patterns; consider removing it
  case .a, case .b:
                ^~

(This is because the second case causes pattern parsing to fail and the recovery inserts a match-anything pattern - thus the confusing warning.)

With this change, you get a single error, and a fix-it to remove the offending token:

test.swift:8:12: error: extraneous 'case' keyword in pattern

@gregomni
Copy link
Contributor Author

gregomni commented Jun 1, 2024

@swift-ci Please smoke test

@gregomni gregomni changed the title Add diagnostic for extraneous case keyword when multiple patterns. [Parse] Add diagnostic for extraneous case keyword when multiple patterns. Jun 1, 2024
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks you. Looks good. Would you be interested in also fixing this in the new parser?

@ahoppen ahoppen merged commit be7c634 into swiftlang:main Jun 1, 2024
3 checks passed
@gregomni
Copy link
Contributor Author

gregomni commented Jun 1, 2024

Sure, will do, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants