A dispatcher is named in place, which tests cannot replace
What it finds
withContext(Dispatchers.IO) fixes the threads the code runs on. Passing the dispatcher in, as a constructor parameter or property, lets tests run the code on a test dispatcher and keeps the choice in one place.
Reported for Dispatchers.IO, Dispatchers.Default and Dispatchers.Unconfined as the context of withContext, launch, async, flowOn and CoroutineScope.
From the rule's source, hardcoded_dispatcher.rs.
import kotlinx.coroutines.* class Store(private val io: CoroutineDispatcher) { suspend fun read() = withContext(Dispatchers.IO) { load() }tx0 reports: Dispatchers.IO is fixed here suspend fun write() = withContext(io) { save() } fun start(scope: CoroutineScope) = scope.launch(Dispatchers.Default) { work() }tx0 reports: Dispatchers.Default is fixed here fun ui(scope: CoroutineScope) = scope.launch(Dispatchers.Main) { show() }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:hardcoded-dispatcher" = "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:hardcoded-dispatcher] reason="..."tx0 explain tx0/kotlin:hardcoded-dispatcher shows the same in the terminal. Found a false positive? Report it.