Fix issue where redundantVoidReturnType rule accidentally removed closure type (#2153)

This commit is contained in:
Cal Stephens
2025-07-16 20:24:14 -07:00
parent 5bd55188f1
commit e73722e30f
2 changed files with 21 additions and 0 deletions
@@ -32,6 +32,11 @@ public extension FormatRule {
guard let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: endIndex) else { return }
// Ensure that `-> ()` isn't actually a closure, like `-> () -> Void`.
guard !formatter.isStartOfClosureType(at: startIndex) else {
return
}
let isInProtocol = nextToken == .endOfScope("}") || (nextToken.isKeywordOrAttribute && nextToken != .keyword("in"))
// After a `Void` we could see the start of a function's body, or if the function is inside a protocol declaration
@@ -112,4 +112,20 @@ class RedundantVoidReturnTypeTests: XCTestCase {
"""
testFormatting(for: input, output, rule: .redundantVoidReturnType)
}
func testNoRemoveThrowingClosureVoidReturnType() {
// https://github.com/nicklockwood/SwiftFormat/issues/1978
let input = "func foo(bar: Bar) -> () throws -> Void"
testFormatting(for: input, rule: .redundantVoidReturnType)
}
func testNoRemoveClosureVoidReturnType() {
let input = "func foo(bar: Bar) -> () -> Void"
testFormatting(for: input, rule: .redundantVoidReturnType)
}
func testNoRemoveAsyncClosureVoidReturnType() {
let input = "func foo(bar: Bar) -> () async -> Void"
testFormatting(for: input, rule: .redundantVoidReturnType)
}
}