Skip to content

Add missing diagnostic for missing brace or if after else #1444

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,10 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
addDiagnostic(node.conditions, MissingConditionInStatement(node: node), handledNodes: [node.conditions.id])
}

if let leftBrace = node.elseBody?.as(CodeBlockSyntax.self)?.leftBrace, leftBrace.presence == .missing {
addDiagnostic(leftBrace, .expectedLeftBraceOrIfAfterElse, handledNodes: [leftBrace.id])
}

return .visitChildren
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ extension DiagnosticMessage where Self == StaticParserError {
public static var expectedExpressionAfterTry: Self {
.init("expected expression after 'try'")
}
public static var expectedLeftBraceOrIfAfterElse: Self {
.init("expected '{' or 'if' after 'else'")
}
public static var expectedSequenceExpressionInForEachLoop: Self {
.init("expected Sequence expression for for-each loop")
}
Expand Down
3 changes: 1 addition & 2 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2202,8 +2202,7 @@ final class RecoveryTests: XCTestCase {
}2️⃣
""",
diagnostics: [
// TODO: Old parser expected error on line 2: expected '{' or 'if' after 'else'
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in 'if' statement"),
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' or 'if' after 'else'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end function"),
]
)
Expand Down