mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Fix issue where redundantVoidReturnType rule accidentally removed closure type (#2153)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user