Skip to content

Commit 94bda6f

Browse files
committed
Add diagnostic for empty switch cases
1 parent 7ad2a4f commit 94bda6f

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,8 @@ extension Parser {
22242224

22252225
let subject: RawExprSyntax
22262226

2227+
// When dosing this if-check, we ensure not passing an missing expression `{ foo }`
2228+
// as a closure expression.
22272229
if self.at(.leftBrace) {
22282230
subject = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
22292231
} else {

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,10 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
10341034
addDiagnostic(node.expression, .missingExpressionInSwitchStatement, handledNodes: [node.expression.id])
10351035
}
10361036

1037+
if node.cases.isEmpty && !node.leftBrace.isMissingAllTokens && !node.rightBrace.isMissingAllTokens {
1038+
addDiagnostic(node.cases, .emptySwitchExprBody, handledNodes: [node.cases.id])
1039+
}
1040+
10371041
return .visitChildren
10381042
}
10391043

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ extension DiagnosticMessage where Self == StaticParserError {
122122
public static var editorPlaceholderInSourceFile: Self {
123123
.init("editor placeholder in source file")
124124
}
125+
public static var emptySwitchExprBody: Self {
126+
.init("'switch' statement body must have at least one 'case' or 'default' block")
127+
}
125128
public static var escapedNewlineAtLatlineOfMultiLineStringLiteralNotAllowed: Self {
126129
.init("escaped newline at the last line of a multi-line string literal is not allowed")
127130
}

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,12 @@ final class RecoveryTests: XCTestCase {
621621
func testRecovery46() {
622622
AssertParse(
623623
"""
624-
switch 1️⃣{
624+
switch 1️⃣{2️⃣
625625
}
626626
""",
627627
diagnostics: [
628-
DiagnosticSpec(message: "expected expression in 'switch' statement")
629-
// TODO: Old parser expected error on line 1: 'switch' statement body must have at least one 'case' or 'default' block
628+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected expression in 'switch' statement"),
629+
DiagnosticSpec(locationMarker: "2️⃣", message: "'switch' statement body must have at least one 'case' or 'default' block"),
630630
]
631631
)
632632
}
@@ -635,12 +635,12 @@ final class RecoveryTests: XCTestCase {
635635
AssertParse(
636636
"""
637637
switch 1️⃣
638-
{
638+
{2️⃣
639639
}
640640
""",
641641
diagnostics: [
642-
DiagnosticSpec(message: "expected expression in 'switch' statement")
643-
// TODO: Old parser expected error on line 1: 'switch' statement body must have at least one 'case' or 'default' block
642+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected expression in 'switch' statement"),
643+
DiagnosticSpec(locationMarker: "2️⃣", message: "'switch' statement body must have at least one 'case' or 'default' block"),
644644
]
645645
)
646646
}
@@ -667,10 +667,8 @@ final class RecoveryTests: XCTestCase {
667667
}
668668
""",
669669
diagnostics: [
670-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected expression in 'switch' statement"),
670+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected expression in 'switch' statement")
671671
// TODO: Old parser expected error on line 2: 'is' keyword required to pattern match against type name, Fix-It replacements: 10 - 10 = 'is '
672-
DiagnosticSpec(locationMarker: "2️⃣", message: "'case' can only appear inside a 'switch' statement or 'enum' declaration"),
673-
DiagnosticSpec(locationMarker: "3️⃣", message: "'case' can only appear inside a 'switch' statement or 'enum' declaration"),
674672
]
675673
)
676674
}

0 commit comments

Comments
 (0)