Report warning on "suspend {}"-like calls based on variable

#KT-22892 Fixed
This commit is contained in:
Denis Zharkov
2018-08-24 16:20:17 +03:00
parent b9f141d4aa
commit 272ee252d8
2 changed files with 16 additions and 6 deletions
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.resolve.calls.callResolverUtil.isInfixCall import org.jetbrains.kotlin.resolve.calls.callResolverUtil.isInfixCall
import org.jetbrains.kotlin.resolve.calls.callUtil.isCallableReference import org.jetbrains.kotlin.resolve.calls.callUtil.isCallableReference
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.serialization.deserialization.KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME import org.jetbrains.kotlin.serialization.deserialization.KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -23,18 +24,19 @@ object LambdaWithSuspendModifierCallChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) { override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val descriptor = resolvedCall.candidateDescriptor val descriptor = resolvedCall.candidateDescriptor
val call = resolvedCall.call val call = resolvedCall.call
val callName = call.referencedName() val calleeName = call.referencedName()
val variableCalleeName = resolvedCall.safeAs<VariableAsFunctionResolvedCall>()?.variableCall?.call?.referencedName()
if (callName != "suspend" && descriptor.name.asString() != "suspend") return if (calleeName != "suspend" && variableCalleeName != "suspend" && descriptor.name.asString() != "suspend") return
when (descriptor.fqNameOrNull()) { when (descriptor.fqNameOrNull()) {
KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME -> { KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME -> {
if (!call.hasFormOfSuspendModifierForLambda() || call.explicitReceiver != null) { if (calleeName != "suspend" || !call.hasFormOfSuspendModifierForLambda() || call.explicitReceiver != null) {
context.trace.report(Errors.NON_MODIFIER_FORM_FOR_BUILT_IN_SUSPEND.on(reportOn)) context.trace.report(Errors.NON_MODIFIER_FORM_FOR_BUILT_IN_SUSPEND.on(reportOn))
} }
} }
else -> { else -> {
if (call.hasFormOfSuspendModifierForLambda()) { if ((calleeName == "suspend" || variableCalleeName == "suspend") && call.hasFormOfSuspendModifierForLambda()) {
context.trace.report(Errors.MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND.on(reportOn)) context.trace.report(Errors.MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND.on(reportOn))
} }
} }
@@ -42,8 +44,7 @@ object LambdaWithSuspendModifierCallChecker : CallChecker {
} }
private fun Call.hasFormOfSuspendModifierForLambda() = private fun Call.hasFormOfSuspendModifierForLambda() =
referencedName() == "suspend" !isCallableReference()
&& !isCallableReference()
&& typeArguments.isEmpty() && typeArguments.isEmpty()
&& (hasNoArgumentListButDanglingLambdas() || isInfixWithRightLambda()) && (hasNoArgumentListButDanglingLambdas() || isInfixWithRightLambda())
@@ -34,3 +34,12 @@ fun bar() {
@Target(AnnotationTarget.EXPRESSION) @Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE) @Retention(AnnotationRetention.SOURCE)
annotation class Ann annotation class Ann
fun main(suspend: WLambdaInvoke) {
<!MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND!>suspend<!> {}
}
class WLambdaInvoke {
operator fun invoke(<!UNUSED_PARAMETER!>l<!>: () -> Unit) {}
}