Skip to content

Commit ced4b4e

Browse files
LaurenWhiteallevato
authored andcommitted
Implement return void instead of empty tuple (swiftlang#60)
1 parent 802d616 commit ced4b4e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tools/swift-format/Sources/Rules/ReturnVoidInsteadOfEmptyTuple.swift

+14
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,19 @@ import SwiftSyntax
1313
///
1414
/// - SeeAlso: https://google.github.io/swift#types-with-shorthand-names
1515
public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
16+
public override func visit(_ node: FunctionTypeSyntax) -> TypeSyntax {
17+
guard let returnType = node.returnType as? TupleTypeSyntax,
18+
returnType.elements.count == 0 else { return node }
19+
diagnose(.returnVoid, on: node.returnType)
20+
let voidKeyword = SyntaxFactory.makeSimpleTypeIdentifier(
21+
name: SyntaxFactory.makeIdentifier(
22+
"Void",
23+
trailingTrivia: returnType.rightParen.trailingTrivia),
24+
genericArgumentClause: nil)
25+
return node.withReturnType(voidKeyword)
26+
}
27+
}
1628

29+
extension Diagnostic.Message {
30+
static let returnVoid = Diagnostic.Message(.warning, "replace '()' with 'Void'")
1731
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Foundation
2+
import XCTest
3+
import SwiftSyntax
4+
5+
@testable import Rules
6+
7+
public class ReturnVoidInsteadOfEmptyTupleTests: DiagnosingTestCase {
8+
public func testEmptyTupleReturns() {
9+
XCTAssertFormatting(
10+
ReturnVoidInsteadOfEmptyTuple.self,
11+
input: """
12+
let callback: () -> ()
13+
typealias x = Int -> ()
14+
func y() -> Int -> () { return }
15+
func z(d: Bool -> ()) {}
16+
""",
17+
expected: """
18+
let callback: () -> Void
19+
typealias x = Int -> Void
20+
func y() -> Int -> Void { return }
21+
func z(d: Bool -> Void) {}
22+
""")
23+
}
24+
25+
#if !os(macOS)
26+
static let allTests = [
27+
ReturnVoidInsteadOfEmptyTupleTests.testEmptyTupleReturns,
28+
]
29+
#endif
30+
}

0 commit comments

Comments
 (0)