[kotlin] inline: delete when condition if when contains only else branch

^KTIJ-29176 fixed

GitOrigin-RevId: 52d6fbf2a12bed144caafd9b2cf3c70d4b8b2944
This commit is contained in:
Anna Kozlova
2024-03-22 19:11:08 +00:00
committed by intellij-monorepo-bot
parent e68cdb6036
commit d8c7446aa5
5 changed files with 30 additions and 1 deletions
@@ -773,6 +773,11 @@ public abstract class InlineTestGenerated extends AbstractInlineTest {
public void testReturnNull() throws Exception {
runTest("testData/refactoring/inline/inlineVariableOrProperty/property/returnNull.kt");
}
@TestMetadata("when.kt")
public void testWhen() throws Exception {
runTest("testData/refactoring/inline/inlineVariableOrProperty/property/when.kt");
}
}
}
@@ -0,0 +1,6 @@
val x = 42
fun callUsage() {
when ((<caret>x)) {
else -> { println(6) }
}
}
@@ -0,0 +1,5 @@
fun callUsage() {
when {
else -> { println(6) }
}
}
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -178,7 +179,14 @@ class ExpressionReplacementPerformer(
}
}
if (canDropElementToBeReplaced) {
stub.delete()
val parent = stub.parents.first { it !is KtParenthesizedExpression }
if (parent is KtWhenExpression &&
parent.subjectExpression?.safeDeparenthesize() == stub &&
parent.leftParenthesis != null && parent.rightParenthesis != null) {
parent.deleteChildRange(parent.leftParenthesis, parent.rightParenthesis)
} else {
stub.delete()
}
null
} else {
stub.replaced(psiFactory.createExpression("Unit"))
@@ -612,6 +612,11 @@ public abstract class KotlinFirInlineTestGenerated extends AbstractKotlinFirInli
public void testReturnNull() throws Exception {
runTest("../../idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/returnNull.kt");
}
@TestMetadata("when.kt")
public void testWhen() throws Exception {
runTest("../../idea/tests/testData/refactoring/inline/inlineVariableOrProperty/property/when.kt");
}
}
}