Convert async {}.await() to withContext(DefaultDispatcher) {}
This fixes incorrect transformation related to KT-24235
This commit is contained in:
+2
-2
@@ -45,8 +45,8 @@ class DeferredResultUnusedInspection(@JvmField var standardOnly: Boolean = true)
|
||||
companion object {
|
||||
private val shortNames = setOf("async")
|
||||
|
||||
private val fqNames: Set<FqName> = shortNames.mapTo(mutableSetOf()) { FqName("kotlinx.coroutines.experimental.$it") }
|
||||
private val fqNames: Set<FqName> = shortNames.mapTo(mutableSetOf()) { FqName("$COROUTINE_PACKAGE.$it") }
|
||||
|
||||
private val deferred = FqName("kotlinx.coroutines.experimental.Deferred")
|
||||
private val deferred = FqName("$COROUTINE_PACKAGE.Deferred")
|
||||
}
|
||||
}
|
||||
+11
-7
@@ -35,8 +35,9 @@ class RedundantAsyncInspection : AbstractCallChainChecker() {
|
||||
if (defaultParent != true) return
|
||||
if (defaultContext!! && !defaultStart!!) return
|
||||
|
||||
val replacement =
|
||||
if (defaultContext!! && defaultStart!!) "kotlinx.coroutines.experimental.runBlocking" else conversion.replacement
|
||||
var replacement = conversion.replacement
|
||||
if (defaultContext!! && defaultStart!!) replacement += "($defaultAsyncArgument)"
|
||||
|
||||
val descriptor = holder.manager.createProblemDescriptor(
|
||||
expression,
|
||||
expression.firstCalleeExpression()!!.textRange.shiftRight(-expression.startOffset),
|
||||
@@ -51,13 +52,16 @@ class RedundantAsyncInspection : AbstractCallChainChecker() {
|
||||
private val conversionGroups = conversions.group()
|
||||
|
||||
companion object {
|
||||
|
||||
private val conversions = listOf(
|
||||
Conversion(
|
||||
"kotlinx.coroutines.experimental.async",
|
||||
"kotlinx.coroutines.experimental.Deferred.await",
|
||||
"kotlinx.coroutines.experimental.withContext"
|
||||
"$COROUTINE_PACKAGE.async",
|
||||
"$COROUTINE_PACKAGE.Deferred.await",
|
||||
"$COROUTINE_PACKAGE.withContext"
|
||||
)
|
||||
)
|
||||
|
||||
private val defaultAsyncArgument = "$COROUTINE_PACKAGE.DefaultDispatcher"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal const val COROUTINE_PACKAGE = "kotlinx.coroutines.experimental"
|
||||
|
||||
@@ -8,7 +8,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -20,7 +20,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -29,7 +29,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
|
||||
@@ -8,7 +8,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -20,7 +20,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -29,7 +29,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
|
||||
@@ -8,7 +8,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -20,7 +20,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -29,7 +29,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
|
||||
@@ -8,7 +8,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -20,7 +20,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -29,7 +29,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
@@ -44,5 +44,5 @@ suspend fun <T> withContext(
|
||||
}
|
||||
|
||||
suspend fun test() {
|
||||
runBlocking { 42 }
|
||||
withContext(DefaultDispatcher) { 42 }
|
||||
}
|
||||
@@ -9,7 +9,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -21,7 +21,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -30,7 +30,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
|
||||
+3
-3
@@ -8,7 +8,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -20,7 +20,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -29,7 +29,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
|
||||
+3
-3
@@ -8,7 +8,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -20,7 +20,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -29,7 +29,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
|
||||
+3
-3
@@ -9,7 +9,7 @@ interface Deferred<T> {
|
||||
|
||||
interface CoroutineContext
|
||||
|
||||
object DefaultContext : CoroutineContext
|
||||
object DefaultDispatcher : CoroutineContext
|
||||
|
||||
enum class CoroutineStart {
|
||||
DEFAULT,
|
||||
@@ -21,7 +21,7 @@ enum class CoroutineStart {
|
||||
interface Job
|
||||
|
||||
fun <T> async(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
parent: Job? = null,
|
||||
f: suspend () -> T
|
||||
@@ -30,7 +30,7 @@ fun <T> async(
|
||||
}
|
||||
|
||||
fun <T> runBlocking(
|
||||
context: CoroutineContext = DefaultContext,
|
||||
context: CoroutineContext = DefaultDispatcher,
|
||||
f: suspend () -> T
|
||||
) {
|
||||
TODO()
|
||||
|
||||
Reference in New Issue
Block a user