Move lambda out: don't apply to multiple/default functional parameters
So #KT-24694 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
a9c91099a4
commit
3e207fd6b2
@@ -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
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun test(a: (String) -> Unit = {}, b: (String) -> Unit = {}) {
|
||||
a("a")
|
||||
b("b")
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
test(<caret>{ })
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun test(a: (String) -> Unit = {}, b: (String) -> Unit = {}) {
|
||||
a("a")
|
||||
b("b")
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
test({ }, { }<caret>)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun test(a: (String) -> Unit = {}, b: (String) -> Unit = {}) {
|
||||
a("a")
|
||||
b("b")
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
test({ }) { }
|
||||
}
|
||||
+10
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user