KT.INJECT_DISPATCHER

Hardcode dispatchers

Always use dependency injection to inject dispatchers for easier testing. This rule is based on the recommendation https://developer.android.com/kotlin/coroutines/coroutines-best-practices#inject-dispatchers

Noncompliant Code

Copy
fun myFunc() {
    coroutineScope(Dispatchers.IO)
}

Compliant Code

Copy
fun myFunc(dispatcher: CoroutineDispatcher = Dispatchers.IO) {
    coroutineScope(dispatcher)
}

class MyRepository(dispatchers: CoroutineDispatcher = Dispatchers.IO)

Options

  • dispatcherNames (default: ['io', 'default', 'unconfined'])

    The names of dispatchers to detect by this rule

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html