kotlin:S6312 raises on suspend extensions of CoroutineScope subtypes like Ktor’s ApplicationCall

  • What language is this for?
    Kotlin

  • Which rule?
    kotlin:S6312 - Extension functions on CoroutineScope should not be suspending

  • Why do you believe it’s a false-positive?

    The rule targets functions that start background coroutines in the receiver scope, which “should not be declared suspend, as suspending functions should not leave running background tasks behind”. However, the check raises on any suspending extension whose receiver has CoroutineScope among its supertypes.

    Some framework types implement CoroutineScope incidentally. Ktor’s ApplicationCall is the most common: suspending extensions on it are the idiomatic way to write request helpers, and Ktor itself declares its API this way (suspend fun ApplicationCall.respondText(...), respondRedirect(...), see io.ktor.server.response). These helpers only await the response and start no coroutine. The same applies to WebSocketSession (Ktor declares suspend fun WebSocketSession.send(content: String)).

    ApplicationCall did not extend CoroutineScope in Ktor 2.x, so the same code becomes noncompliant just by upgrading to Ktor 3.

    Suggestion: raise only when the declared receiver type is CoroutineScope itself, or when the body uses the receiver as a scope (implicit launch/async/produce…). Excluding known types such as io.ktor.server.application.ApplicationCall would also work, but would never be complete.

  • Are you using:

    • SonarQube Community Build 26.10.0.133078
    • SonarScanner for Gradle 7.5.0.8588
    • Kotlin 2.4.21, Ktor 3.6.0
  • How can we reproduce the problem?

    Dependency: implementation("io.ktor:ktor-server-core:3.6.0")

    import io.ktor.http.ContentType
    import io.ktor.http.HttpStatusCode
    import io.ktor.server.application.ApplicationCall
    import io.ktor.server.response.respondText
    
    // Raised, although it has the same shape as Ktor's respondText and starts no coroutine.
    suspend fun ApplicationCall.respondJson(json: String, status: HttpStatusCode = HttpStatusCode.OK) =
        respondText(json, ContentType.Application.Json, status)
    

    For contrast, this one matches the rule’s rationale and should keep being raised:

    import kotlinx.coroutines.launch
    
    suspend fun ApplicationCall.fireAndForget() {
        launch { /* ... */ }
    }