Code inliner: move lambda outside parentheses always when possible
So #KT-24215 Fixed
This commit is contained in:
@@ -593,15 +593,15 @@ class CodeInliner<TCallElement : KtElement>(
|
||||
val callExpression = argumentList.parent as? KtCallExpression ?: return
|
||||
if (callExpression.lambdaArguments.isNotEmpty()) return
|
||||
|
||||
val resolvedCall = callExpression.resolveToCall() ?: return
|
||||
if (!resolvedCall.isReallySuccess()) return
|
||||
val argumentMatch = resolvedCall.getArgumentMapping(argument) as ArgumentMatch
|
||||
if (argumentMatch.valueParameter != resolvedCall.resultingDescriptor.valueParameters.last()) return
|
||||
|
||||
callExpression.resolveToCall() ?: return
|
||||
callExpressions.add(callExpression)
|
||||
})
|
||||
|
||||
callExpressions.forEach { it.moveFunctionLiteralOutsideParentheses() }
|
||||
callExpressions.forEach {
|
||||
if (it.canMoveLambdaOutsideParentheses()) {
|
||||
it.moveFunctionLiteralOutsideParentheses()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private operator fun <T : Any> PsiElement.get(key: Key<T>): T? = getCopyableUserData(key)
|
||||
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// "Replace with 'useSimple(simple(init))'" "true"
|
||||
|
||||
fun simple(param: Int = 0, init: () -> Unit): Int {
|
||||
init()
|
||||
return param
|
||||
}
|
||||
|
||||
@Deprecated("Use useSimple instead", ReplaceWith("useSimple(simple(init))"))
|
||||
fun use(init: () -> Unit): Int {
|
||||
init()
|
||||
return 0
|
||||
}
|
||||
|
||||
fun useSimple(s: Int): Int {
|
||||
return s
|
||||
}
|
||||
|
||||
fun println(s: String) {}
|
||||
|
||||
fun useIt() {
|
||||
val t = use<caret> {
|
||||
println("abc")
|
||||
}
|
||||
}
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// "Replace with 'useSimple(simple(init))'" "true"
|
||||
|
||||
fun simple(param: Int = 0, init: () -> Unit): Int {
|
||||
init()
|
||||
return param
|
||||
}
|
||||
|
||||
@Deprecated("Use useSimple instead", ReplaceWith("useSimple(simple(init))"))
|
||||
fun use(init: () -> Unit): Int {
|
||||
init()
|
||||
return 0
|
||||
}
|
||||
|
||||
fun useSimple(s: Int): Int {
|
||||
return s
|
||||
}
|
||||
|
||||
fun println(s: String) {}
|
||||
|
||||
fun useIt() {
|
||||
val t = useSimple(simple {
|
||||
println("abc")
|
||||
})
|
||||
}
|
||||
idea/testData/quickfix/deprecatedSymbolUsage/optionalParameters/optionalParameterAndLambdaComplex.kt
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
// "Replace with 'addA(d(createDummy, dummyParam1, initDummy), dummyParam)'" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
typealias NewDummyRef<V> = (Any) -> V
|
||||
|
||||
inline fun <A : Any> Any.d(
|
||||
createDummy: NewDummyRef<A>,
|
||||
dummyParam1: Int = 0,
|
||||
dummyParam2: Int = 0,
|
||||
initDummy: A.() -> Unit = {}
|
||||
) = createDummy(this).also(initDummy).also { dummyParam1 + dummyParam2 }
|
||||
|
||||
@Deprecated("Use d instead", ReplaceWith("addA(d(createDummy, dummyParam1, initDummy), dummyParam)"))
|
||||
inline fun <A : Any> MutableList<A>.addA(
|
||||
createDummy: NewDummyRef<A>,
|
||||
dummyParam1: Int = 0,
|
||||
dummyParam: Unit,
|
||||
initDummy: A.() -> Unit = {}
|
||||
) = createDummy(this).also(initDummy).also { dummyParam1 }.also { add(it) }
|
||||
|
||||
@Deprecated("Use d instead", ReplaceWith("addA(d(createDummy, dummyParam1, dummyParam2, initDummy), dummyParam)"))
|
||||
inline fun <A : Any> MutableList<A>.addA(
|
||||
createDummy: NewDummyRef<A>,
|
||||
dummyParam1: Int = 0,
|
||||
dummyParam2: Int = 0,
|
||||
dummyParam: Unit,
|
||||
initDummy: A.() -> Unit = {}
|
||||
) = createDummy(this).also(initDummy).also { dummyParam1 + dummyParam2 }.also { add(it) }
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <A : Any> MutableList<A>.addA(a: A, dummyParam: Unit): A = a.also { add(a) }
|
||||
|
||||
fun createHi(any: Any) = "Hi $any"
|
||||
|
||||
val unDeprecateMe = mutableListOf("Hello").apply {
|
||||
addA<caret>(::createHi, 1, Unit) { // Run the quick fix from the IDE and watch it produce broken code.
|
||||
println("Yo")
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// "Replace with 'addA(d(createDummy, dummyParam1, initDummy), dummyParam)'" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
typealias NewDummyRef<V> = (Any) -> V
|
||||
|
||||
inline fun <A : Any> Any.d(
|
||||
createDummy: NewDummyRef<A>,
|
||||
dummyParam1: Int = 0,
|
||||
dummyParam2: Int = 0,
|
||||
initDummy: A.() -> Unit = {}
|
||||
) = createDummy(this).also(initDummy).also { dummyParam1 + dummyParam2 }
|
||||
|
||||
@Deprecated("Use d instead", ReplaceWith("addA(d(createDummy, dummyParam1, initDummy), dummyParam)"))
|
||||
inline fun <A : Any> MutableList<A>.addA(
|
||||
createDummy: NewDummyRef<A>,
|
||||
dummyParam1: Int = 0,
|
||||
dummyParam: Unit,
|
||||
initDummy: A.() -> Unit = {}
|
||||
) = createDummy(this).also(initDummy).also { dummyParam1 }.also { add(it) }
|
||||
|
||||
@Deprecated("Use d instead", ReplaceWith("addA(d(createDummy, dummyParam1, dummyParam2, initDummy), dummyParam)"))
|
||||
inline fun <A : Any> MutableList<A>.addA(
|
||||
createDummy: NewDummyRef<A>,
|
||||
dummyParam1: Int = 0,
|
||||
dummyParam2: Int = 0,
|
||||
dummyParam: Unit,
|
||||
initDummy: A.() -> Unit = {}
|
||||
) = createDummy(this).also(initDummy).also { dummyParam1 + dummyParam2 }.also { add(it) }
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <A : Any> MutableList<A>.addA(a: A, dummyParam: Unit): A = a.also { add(a) }
|
||||
|
||||
fun createHi(any: Any) = "Hi $any"
|
||||
|
||||
val unDeprecateMe = mutableListOf("Hello").apply {
|
||||
addA(d(::createHi, 1 ) { // Run the quick fix from the IDE and watch it produce broken code.
|
||||
println("Yo")
|
||||
}, Unit)
|
||||
}
|
||||
@@ -5602,6 +5602,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
runTest("idea/testData/quickfix/deprecatedSymbolUsage/optionalParameters/namedArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("optionalParameterAndLambda.kt")
|
||||
public void testOptionalParameterAndLambda() throws Exception {
|
||||
runTest("idea/testData/quickfix/deprecatedSymbolUsage/optionalParameters/optionalParameterAndLambda.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("optionalParameterAndLambdaComplex.kt")
|
||||
public void testOptionalParameterAndLambdaComplex() throws Exception {
|
||||
runTest("idea/testData/quickfix/deprecatedSymbolUsage/optionalParameters/optionalParameterAndLambdaComplex.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("optionalParameters1.kt")
|
||||
public void testOptionalParameters1() throws Exception {
|
||||
runTest("idea/testData/quickfix/deprecatedSymbolUsage/optionalParameters/optionalParameters1.kt");
|
||||
|
||||
Reference in New Issue
Block a user