unhandled_throwing_task reports fine cases as errors
Open
Nobody has claimed this yet.
bug
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
Describe the bug
unhandled_throwing_task is output as an error even if the exception is actually caught in the Task.
Environment
- SwiftLint version (run
swiftlint versionto be sure)? 0.52.0 - Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew
- Paste your configuration file:
# insert yaml contents here
- Are you using nested configurations?
If so, paste their relative paths and respective contents.: No - Which Xcode version are you using (check
xcodebuild -version)? Xcode 14.3 (14E222b) - Do you have a sample that shows the issue? Run
echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
to quickly test if your example is really demonstrating the issue. If your example is more
complex, you can useswiftlint lint --path [file here] --no-cache --enable-all-rules.
Of course.
@Sendable func exec() async throws {
// any async method
}
@Sendable @MainActor func toOptional<T>(closure: @Sendable () async throws -> T) async -> T? {
// For example, when you want to leave only the log and ignore the error
return try? await closure()
}
Task { // Unhandled Throwing Task Violation
await toOptional(closure: { try await exec() })
}
<nopath>:8:1: error: Unhandled Throwing Task Violation: Errors thrown inside this task are not handled, which may be unexpected. Handle errors inside the task, or use `try await` to access the Tasks value and handle errors. See this forum thread for more details: https://forums.swift.org/t/task-initializer-with-throwing-closure-swallows-error/56066 (unhandled_throwing_task)
(Prints some warnings, please ignore)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the provided Swift example with SwiftLint 0.52.0 and inspect the implementation of the unhandled_throwing_task rule. Confirm that the caught error in the Task is not reported, then add or update coverage for this example and run the relevant SwiftLint tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100