KT.SUSPEND_FUN_WITH_COROUTINE_SCOPE_RECEIVER

Functions that use CoroutineScope as receive are marked as suspend

Suspend functions that use CoroutineScope as receiver should not be marked as suspend. A CoroutineScope provides structured concurrency via its coroutineContext. A suspend function also has its own coroutineContext, which is now ambiguous and mixed with the receiver`s.

Noncompliant Code

Copy
suspend fun CoroutineScope.foo() {
  launch {
    delay(1.seconds)
  }
}

Compliant Code

Copy
fun CoroutineScope.foo() {
    launch {
      delay(1.seconds)
    }
}

// Alternative
suspend fun foo() = coroutineScope {
    launch {
      delay(1.seconds)
    }
}

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