A flow builder emits from inside withContext, which the flow fails on at run time
What it finds
A flow must emit from the coroutine that collects it. flow { withContext(Dispatchers.IO) { emit(load()) } } emits from another context, and the flow throws IllegalStateException the first time it runs. flowOn changes the context of the upstream work instead.
Reported for a call to emit or emitAll in the lambda of a withContext that is itself in the lambda of flow.
From the rule's source, emit_from_other_context.rs.
import kotlinx.coroutines.*import kotlinx.coroutines.flow.* fun numbers() = flow { withContext(Dispatchers.IO) { emit(load())tx0 reports: a flow emits from inside withContext } val value = withContext(Dispatchers.IO) { load() } emit(value)} fun moved() = flow { emit(load()) }.flowOn(Dispatchers.IO) fun channel() = channelFlow { withContext(Dispatchers.IO) { send(load()) }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/kotlin:flow-emits-from-other-context" = "off"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:flow-emits-from-other-context] reason="..."tx0 explain tx0/kotlin:flow-emits-from-other-context shows the same in the terminal. Found a false positive? Report it.