Skip to content

Commit 2b0fa1c

Browse files
author
Greg Titus
committed
Add diagnostic for extraneous case keyword when multiple patterns.
1 parent 11a5359 commit 2b0fa1c

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

include/swift/AST/DiagnosticsParse.def

+2
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,8 @@ ERROR(expected_case_where_expr,PointsToFirstBadToken,
12221222
"expected expression for 'where' guard of 'case'", ())
12231223
ERROR(expected_case_colon,PointsToFirstBadToken,
12241224
"expected ':' after '%0'", (StringRef))
1225+
ERROR(extra_case_keyword,none,
1226+
"extraneous 'case' keyword in pattern", ())
12251227
ERROR(default_with_where,none,
12261228
"'default' cannot be used with a 'where' guard expression",
12271229
())

lib/Parse/ParseStmt.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,12 @@ static ParserStatus parseStmtCase(Parser &P, SourceLoc &CaseLoc,
26332633
isFirst = false;
26342634
if (!P.consumeIf(tok::comma))
26352635
break;
2636+
2637+
if (P.Tok.is(tok::kw_case)) {
2638+
P.diagnose(P.Tok, diag::extra_case_keyword)
2639+
.fixItRemove(SourceRange(P.Tok.getLoc()));
2640+
P.consumeToken(tok::kw_case);
2641+
}
26362642
}
26372643
}
26382644

test/Parse/switch.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ default:
7373
// Multiple cases per case block
7474
switch x { // expected-error {{switch must be exhaustive}} expected-note{{add a default clause}}
7575
case 0: // expected-error {{'case' label in a 'switch' must have at least one executable statement}} {{8-8= break}}
76-
case 1:
76+
case 1, case 2: // expected-error {{extraneous 'case' keyword in pattern}} {{9-14=}}
7777
x = 0
7878
}
7979

0 commit comments

Comments
 (0)