Fix unsafe keyword dot-spacing edge case (#2513)

This commit is contained in:
Alexey
2026-04-24 17:25:41 +03:00
committed by Cal Stephens
parent a9ec7c34ce
commit 1e0aaaa516
2 changed files with 16 additions and 0 deletions
+2
View File
@@ -63,6 +63,8 @@ public extension FormatRule {
switch formatter.tokens[prevIndex] {
case .operator(_, .infix), .startOfScope:
return
case let token where [.identifier("unsafe")].contains(token):
return // `unsafe` is contextual, so leave existing spacing unchanged.
case let token where token.isUnwrapOperator:
if let prevToken = formatter.last(.nonSpace, before: prevIndex),
[.keyword("as"), .keyword("try")].contains(prevToken)
@@ -54,6 +54,20 @@ final class SpaceAroundOperatorsTests: XCTestCase {
testFormatting(for: input, rule: .spaceAroundOperators)
}
func testSpacePreservedBetweenUnsafeAndDot() {
let input = """
unsafe .foo
"""
testFormatting(for: input, rule: .spaceAroundOperators)
}
func testNoSpaceAddedBetweenUnsafeAndDot() {
let input = """
unsafe.foo
"""
testFormatting(for: input, rule: .spaceAroundOperators)
}
func testSpaceBetweenOptionalAndDefaultValueInFunction() {
let input = """
func foo(bar _: String?=nil) {}