Introduce "redundant async" inspection #KT-24235 Fixed

This commit is contained in:
Mikhail Glukhikh
2018-05-11 15:01:05 +03:00
parent 365c13c38e
commit 13be7803bb
19 changed files with 564 additions and 1 deletions
@@ -0,0 +1,6 @@
<html>
<body>
This inspection reports <b>async</b> call that is immediately followed by <b>await</b>.
Such a call can be replaced with a kind of blocking call.
</body>
</html>
+9
View File
@@ -2839,6 +2839,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.coroutines.RedundantAsyncInspection"
displayName="Redundant async call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -2834,6 +2834,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.coroutines.RedundantAsyncInspection"
displayName="Redundant async call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -2834,6 +2834,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.coroutines.RedundantAsyncInspection"
displayName="Redundant async call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -2840,6 +2840,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.coroutines.RedundantAsyncInspection"
displayName="Redundant async call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -2834,6 +2834,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.coroutines.RedundantAsyncInspection"
displayName="Redundant async call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -2834,6 +2834,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.coroutines.RedundantAsyncInspection"
displayName="Redundant async call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.inspections.collections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
@@ -66,7 +67,8 @@ class SimplifyCallChainFix(val newName: String) : LocalQuickFix {
argumentsText
)
secondQualifiedExpression.replaced(newQualifiedExpression)
val result = secondQualifiedExpression.replaced(newQualifiedExpression)
ShortenReferences.DEFAULT.process(result)
}
}
}
@@ -0,0 +1,63 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.inspections.coroutines
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import org.jetbrains.kotlin.idea.inspections.collections.AbstractCallChainChecker
import org.jetbrains.kotlin.idea.inspections.collections.SimplifyCallChainFix
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.qualifiedExpressionVisitor
import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument
class RedundantAsyncInspection : AbstractCallChainChecker() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) =
qualifiedExpressionVisitor(fun(expression) {
var defaultContext: Boolean? = null
var defaultStart: Boolean? = null
var defaultParent: Boolean? = null
val conversion = findQualifiedConversion(expression, conversionGroups) check@{ _, firstResolvedCall, _, _ ->
for ((parameterDescriptor, valueArgument) in firstResolvedCall.valueArguments) {
val default = valueArgument is DefaultValueArgument
when (parameterDescriptor.name.asString()) {
"context" -> defaultContext = default
"start" -> defaultStart = default
"parent" -> defaultParent = default
}
}
true
} ?: return
defaultContext ?: return
defaultStart ?: return
if (defaultParent != true) return
if (defaultContext!! && !defaultStart!!) return
val replacement =
if (defaultContext!! && defaultStart!!) "kotlinx.coroutines.experimental.runBlocking" else conversion.replacement
val descriptor = holder.manager.createProblemDescriptor(
expression,
expression.firstCalleeExpression()!!.textRange.shiftRight(-expression.startOffset),
"Redundant 'async' call may be reduced to '$replacement'",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOnTheFly,
SimplifyCallChainFix(replacement)
)
holder.registerProblem(descriptor)
})
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"
)
)
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.coroutines.RedundantAsyncInspection
@@ -0,0 +1,48 @@
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test(ctx: CoroutineContext) {
<caret>async(ctx) { 42 }.await()
}
@@ -0,0 +1,48 @@
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test(ctx: CoroutineContext) {
withContext(ctx) { 42 }
}
@@ -0,0 +1,48 @@
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test() {
<caret>async { 42 }.await()
}
@@ -0,0 +1,48 @@
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test() {
runBlocking { 42 }
}
@@ -0,0 +1,49 @@
// PROBLEM: none
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test(ctx: CoroutineContext) {
<caret>async(parent = null) { 42 }.await()
}
@@ -0,0 +1,48 @@
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test(ctx: CoroutineContext) {
<caret>async(ctx, CoroutineStart.LAZY) { 42 }.await()
}
@@ -0,0 +1,48 @@
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test(ctx: CoroutineContext) {
withContext(ctx, CoroutineStart.LAZY) { 42 }
}
@@ -0,0 +1,49 @@
// PROBLEM: none
// WITH_RUNTIME
package kotlinx.coroutines.experimental
interface Deferred<T> {
suspend fun await(): T
}
interface CoroutineContext
object DefaultContext : CoroutineContext
enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Job
fun <T> async(
context: CoroutineContext = DefaultContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
f: suspend () -> T
): Deferred<T> {
TODO()
}
fun <T> runBlocking(
context: CoroutineContext = DefaultContext,
f: suspend () -> T
) {
TODO()
}
suspend fun <T> withContext(
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
f: suspend () -> T
) {
TODO()
}
suspend fun test(ctx: CoroutineContext) {
<caret>async(start = CoroutineStart.LAZY) { 42 }.await()
}
@@ -1226,6 +1226,57 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/coroutines")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Coroutines extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInCoroutines() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/coroutines"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/inspectionsLocal/coroutines/redundantAsync")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RedundantAsync extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRedundantAsync() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/coroutines/redundantAsync"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/inspectionsLocal/coroutines/redundantAsync/simple.kt");
}
@TestMetadata("simplest.kt")
public void testSimplest() throws Exception {
runTest("idea/testData/inspectionsLocal/coroutines/redundantAsync/simplest.kt");
}
@TestMetadata("withParent.kt")
public void testWithParent() throws Exception {
runTest("idea/testData/inspectionsLocal/coroutines/redundantAsync/withParent.kt");
}
@TestMetadata("withStartAndContext.kt")
public void testWithStartAndContext() throws Exception {
runTest("idea/testData/inspectionsLocal/coroutines/redundantAsync/withStartAndContext.kt");
}
@TestMetadata("withStartNoContext.kt")
public void testWithStartNoContext() throws Exception {
runTest("idea/testData/inspectionsLocal/coroutines/redundantAsync/withStartNoContext.kt");
}
}
}
@TestMetadata("idea/testData/inspectionsLocal/deprecatedCallableAddReplaceWith")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)