Convert lambda to reference: suggest on lambda argument for suspend function parameter
#KT-34378 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
9b6b25f069
commit
f7a69d4aa7
@@ -72,19 +72,20 @@ open class ConvertLambdaToReferenceIntention(textGetter: () -> String) : SelfTar
|
||||
val calleeDescriptor = context[REFERENCE_TARGET, calleeReferenceExpression] as? CallableMemberDescriptor ?: return false
|
||||
|
||||
val lambdaParameterType = lambdaExpression.lambdaParameterType(context)
|
||||
if (lambdaParameterType?.isSuspendFunctionType == true) return false
|
||||
if (lambdaParameterType?.isExtensionFunctionType == true) {
|
||||
if (explicitReceiver != null && explicitReceiver !is KtThisExpression) return false
|
||||
val receiver = calleeDescriptor.dispatchReceiverParameter ?: calleeDescriptor.extensionReceiverParameter
|
||||
if (lambdaParameterType.getReceiverTypeFromFunctionType() != receiver?.type) return false
|
||||
}
|
||||
|
||||
val lambdaParameterIsSuspend = lambdaParameterType?.isSuspendFunctionType == true
|
||||
val calleeFunctionIsSuspend = (calleeDescriptor as? FunctionDescriptor)?.isSuspend == true
|
||||
if (lambdaParameterIsSuspend && !calleeFunctionIsSuspend || !lambdaParameterIsSuspend && calleeFunctionIsSuspend) return false
|
||||
|
||||
// No references with type parameters
|
||||
if (calleeDescriptor.typeParameters.isNotEmpty()) return false
|
||||
// No references to Java synthetic properties
|
||||
if (calleeDescriptor is SyntheticJavaPropertyDescriptor) return false
|
||||
// No suspend functions
|
||||
if ((calleeDescriptor as? FunctionDescriptor)?.isSuspend == true) return false
|
||||
|
||||
val descriptorHasReceiver = with(calleeDescriptor) {
|
||||
// No references to both member / extension
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo(a: suspend () -> Unit) {}
|
||||
|
||||
suspend fun action() {}
|
||||
|
||||
fun usage() {
|
||||
foo { action() <caret> }
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo(a: suspend () -> Unit) {}
|
||||
|
||||
suspend fun action() {}
|
||||
|
||||
fun usage() {
|
||||
foo(::action)
|
||||
}
|
||||
@@ -5165,6 +5165,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/suspendFunctionParameter.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("suspendFunctionParameter2.kt")
|
||||
public void testSuspendFunctionParameter2() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/suspendFunctionParameter2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("syntheticProperty.kt")
|
||||
public void testSyntheticProperty() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/syntheticProperty.kt");
|
||||
|
||||
Reference in New Issue
Block a user