Skip to content

Commit b8d99db

Browse files
committed
Don't lose a trailing comma in UseExplicitNilCheckInConditions.
This was happening because we were creating a new `ConditionListElement` and not considering the `trailingComma` property. It's safer to just mutate the original node, preserving anything that was previously there.
1 parent 2a85d12 commit b8d99db

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Sources/SwiftFormat/Rules/UseExplicitNilCheckInConditions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public final class UseExplicitNilCheckInConditions: SyntaxFormatRule {
5252
rightOperand: NilLiteralExprSyntax())
5353
inequalExpr.leadingTrivia = node.leadingTrivia
5454
inequalExpr.trailingTrivia = trailingTrivia
55-
return ConditionElementSyntax(condition: .expression(ExprSyntax(inequalExpr)))
55+
56+
var result = node
57+
result.condition = .expression(ExprSyntax(inequalExpr))
58+
return result
5659
default:
5760
return node
5861
}

Tests/SwiftFormatTests/Rules/UseExplicitNilCheckInConditionsTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,20 @@ final class UseExplicitNilCheckInConditionsTests: LintOrFormatRuleTestCase {
7979
]
8080
)
8181
}
82+
83+
func testDoNotDropTrailingCommaInConditionList() {
84+
assertFormatting(
85+
UseExplicitNilCheckInConditions.self,
86+
input: """
87+
if 1️⃣let _ = x, 2️⃣let _ = y {}
88+
""",
89+
expected: """
90+
if x != nil, y != nil {}
91+
""",
92+
findings: [
93+
FindingSpec("1️⃣", message: "compare this value using `!= nil` instead of binding and discarding it"),
94+
FindingSpec("2️⃣", message: "compare this value using `!= nil` instead of binding and discarding it"),
95+
]
96+
)
97+
}
8298
}

0 commit comments

Comments
 (0)