mirror of
https://github.com/apple/swift.git
synced 2026-06-27 12:25:55 +02:00
bfe8dbca59
Pending swift evolution discussion: https://github.com/swiftlang/swift-evolution/pull/3133 replaces #87171 update new error message according to proposal also handle untyped throws in _nonDiscardableWhenThrowing attr Rename the internal attr _nonDiscardableWhenThrowingOperation
1.4 KiB
1.4 KiB
Unused throwing unstructured task creation (NoUseUnstructuredThrowingTask)
Overview
Creating a throwing unstructured Task without storing or discarding its result means any error thrown
by the task body will be silently ignored. Resolve this warning by explicitly storing or discarding the
task reference.
For example:
func example() {
Task { throw MyError() }
}
Building this code produces a warning about the unused unstructured task:
| func example() {
| Task { throw MyError() }
| `- warning: unstructured throwing task created by 'init(name:priority:operation:)' is not used, which may accidentally ignore errors thrown inside the task
| note: to silence this warning, handle the error inside the task, or store/discard the task value explicitly
| }
By storing the task reference and awaiting it, you won't miss the error thrown by the task operation because awaiting task.value will be forced to acknowledge the thrown error with try:
let task = Task { throw MyError() }
try await task.value
Alternatively, ignore the thrown error explicitly:
_ = Task { throw MyError() }
Cancelling unstructured tasks
Another reason to keep references to unstructured tasks is to be able to cancel them explicitly:
let task = Task { try Task.sleep(for: .seconds(10)) }
task.cancel() // cancel and wake-up the sleeping task
try await task.value