Move lambda argument out of parentheses: fix false negative when function type is type parameter

#KT-26979 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-18 10:52:03 +09:00
committed by Mikhail Glukhikh
parent b4789b95ef
commit 8e61a13809
4 changed files with 20 additions and 2 deletions
@@ -45,6 +45,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.SmartList
@@ -138,8 +139,10 @@ fun KtCallExpression.canMoveLambdaOutsideParentheses(): Boolean {
// if there are functions among candidates but none of them have last function parameter then not show the intention
if (candidates.isNotEmpty() && candidates.none { candidate ->
val params = candidate.valueParameters
params.lastOrNull()?.type?.isFunctionOrSuspendFunctionType == true &&
params.count { it.type.isFunctionOrSuspendFunctionType } == lambdaArgumentCount + referenceArgumentCount
val lastParamType = params.lastOrNull()?.type
(lastParamType?.isFunctionOrSuspendFunctionType == true || lastParamType?.isTypeParameter() == true) && params.count {
it.type.let { type -> type.isFunctionOrSuspendFunctionType || type.isTypeParameter() }
} == lambdaArgumentCount + referenceArgumentCount
}
) return false
}
@@ -0,0 +1,5 @@
fun <T> foo(t: T) {}
fun test() {
foo(<caret>{ "a" })
}
@@ -0,0 +1,5 @@
fun <T> foo(t: T) {}
fun test() {
foo { "a" }
}
@@ -4167,6 +4167,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
public void testSuspendLambda() throws Exception {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/suspendLambda.kt");
}
@TestMetadata("typeParameter.kt")
public void testTypeParameter() throws Exception {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/typeParameter.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/moveSuspiciousCallableReferenceIntoParentheses")