Convert to lambda reference: don't suggest when lambda is argument for 'suspend' function parameter

#KT-16907 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-01-27 16:06:47 +09:00
committed by Mikhail Glukhikh
parent b75ab5c832
commit 59f70a912a
3 changed files with 23 additions and 1 deletions
@@ -9,6 +9,7 @@ import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.builtins.getReturnTypeFromFunctionType
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.builtins.isSuspendFunctionType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -137,7 +138,8 @@ open class ConvertLambdaToReferenceIntention(text: String) :
if (lambdaParent is KtLambdaArgument) {
val outerCalleeDescriptor = lambdaParent.outerCalleeDescriptor() ?: return false
val lambdaParameterType = outerCalleeDescriptor.valueParameters.lastOrNull()?.type
if (lambdaParameterType != null && lambdaParameterType.isFunctionType) {
if (lambdaParameterType?.isSuspendFunctionType == true) return false
if (lambdaParameterType?.isFunctionType == true) {
// For lambda parameter with receiver, conversion is not allowed
if (lambdaParameterType.isExtensionFunctionType) return false
// Special Unit case (non-Unit returning lambda is accepted here, but non-Unit returning reference is not)
@@ -0,0 +1,15 @@
// IS_APPLICABLE: false
fun coroutine(block: suspend () -> Unit) {}
suspend fun testAction(obj: Any, action: suspend (Any) -> Unit) {
action(action)
}
fun println(message: Any?) {}
fun main() = coroutine {
testAction("OK") {<caret>
println(it)
}
}
@@ -4974,6 +4974,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertLambdaToReference/suspendFun.kt");
}
@TestMetadata("suspendFunctionParameter.kt")
public void testSuspendFunctionParameter() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/suspendFunctionParameter.kt");
}
@TestMetadata("syntheticProperty.kt")
public void testSyntheticProperty() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/syntheticProperty.kt");