Inline variable: fix it works correctly for 'when' subject variable

#KT-29870 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-02-14 16:37:41 +09:00
committed by Vladimir Dolzhenko
parent d8ab046136
commit e56abcbb85
8 changed files with 52 additions and 3 deletions
@@ -40,7 +40,8 @@ class KotlinInlineCallableProcessor(
private val reference: KtSimpleNameReference?, private val reference: KtSimpleNameReference?,
private val inlineThisOnly: Boolean, private val inlineThisOnly: Boolean,
private val deleteAfter: Boolean, private val deleteAfter: Boolean,
private val statementToDelete: KtBinaryExpression? = null private val statementToDelete: KtBinaryExpression? = null,
private val postAction: (KtCallableDeclaration) -> Unit = {}
) : BaseRefactoringProcessor(project) { ) : BaseRefactoringProcessor(project) {
private val kind = when (declaration) { private val kind = when (declaration) {
@@ -82,6 +83,7 @@ class KotlinInlineCallableProcessor(
) )
} }
} }
postAction(declaration)
} }
) )
} }
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.psi.KtBinaryExpression import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtWhenExpression
class KotlinInlineValDialog( class KotlinInlineValDialog(
property: KtProperty, property: KtProperty,
@@ -66,12 +67,20 @@ class KotlinInlineValDialog(
override fun isInlineThis() = KotlinRefactoringSettings.instance.INLINE_LOCAL_THIS override fun isInlineThis() = KotlinRefactoringSettings.instance.INLINE_LOCAL_THIS
public override fun doAction() { public override fun doAction() {
val isWhenSubjectVariable = (callable.parent as? KtWhenExpression)?.subjectVariable == callable
val deleteAfter = !isInlineThisOnly && !isKeepTheDeclaration
invokeRefactoring( invokeRefactoring(
KotlinInlineCallableProcessor( KotlinInlineCallableProcessor(
project, replacementStrategy, callable, reference, project, replacementStrategy, callable, reference,
inlineThisOnly = isInlineThisOnly, inlineThisOnly = isInlineThisOnly,
deleteAfter = !isInlineThisOnly && !isKeepTheDeclaration, deleteAfter = deleteAfter && !isWhenSubjectVariable,
statementToDelete = assignmentToDelete statementToDelete = assignmentToDelete,
postAction = { declaration ->
if (deleteAfter && isWhenSubjectVariable) {
val property = declaration as? KtProperty
property?.initializer?.let { property.replace(it) }
}
}
) )
) )
@@ -0,0 +1,7 @@
fun test(value: Any): String {
return when (val <caret>v = value) {
is String -> "$v is String"
is Int -> "$v is Int"
else -> "$v"
}
}
@@ -0,0 +1,7 @@
fun test(value: Any): String {
return when (value) {
is String -> "$value is String"
is Int -> "$value is Int"
else -> "$value"
}
}
@@ -0,0 +1,7 @@
fun test(s: String) {
when (val x = s.length) {
1 -> println("one")
2 -> println("two")
else -> println(x<caret>)
}
}
@@ -0,0 +1,7 @@
fun test(s: String) {
when (s.length) {
1 -> println("one")
2 -> println("two")
else -> println(s.length)
}
}
@@ -12924,6 +12924,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
public void testVarCopy() throws Exception { public void testVarCopy() throws Exception {
runTest("idea/testData/inspectionsLocal/unnecessaryVariable/varCopy.kt"); runTest("idea/testData/inspectionsLocal/unnecessaryVariable/varCopy.kt");
} }
@TestMetadata("whenSubject.kt")
public void testWhenSubject() throws Exception {
runTest("idea/testData/inspectionsLocal/unnecessaryVariable/whenSubject.kt");
}
} }
@TestMetadata("idea/testData/inspectionsLocal/unsafeCastFromDynamic") @TestMetadata("idea/testData/inspectionsLocal/unsafeCastFromDynamic")
@@ -620,6 +620,11 @@ public class InlineTestGenerated extends AbstractInlineTest {
runTest("idea/testData/refactoring/inline/inlineVariableOrProperty/varWithInc.kt"); runTest("idea/testData/refactoring/inline/inlineVariableOrProperty/varWithInc.kt");
} }
@TestMetadata("whenSubject.kt")
public void testWhenSubject() throws Exception {
runTest("idea/testData/refactoring/inline/inlineVariableOrProperty/whenSubject.kt");
}
@TestMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis") @TestMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)