File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -13,5 +13,19 @@ import SwiftSyntax
13
13
///
14
14
/// - SeeAlso: https://google.github.io/swift#types-with-shorthand-names
15
15
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
+ }
16
28
29
+ extension Diagnostic . Message {
30
+ static let returnVoid = Diagnostic . Message ( . warning, " replace '()' with 'Void' " )
17
31
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments