ITADN

`unneeded_throws_rethrows` false positive when `try` is in parameter in a function with trailing closure

#6491OpenNef10 创建于 2026-02-13
bug
N
Nef10commented
### New Issue Checklist - [x] I've Updated SwiftLint to the latest version - [x] I've searched for [existing GitHub issues](https://github.com/realm/SwiftLint/issues). ### Bug Description A clear and concise description of what the bug is. Ideally, provide a small (but compilable) example code snippet that can be used to reproduce the issue. ```swift import Foundation func a() throws -> String { throw NSError(domain: "a", code: 1, userInfo: nil) } func b(text: String, completion: () -> Void) { print(text) completion() } func c() throws { // Violation on the throw here b(text: try a()) { print("") } } ``` However, it is required by the compiler. Without the throw: ```bash $ swift a.swift a.swift:13:13: error: errors thrown from here are not handled 11 | 12 | func c() { 13 | b(text: try a()) { | `- error: errors thrown from here are not handled 14 | print("") 15 | } ``` Without the closure, no violation is reported: ```swift import Foundation func a() throws -> String { throw NSError(domain: "a", code: 1, userInfo: nil) } func b(text: String) { print(text) } func c() throws { // All good b(text: try a()) } ``` ```bash $ swiftlint lint --only-rule unneeded_throws_rethrows --no-cache ./a.swift Linting Swift files at paths ./a.swift Linting 'a.swift' (1/1) /Users/steffen/Desktop/a.swift:12:10: warning: Unneeded (Re)Throws Keyword Violation: Superfluous 'throws'; body of this function does not throw any error (unneeded_throws_rethrows) Done linting! Found 1 violation, 0 serious in 1 file. ``` ### Environment * SwiftLint version (run `swiftlint version` to be sure) 0.63.2 * Xcode version (run `xcodebuild -version` to be sure) Xcode 26.2 Build version 17C52 * Installation method used (Homebrew, CocoaPods, building from source, etc) Homebrew * Are you using [nested configurations](https://github.com/realm/SwiftLint#nested-configurations)? No
0 条评论