Move lambda out: don't apply to multiple/default functional parameters

So #KT-24694 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-06-26 17:43:59 +03:00
committed by Mikhail Glukhikh
parent a9c91099a4
commit 3e207fd6b2
5 changed files with 40 additions and 5 deletions
@@ -131,6 +131,7 @@ fun KtCallExpression.canMoveLambdaOutsideParentheses(): Boolean {
val callee = calleeExpression
if (callee is KtNameReferenceExpression) {
val lambdaArgumentCount = valueArguments.mapNotNull { it.getArgumentExpression()?.unpackFunctionLiteral() }.count()
val bindingContext = analyze(BodyResolveMode.PARTIAL)
val targets = bindingContext[BindingContext.REFERENCE_TARGET, callee]?.let { listOf(it) }
?: bindingContext[BindingContext.AMBIGUOUS_REFERENCE_TARGET, callee]
@@ -138,11 +139,9 @@ fun KtCallExpression.canMoveLambdaOutsideParentheses(): Boolean {
val candidates = targets.filterIsInstance<FunctionDescriptor>()
// if there are functions among candidates but none of them have last function parameter then not show the intention
if (candidates.isNotEmpty() && candidates.none {
val lastParameter = it.valueParameters.lastOrNull()
lastParameter != null && lastParameter.type.isFunctionType
}) {
return false
}
val params = it.valueParameters
params.lastOrNull()?.type?.isFunctionType == true && params.count { it.type.isFunctionType } == lambdaArgumentCount
}) return false
}
return true
@@ -0,0 +1,10 @@
// PROBLEM: none
fun test(a: (String) -> Unit = {}, b: (String) -> Unit = {}) {
a("a")
b("b")
}
fun foo() {
test(<caret>{ })
}
@@ -0,0 +1,8 @@
fun test(a: (String) -> Unit = {}, b: (String) -> Unit = {}) {
a("a")
b("b")
}
fun foo() {
test({ }, { }<caret>)
}
@@ -0,0 +1,8 @@
fun test(a: (String) -> Unit = {}, b: (String) -> Unit = {}) {
a("a")
b("b")
}
fun foo() {
test({ }) { }
}
@@ -2665,6 +2665,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/inapplicableAlreadyHasFunctionLiteral.kt");
}
@TestMetadata("inapplicableMultiFunctionTypeParameters.kt")
public void testInapplicableMultiFunctionTypeParameters() throws Exception {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/inapplicableMultiFunctionTypeParameters.kt");
}
@TestMetadata("inapplicableMultiFunctionTypeParameters2.kt")
public void testInapplicableMultiFunctionTypeParameters2() throws Exception {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/inapplicableMultiFunctionTypeParameters2.kt");
}
@TestMetadata("inapplicableOptionalParametersAfter.kt")
public void testInapplicableOptionalParametersAfter() throws Exception {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/inapplicableOptionalParametersAfter.kt");