|
| 1 | +// |
| 2 | +// RedundantThrows.swift |
| 3 | +// SwiftFormat |
| 4 | +// |
| 5 | +// Created by Cal Stephens on 2025-09-16. |
| 6 | +// Copyright © 2024 Nick Lockwood. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +public extension FormatRule { |
| 12 | + static let redundantThrows = FormatRule( |
| 13 | + help: "Remove redundant `throws` keyword from function declarations that don't throw any errors.", |
| 14 | + orderAfter: [.noForceUnwrapInTests, .noForceTryInTests, .noGuardInTests, .throwingTests], |
| 15 | + options: ["redundant-throws"] |
| 16 | + ) { formatter in |
| 17 | + let testFramework = formatter.detectTestingFramework() |
| 18 | + if formatter.options.redundantThrows == .testsOnly, testFramework == nil { |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + formatter.forEach(.keyword) { keywordIndex, keyword in |
| 23 | + guard ["func", "init", "subscript"].contains(keyword.string), |
| 24 | + let functionDecl = formatter.parseFunctionDeclaration(keywordIndex: keywordIndex), |
| 25 | + functionDecl.effects.contains(where: { $0.hasPrefix("throws") }), |
| 26 | + let bodyRange = functionDecl.bodyRange |
| 27 | + else { return } |
| 28 | + |
| 29 | + // Don't modify override functions - they need to match their parent's signature |
| 30 | + if formatter.modifiersForDeclaration(at: keywordIndex, contains: "override") { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + if formatter.options.redundantThrows == .testsOnly { |
| 35 | + // Only process test functions |
| 36 | + guard let testFramework, functionDecl.returnType == nil else { return } |
| 37 | + |
| 38 | + switch testFramework { |
| 39 | + case .xcTest: |
| 40 | + guard functionDecl.name?.starts(with: "test") == true else { return } |
| 41 | + case .swiftTesting: |
| 42 | + guard formatter.modifiersForDeclaration(at: keywordIndex, contains: "@Test") else { return } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Check if the function body contains any try keywords (excluding try! and try?) or throw statements |
| 47 | + var bodyContainsThrowingCode = false |
| 48 | + for index in bodyRange { |
| 49 | + if formatter.tokens[index] == .keyword("try") { |
| 50 | + // Check if this try is followed by ! or ? (which means it doesn't need throws) |
| 51 | + if let nextTokenIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: index), |
| 52 | + formatter.tokens[nextTokenIndex].isUnwrapOperator |
| 53 | + { |
| 54 | + continue // Skip try! and try? |
| 55 | + } |
| 56 | + |
| 57 | + // Only count try keywords that are directly in this function's body |
| 58 | + // (not in nested closures or functions) |
| 59 | + if formatter.isInFunctionBody(of: functionDecl, at: index) { |
| 60 | + bodyContainsThrowingCode = true |
| 61 | + break |
| 62 | + } |
| 63 | + } else if formatter.tokens[index] == .keyword("throw") { |
| 64 | + // Only count throw statements that are directly in this function's body |
| 65 | + // (not in nested closures or functions) |
| 66 | + if formatter.isInFunctionBody(of: functionDecl, at: index) { |
| 67 | + bodyContainsThrowingCode = true |
| 68 | + break |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // If the body doesn't contain any throwing code, remove the throws |
| 74 | + if !bodyContainsThrowingCode { |
| 75 | + guard let effectsRange = functionDecl.effectsRange else { return } |
| 76 | + |
| 77 | + // Find the throws keyword in the effects range |
| 78 | + for index in effectsRange { |
| 79 | + if formatter.tokens[index] == .keyword("throws") { |
| 80 | + var endIndex = index |
| 81 | + |
| 82 | + // Check if there's typed throws (throws(...)) |
| 83 | + if let nextTokenIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: index), |
| 84 | + formatter.tokens[nextTokenIndex] == .startOfScope("("), |
| 85 | + let endOfScope = formatter.endOfScope(at: nextTokenIndex) |
| 86 | + { |
| 87 | + endIndex = endOfScope |
| 88 | + } |
| 89 | + |
| 90 | + // Include trailing whitespace if present |
| 91 | + if endIndex + 1 < formatter.tokens.count, |
| 92 | + formatter.tokens[endIndex + 1].isSpace |
| 93 | + { |
| 94 | + endIndex += 1 |
| 95 | + } |
| 96 | + |
| 97 | + formatter.removeTokens(in: index ... endIndex) |
| 98 | + break // Only remove the first throws found |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } examples: { |
| 104 | + """ |
| 105 | + ```diff |
| 106 | + // With --redundant-throws tests-only (default) |
| 107 | + import Testing |
| 108 | +
|
| 109 | + - @Test func myFeature() throws { |
| 110 | + + @Test func myFeature() throws { |
| 111 | + #expect(foo == 1) |
| 112 | + } |
| 113 | +
|
| 114 | + import XCTest |
| 115 | +
|
| 116 | + class TestCase: XCTestCase { |
| 117 | + - func testMyFeature() throws { |
| 118 | + + func testMyFeature() { |
| 119 | + XCTAssertEqual(foo, 1) |
| 120 | + } |
| 121 | + } |
| 122 | + ``` |
| 123 | +
|
| 124 | + Also supports `--redundant-throws always`. |
| 125 | + This will cause warnings anywhere the updated method is called with `try`, since `try` is now redundant at the callsite. |
| 126 | +
|
| 127 | + ```diff |
| 128 | + // With --redundant-throws always |
| 129 | + - func myNonThrowingMethod() throws -> Int { |
| 130 | + + func myNonThrowingMethod() -> Int { |
| 131 | + return 0 |
| 132 | + } |
| 133 | +
|
| 134 | + // Possibly elsewhere in codebase: |
| 135 | + let value = try myNonThrowingMethod() |
| 136 | + + `- warning: no calls to throwing functions occur within 'try' expression |
| 137 | + ``` |
| 138 | + """ |
| 139 | + } |
| 140 | +} |
0 commit comments