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
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,8 @@ ERROR(expected_case_where_expr,PointsToFirstBadToken,
"expected expression for 'where' guard of 'case'", ())
ERROR(expected_case_colon,PointsToFirstBadToken,
"expected ':' after '%0'", (StringRef))
ERROR(extra_case_keyword,none,
"extraneous 'case' keyword in pattern", ())
ERROR(default_with_where,none,
"'default' cannot be used with a 'where' guard expression",
())
Expand Down
6 changes: 6 additions & 0 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,12 @@ static ParserStatus parseStmtCase(Parser &P, SourceLoc &CaseLoc,
isFirst = false;
if (!P.consumeIf(tok::comma))
break;

if (P.Tok.is(tok::kw_case)) {
P.diagnose(P.Tok, diag::extra_case_keyword)
.fixItRemove(SourceRange(P.Tok.getLoc()));
P.consumeToken(tok::kw_case);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/Parse/switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ default:
// Multiple cases per case block
switch x { // expected-error {{switch must be exhaustive}} expected-note{{add a default clause}}
case 0: // expected-error {{'case' label in a 'switch' must have at least one executable statement}} {{8-8= break}}
case 1:
case 1, case 2: // expected-error {{extraneous 'case' keyword in pattern}} {{9-14=}}
x = 0
}

Expand Down