diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeInliner.kt b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeInliner.kt index 2e904f9b1b7..6689e0c3e78 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeInliner.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeInliner.kt @@ -70,6 +70,15 @@ class CodeInliner( 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( if (elementToBeReplaced is KtExpression) { if (receiver != null) { val thisReplaced = codeToInline.collectDescendantsOfType { 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( //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)) } } diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt b/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt index 6db1110e903..c25e40d3f27 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt +++ b/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt @@ -6,6 +6,6 @@ val File.prop: String get() = absolutePath fun foo(file: File) { - file.prop + val v = file.prop } diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt.after index 59d00f48f0a..0a91cd3de14 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt.after +++ b/idea/testData/quickfix/deprecatedSymbolUsage/propertyToSyntheticExtension.kt.after @@ -6,6 +6,6 @@ val File.prop: String get() = absolutePath fun foo(file: File) { - file.absolutePath + val v = file.absolutePath } diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt b/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt index a03540824f0..e3c23b9ee25 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt +++ b/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt @@ -3,5 +3,5 @@ fun oldFun(p: Int): Int = p fun foo() { - oldFun(0) + val v = oldFun(0) } diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt.after index e0b3b8a9bda..cb9cade936b 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt.after +++ b/idea/testData/quickfix/deprecatedSymbolUsage/replaceCallWithArgument.kt.after @@ -3,5 +3,5 @@ fun oldFun(p: Int): Int = p fun foo() { - 0 + val v = 0 } diff --git a/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt b/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt index 1edcabc012b..5a4d719391a 100644 --- a/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt +++ b/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt @@ -3,6 +3,6 @@ fun f(p: Int) = p + p fun complexFun(): Int { } -fun main(args: Array) { - f(complexFun()) +fun g(): Int { + return f(complexFun()) } \ No newline at end of file diff --git a/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt.after b/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt.after index 0ee242ef906..21f93de83a3 100644 --- a/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt.after +++ b/idea/testData/refactoring/inline/function/expressionBody/ComplexArgumentUsedTwice.kt.after @@ -1,7 +1,7 @@ fun complexFun(): Int { } -fun main(args: Array) { +fun g(): Int { val p = complexFun() - p + p + return p + p } \ No newline at end of file diff --git a/idea/testData/refactoring/inline/function/returnAtEnd/AddBlockToControlStatement.kt.after b/idea/testData/refactoring/inline/function/returnAtEnd/AddBlockToControlStatement.kt.after index 94ba67ceae9..71f72833a16 100644 --- a/idea/testData/refactoring/inline/function/returnAtEnd/AddBlockToControlStatement.kt.after +++ b/idea/testData/refactoring/inline/function/returnAtEnd/AddBlockToControlStatement.kt.after @@ -2,18 +2,15 @@ fun main(args: Array) { 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) { diff --git a/idea/testData/refactoring/inline/function/returnAtEnd/MultipleStatements.kt.after b/idea/testData/refactoring/inline/function/returnAtEnd/MultipleStatements.kt.after index 1e8b15095a4..6aa5a456284 100644 --- a/idea/testData/refactoring/inline/function/returnAtEnd/MultipleStatements.kt.after +++ b/idea/testData/refactoring/inline/function/returnAtEnd/MultipleStatements.kt.after @@ -1,5 +1,4 @@ fun main(args: Array) { println(3) println(5) - 3 + 5 } \ No newline at end of file diff --git a/idea/testData/refactoring/inline/function/returnAtEnd/UnusedComplicatedReturnValue.kt b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedComplicatedReturnValue.kt new file mode 100644 index 00000000000..0068ffe09a4 --- /dev/null +++ b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedComplicatedReturnValue.kt @@ -0,0 +1,10 @@ +fun f(p: Int): Boolean { + println(p) + return g(p) +} + +fun g(p: Int): Boolean = TODO() + +fun main(args: Array) { + f(1) +} \ No newline at end of file diff --git a/idea/testData/refactoring/inline/function/returnAtEnd/UnusedComplicatedReturnValue.kt.after b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedComplicatedReturnValue.kt.after new file mode 100644 index 00000000000..8d898e7d75c --- /dev/null +++ b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedComplicatedReturnValue.kt.after @@ -0,0 +1,6 @@ +fun g(p: Int): Boolean = TODO() + +fun main(args: Array) { + println(1) + g(1) +} diff --git a/idea/testData/refactoring/inline/function/returnAtEnd/UnusedReturnValue.kt b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedReturnValue.kt new file mode 100644 index 00000000000..eb2eac07647 --- /dev/null +++ b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedReturnValue.kt @@ -0,0 +1,11 @@ +fun f(p: Int): Boolean { + println(p) + return p > 0 +} + +fun g(): Int = TODO() + +fun main(args: Array) { + f(1) + f(g()) +} \ No newline at end of file diff --git a/idea/testData/refactoring/inline/function/returnAtEnd/UnusedReturnValue.kt.after b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedReturnValue.kt.after new file mode 100644 index 00000000000..10e78541395 --- /dev/null +++ b/idea/testData/refactoring/inline/function/returnAtEnd/UnusedReturnValue.kt.after @@ -0,0 +1,6 @@ +fun g(): Int = TODO() + +fun main(args: Array) { + println(1) + println(g()) +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java index 1a26b513964..bc1b13237dd 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java @@ -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");