Minor: Extract suspend call check to a separate function

This commit is contained in:
Yan Zhulanow
2020-08-06 18:34:01 +09:00
parent ef5335ba99
commit ed2b547973
@@ -13,17 +13,19 @@ import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.qualifiedExpressionVisitor
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
class SimplifiableCallChainInspection : AbstractCallChainChecker() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) =
qualifiedExpressionVisitor(fun(expression) {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): KtVisitorVoid {
return qualifiedExpressionVisitor(fun(expression) {
val conversion = findQualifiedConversion(expression, conversionGroups) check@{ conversion, firstResolvedCall, _, context ->
// Do not apply on maps due to lack of relevant stdlib functions
val firstReceiverType = firstResolvedCall.extensionReceiver?.type
@@ -40,13 +42,8 @@ class SimplifiableCallChainInspection : AbstractCallChainChecker() {
}
) return@check false
}
if (!conversion.enableSuspendFunctionCall) {
val isCallingSuspendFunction = firstResolvedCall.call.callElement.anyDescendantOfType<KtCallExpression> {
it.getResolvedCall(context)?.resultingDescriptor?.isSuspend == true
}
if (isCallingSuspendFunction) return@check false
}
true
return@check conversion.enableSuspendFunctionCall || !containsSuspendFunctionCall(firstResolvedCall, context)
} ?: return
val replacement = conversion.replacement
@@ -69,6 +66,13 @@ class SimplifiableCallChainInspection : AbstractCallChainChecker() {
)
holder.registerProblem(descriptor)
})
}
private fun containsSuspendFunctionCall(resolvedCall: ResolvedCall<*>, context: BindingContext): Boolean {
return resolvedCall.call.callElement.anyDescendantOfType<KtCallExpression> {
it.getResolvedCall(context)?.resultingDescriptor?.isSuspend == true
}
}
private val conversionGroups = conversions.group()