报告在挂起上下文中可能存在问题的隐式 CoroutineScope 接收器访问。
当一个挂起函数或 Lambda 从外部上下文捕获隐式 CoroutineScope 接收器时,可能会导致意外行为。
这是因为捕获的作用域在挂起函数仍在运行时可能会被取消或完成。
此检查可以检测代码在挂起函数或 Lambda 中隐式访问 CoroutineScope 接收器的情况,
隐式访问会创建对外部作用域的潜在危险依赖。
示例:
class MyClass {
fun CoroutineScope.launchJobs() {
launch { // 可以:直接在扩展函数中使用
doSomething()
}
suspendingFunction { // 警告:可疑的隐式 'CoroutineScope' 接收器访问
launch { // launch 调用使用外部上下文中的隐式 CoroutineScope
doSomethingElse()
}
}
}
}
选项:
要解决此问题,您可以:
this@receiverLabel.method() 明确指定接收器coroutineScope { } 构建器创建一个与挂起函数的生命周期绑定的子作用域