diff --git a/SwiftFormat/Rules.swift b/SwiftFormat/Rules.swift index a5e91985d..28871bf81 100644 --- a/SwiftFormat/Rules.swift +++ b/SwiftFormat/Rules.swift @@ -1329,7 +1329,8 @@ public func void(_ formatter: Formatter) { formatter.tokenAtIndex(prevIndex) == .startOfScope("("), let nextIndex = formatter.indexOfNextToken(fromIndex: i, matching: { !$0.isWhitespaceOrLinebreak }), formatter.tokenAtIndex(nextIndex) == .endOfScope(")") { - if formatter.nextNonWhitespaceOrCommentOrLinebreakToken(fromIndex: nextIndex) == .symbol("->") { + if let nextToken = formatter.nextNonWhitespaceOrCommentOrLinebreakToken(fromIndex: nextIndex), + [.symbol("->"), .keyword("throws"), .keyword("rethrows")].contains(nextToken) { // Remove Void formatter.removeTokensInRange(prevIndex + 1 ..< nextIndex) } else if formatter.options.useVoid { diff --git a/SwiftFormatTests/RulesTests.swift b/SwiftFormatTests/RulesTests.swift index 5eb52ebfe..3c7363282 100644 --- a/SwiftFormatTests/RulesTests.swift +++ b/SwiftFormatTests/RulesTests.swift @@ -2255,6 +2255,13 @@ class RulesTests: XCTestCase { XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n") } + func testVoidThrowsIsNotMangled() { + let input = "(Void) throws -> Void" + let output = "() throws -> Void" + XCTAssertEqual(try! format(input, rules: [void]), output) + XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n") + } + // MARK: useVoid = false func testUseVoidOptionFalse() {