"Convert lambda to reference": fix false positive for vararg function
#KT-16422 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
28aa0763a9
commit
08cfe1acfd
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.resolve.BindingContext.FUNCTION
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
|
||||
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy
|
||||
@@ -128,8 +129,9 @@ open class ConvertLambdaToReferenceIntention(text: String) :
|
||||
if (lambdaValueParameterDescriptors.size < explicitReceiverShift + callableExpression.valueArguments.size) return false
|
||||
val resolvedCall = callableExpression.getResolvedCall(context) ?: return false
|
||||
resolvedCall.valueArguments.entries.forEach { (valueParameter, resolvedArgument) ->
|
||||
val argumentExpression =
|
||||
resolvedArgument.arguments.singleOrNull()?.getArgumentExpression() as? KtNameReferenceExpression ?: return false
|
||||
val argument = resolvedArgument.arguments.singleOrNull() ?: return false
|
||||
if (resolvedArgument is VarargValueArgument && argument.getSpreadElement() == null) return false
|
||||
val argumentExpression = argument.getArgumentExpression() as? KtNameReferenceExpression ?: return false
|
||||
val argumentTarget = context[REFERENCE_TARGET, argumentExpression] as? ValueParameterDescriptor ?: return false
|
||||
if (argumentTarget != lambdaValueParameterDescriptors[valueParameter.index + explicitReceiverShift]) return false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
fun test(i: Int?): IntArray? {
|
||||
return i?.let { <caret>intArrayOf(it) }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun test(vararg i: Int): IntArray {
|
||||
return i.let { <caret>double(*it) }
|
||||
}
|
||||
|
||||
fun double(vararg elements: Int): IntArray = elements.map { it * 2 }.toIntArray()
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun test(vararg i: Int): IntArray {
|
||||
return i.let(::double)
|
||||
}
|
||||
|
||||
fun double(vararg elements: Int): IntArray = elements.map { it * 2 }.toIntArray()
|
||||
@@ -5400,6 +5400,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/unwrap.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("varargFunction.kt")
|
||||
public void testVarargFunction() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/varargFunction.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("varargFunction2.kt")
|
||||
public void testVarargFunction2() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/varargFunction2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("wrongNamedOrder.kt")
|
||||
public void testWrongNamedOrder() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/wrongNamedOrder.kt");
|
||||
|
||||
Reference in New Issue
Block a user