Minor: Extract suspend call check to a separate function
This commit is contained in:
+13
-9
@@ -13,17 +13,19 @@ import org.jetbrains.kotlin.idea.KotlinBundle
|
|||||||
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
|
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
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.anyDescendantOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.psi.qualifiedExpressionVisitor
|
import org.jetbrains.kotlin.psi.qualifiedExpressionVisitor
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
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.builtIns
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||||
|
|
||||||
class SimplifiableCallChainInspection : AbstractCallChainChecker() {
|
class SimplifiableCallChainInspection : AbstractCallChainChecker() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) =
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): KtVisitorVoid {
|
||||||
qualifiedExpressionVisitor(fun(expression) {
|
return qualifiedExpressionVisitor(fun(expression) {
|
||||||
val conversion = findQualifiedConversion(expression, conversionGroups) check@{ conversion, firstResolvedCall, _, context ->
|
val conversion = findQualifiedConversion(expression, conversionGroups) check@{ conversion, firstResolvedCall, _, context ->
|
||||||
// Do not apply on maps due to lack of relevant stdlib functions
|
// Do not apply on maps due to lack of relevant stdlib functions
|
||||||
val firstReceiverType = firstResolvedCall.extensionReceiver?.type
|
val firstReceiverType = firstResolvedCall.extensionReceiver?.type
|
||||||
@@ -40,13 +42,8 @@ class SimplifiableCallChainInspection : AbstractCallChainChecker() {
|
|||||||
}
|
}
|
||||||
) return@check false
|
) return@check false
|
||||||
}
|
}
|
||||||
if (!conversion.enableSuspendFunctionCall) {
|
|
||||||
val isCallingSuspendFunction = firstResolvedCall.call.callElement.anyDescendantOfType<KtCallExpression> {
|
return@check conversion.enableSuspendFunctionCall || !containsSuspendFunctionCall(firstResolvedCall, context)
|
||||||
it.getResolvedCall(context)?.resultingDescriptor?.isSuspend == true
|
|
||||||
}
|
|
||||||
if (isCallingSuspendFunction) return@check false
|
|
||||||
}
|
|
||||||
true
|
|
||||||
} ?: return
|
} ?: return
|
||||||
|
|
||||||
val replacement = conversion.replacement
|
val replacement = conversion.replacement
|
||||||
@@ -69,6 +66,13 @@ class SimplifiableCallChainInspection : AbstractCallChainChecker() {
|
|||||||
)
|
)
|
||||||
holder.registerProblem(descriptor)
|
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()
|
private val conversionGroups = conversions.group()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user