A coroutine is started in a scope created on the spot, which nothing keeps to cancel
What it finds
CoroutineScope(Dispatchers.IO).launch { ... } creates a scope and drops it at once, so the coroutine is as unowned as one in GlobalScope: nothing cancels it or waits for it. A scope is meant to be kept and cancelled when the work it owns ends.
From the rule's source, unscoped_coroutine_scope.rs.
import kotlinx.coroutines.* class Sync { private val scope = CoroutineScope(Dispatchers.IO) fun start() { CoroutineScope(Dispatchers.IO).launch { work() }tx0 reports: launch runs in a scope that is created here MainScope().async { work() }tx0 reports: async runs in a scope that is created here CoroutineScope(coroutineContext).launch { work() } scope.launch { work() } }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:throwaway-coroutine-scope" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:throwaway-coroutine-scope] reason="..."tx0 explain tx0/kotlin:throwaway-coroutine-scope shows the same in the terminal. Found a false positive? Report it.