Drop result value if it's not needed
This commit is contained in:
@@ -70,6 +70,15 @@ class CodeInliner<TCallElement : KtElement>(
|
||||
|
||||
val commentSaver = CommentSaver(elementToBeReplaced, saveLineBreaks = true)
|
||||
|
||||
// if the value to be inlined is not used and has no side effects we may drop it
|
||||
if (codeToInline.mainExpression != null
|
||||
&& elementToBeReplaced is KtExpression
|
||||
&& !elementToBeReplaced.isUsedAsExpression(bindingContext)
|
||||
&& !codeToInline.mainExpression.shouldKeepValue(usageCount = 0)
|
||||
) {
|
||||
codeToInline.mainExpression = null
|
||||
}
|
||||
|
||||
var receiver = nameExpression.getReceiverExpression()?.marked(USER_CODE_KEY)
|
||||
var receiverType = if (receiver != null) bindingContext.getType(receiver) else null
|
||||
|
||||
@@ -111,7 +120,7 @@ class CodeInliner<TCallElement : KtElement>(
|
||||
if (elementToBeReplaced is KtExpression) {
|
||||
if (receiver != null) {
|
||||
val thisReplaced = codeToInline.collectDescendantsOfType<KtExpression> { it[RECEIVER_VALUE_KEY] }
|
||||
if (receiver.shouldKeepValue(thisReplaced.size)) {
|
||||
if (receiver.shouldKeepValue(usageCount = thisReplaced.size)) {
|
||||
codeToInline.introduceValue(receiver, receiverType, thisReplaced, elementToBeReplaced)
|
||||
}
|
||||
}
|
||||
@@ -182,7 +191,7 @@ class CodeInliner<TCallElement : KtElement>(
|
||||
|
||||
//TODO: sometimes we need to add explicit type arguments here because we don't have expected type in the new context
|
||||
|
||||
if (argument.expression.shouldKeepValue(usages.size)) {
|
||||
if (argument.expression.shouldKeepValue(usageCount = usages.size)) {
|
||||
introduceValuesForParameters.add(IntroduceValueForParameter(parameter, argument.expression, argument.expressionType))
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,6 +6,6 @@ val File.prop: String
|
||||
get() = absolutePath
|
||||
|
||||
fun foo(file: File) {
|
||||
file.prop<caret>
|
||||
val v = file.prop<caret>
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,6 +6,6 @@ val File.prop: String
|
||||
get() = absolutePath
|
||||
|
||||
fun foo(file: File) {
|
||||
file.absolutePath
|
||||
val v = file.absolutePath
|
||||
}
|
||||
|
||||
|
||||
@@ -3,5 +3,5 @@
|
||||
fun oldFun(p: Int): Int = p
|
||||
|
||||
fun foo() {
|
||||
<caret>oldFun(0)
|
||||
val v = <caret>oldFun(0)
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,5 +3,5 @@
|
||||
fun oldFun(p: Int): Int = p
|
||||
|
||||
fun foo() {
|
||||
<caret>0
|
||||
val v = 0
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,6 +3,6 @@ fun <caret>f(p: Int) = p + p
|
||||
fun complexFun(): Int {
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
f(complexFun())
|
||||
fun g(): Int {
|
||||
return f(complexFun())
|
||||
}
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
fun complexFun(): Int {
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
fun g(): Int {
|
||||
val p = complexFun()
|
||||
p + p
|
||||
return p + p
|
||||
}
|
||||
-3
@@ -2,18 +2,15 @@ fun main(args: Array<String>) {
|
||||
if (args.size > 0) {
|
||||
println(1)
|
||||
println(2)
|
||||
1 + 2
|
||||
}
|
||||
else {
|
||||
println(3)
|
||||
println(4)
|
||||
3 + 4
|
||||
}
|
||||
|
||||
for (i in 1..10) {
|
||||
println(0)
|
||||
println(1)
|
||||
0 + 1
|
||||
}
|
||||
|
||||
when (args.size) {
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
fun main(args: Array<String>) {
|
||||
println(3)
|
||||
println(5)
|
||||
3 + 5
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
fun <caret>f(p: Int): Boolean {
|
||||
println(p)
|
||||
return g(p)
|
||||
}
|
||||
|
||||
fun g(p: Int): Boolean = TODO()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
f(1)
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
fun g(p: Int): Boolean = TODO()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println(1)
|
||||
g(1)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun <caret>f(p: Int): Boolean {
|
||||
println(p)
|
||||
return p > 0
|
||||
}
|
||||
|
||||
fun g(): Int = TODO()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
f(1)
|
||||
f(g())
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun g(): Int = TODO()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println(1)
|
||||
println(g())
|
||||
}
|
||||
@@ -466,6 +466,18 @@ public class InlineTestGenerated extends AbstractInlineTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("UnusedComplicatedReturnValue.kt")
|
||||
public void testUnusedComplicatedReturnValue() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/returnAtEnd/UnusedComplicatedReturnValue.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("UnusedReturnValue.kt")
|
||||
public void testUnusedReturnValue() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/returnAtEnd/UnusedReturnValue.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ValIntializer.kt")
|
||||
public void testValIntializer() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/returnAtEnd/ValIntializer.kt");
|
||||
|
||||
Reference in New Issue
Block a user