Skip to content

Commit c0a2778

Browse files
committed
Update ReturnVoidInsteadOfEmptyTuple Rule
The new parser will attach the unexpected text here as trailing trivia. Update this rule to account for that. The new parser also fixes a regression in this test and diagnoses () -> (\u{feff}) without replacing it
1 parent ed8f946 commit c0a2778

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Sources/SwiftFormatRules/ReturnVoidInsteadOfEmptyTuple.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
3434
// If the user has put non-whitespace trivia inside the empty tuple, like a comment, then we
3535
// still diagnose it as a lint error but we don't replace it because it's not obvious where the
3636
// comment should go.
37-
if hasNonWhitespaceLeadingTrivia(returnType.rightParen) {
37+
if hasNonWhitespaceTrivia(returnType.leftParen, at: .trailing)
38+
|| hasNonWhitespaceTrivia(returnType.rightParen, at: .leading) {
3839
return super.visit(node)
3940
}
4041

@@ -58,7 +59,8 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
5859
// If the user has put non-whitespace trivia inside the empty tuple, like a comment, then we
5960
// still diagnose it as a lint error but we don't replace it because it's not obvious where the
6061
// comment should go.
61-
if hasNonWhitespaceLeadingTrivia(returnType.rightParen) {
62+
if hasNonWhitespaceTrivia(returnType.leftParen, at: .trailing)
63+
|| hasNonWhitespaceTrivia(returnType.rightParen, at: .leading) {
6264
return super.visit(node)
6365
}
6466

@@ -79,8 +81,8 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
7981

8082
/// Returns a value indicating whether the leading trivia of the given token contained any
8183
/// non-whitespace pieces.
82-
private func hasNonWhitespaceLeadingTrivia(_ token: TokenSyntax) -> Bool {
83-
for piece in token.leadingTrivia {
84+
private func hasNonWhitespaceTrivia(_ token: TokenSyntax, at position: TriviaPosition) -> Bool {
85+
for piece in position == .leading ? token.leadingTrivia : token.trailingTrivia {
8486
switch piece {
8587
case .blockComment, .docBlockComment, .docLineComment, .unexpectedText, .lineComment,
8688
.shebang:

Tests/SwiftFormatRulesTests/ReturnVoidInsteadOfEmptyTupleTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ final class ReturnVoidInsteadOfEmptyTupleTests: LintOrFormatRuleTestCase {
153153
XCTAssertDiagnosed(.returnVoid, line: 6, column: 21)
154154
XCTAssertDiagnosed(.returnVoid, line: 7, column: 21)
155155
XCTAssertDiagnosed(.returnVoid, line: 10, column: 21)
156+
XCTAssertDiagnosed(.returnVoid, line: 13, column: 21)
156157
XCTAssertDiagnosed(.returnVoid, line: 15, column: 22)
157158
XCTAssertDiagnosed(.returnVoid, line: 15, column: 29)
158159
XCTAssertDiagnosed(.returnVoid, line: 16, column: 44)

0 commit comments

Comments
 (0)