Unhandled Throwing Task

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

  • Identifier: unhandled_throwing_task
  • Enabled by default: No
  • Supports autocorrection: No
  • Kind: lint
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity error

Non Triggering Examples

Task<Void, Never> {
  try await myThrowingFunction()
}
Task {
  try? await myThrowingFunction()
}
Task {
  try! await myThrowingFunction()
}
Task<Void, String> {
  let text = try myThrowingFunction()
  return text
}
Task {
  do {
    try myThrowingFunction()
  } catch let e {
    print(e)
  }
}
func someFunction() throws {
  Task {
    anotherFunction()
    do {
      try myThrowingFunction()
    } catch {
      print(error)
    }
  }

  try something()
}
let task = Task {
  try await myThrowingFunction()
}
var task = Task {
  try await myThrowingFunction()
}
try await Task {
  try await myThrowingFunction()
}.value
executor.task = Task {
  try await isolatedOpen(.init(executor.asUnownedSerialExecutor()))
}
let result = await Task {
  throw CancellationError()
}.result
func makeTask() -> Task<String, Error> {
  return Task {
    try await someThrowingFunction()
  }
}
func makeTask() -> Task<String, Error> {
  // Implicit return
  Task {
    try await someThrowingFunction()
  }
}
Task {
  return Result {
      try someThrowingFunc()
  }
}

Triggering Examples

Task {
  try await myThrowingFunction()
}
Task {
  let text = try myThrowingFunction()
  return text
}
Task {
  do {
    try myThrowingFunction()
  }
}
Task {
  do {
    try myThrowingFunction()
  } catch let e as FooError {
    print(e)
  }
}
Task {
  do {
    throw FooError.bar
  }
}
Task {
  throw FooError.bar
}
Task<_, _> {
  throw FooError.bar
}
Task<Void,_> {
  throw FooError.bar
}
Task {
  do {
    try foo()
  } catch {
    try bar()
  }
}
Task {
  do {
    try foo()
  } catch {
    throw BarError()
  }
}
func doTask() {
  Task {
    try await someThrowingFunction()
  }
}