diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineCallableProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineCallableProcessor.kt index 067873337e1..6528ecdc72c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineCallableProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineCallableProcessor.kt @@ -40,7 +40,8 @@ class KotlinInlineCallableProcessor( private val reference: KtSimpleNameReference?, private val inlineThisOnly: Boolean, private val deleteAfter: Boolean, - private val statementToDelete: KtBinaryExpression? = null + private val statementToDelete: KtBinaryExpression? = null, + private val postAction: (KtCallableDeclaration) -> Unit = {} ) : BaseRefactoringProcessor(project) { private val kind = when (declaration) { @@ -82,6 +83,7 @@ class KotlinInlineCallableProcessor( ) } } + postAction(declaration) } ) } diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineValDialog.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineValDialog.kt index f7f89746140..6b9a0528bad 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineValDialog.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/KotlinInlineValDialog.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings import org.jetbrains.kotlin.idea.references.KtSimpleNameReference import org.jetbrains.kotlin.psi.KtBinaryExpression import org.jetbrains.kotlin.psi.KtProperty +import org.jetbrains.kotlin.psi.KtWhenExpression class KotlinInlineValDialog( property: KtProperty, @@ -66,12 +67,20 @@ class KotlinInlineValDialog( override fun isInlineThis() = KotlinRefactoringSettings.instance.INLINE_LOCAL_THIS public override fun doAction() { + val isWhenSubjectVariable = (callable.parent as? KtWhenExpression)?.subjectVariable == callable + val deleteAfter = !isInlineThisOnly && !isKeepTheDeclaration invokeRefactoring( KotlinInlineCallableProcessor( project, replacementStrategy, callable, reference, inlineThisOnly = isInlineThisOnly, - deleteAfter = !isInlineThisOnly && !isKeepTheDeclaration, - statementToDelete = assignmentToDelete + deleteAfter = deleteAfter && !isWhenSubjectVariable, + statementToDelete = assignmentToDelete, + postAction = { declaration -> + if (deleteAfter && isWhenSubjectVariable) { + val property = declaration as? KtProperty + property?.initializer?.let { property.replace(it) } + } + } ) ) diff --git a/idea/testData/inspectionsLocal/unnecessaryVariable/whenSubject.kt b/idea/testData/inspectionsLocal/unnecessaryVariable/whenSubject.kt new file mode 100644 index 00000000000..32691daccad --- /dev/null +++ b/idea/testData/inspectionsLocal/unnecessaryVariable/whenSubject.kt @@ -0,0 +1,7 @@ +fun test(value: Any): String { + return when (val v = value) { + is String -> "$v is String" + is Int -> "$v is Int" + else -> "$v" + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/unnecessaryVariable/whenSubject.kt.after b/idea/testData/inspectionsLocal/unnecessaryVariable/whenSubject.kt.after new file mode 100644 index 00000000000..308725a0313 --- /dev/null +++ b/idea/testData/inspectionsLocal/unnecessaryVariable/whenSubject.kt.after @@ -0,0 +1,7 @@ +fun test(value: Any): String { + return when (value) { + is String -> "$value is String" + is Int -> "$value is Int" + else -> "$value" + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/inline/inlineVariableOrProperty/whenSubject.kt b/idea/testData/refactoring/inline/inlineVariableOrProperty/whenSubject.kt new file mode 100644 index 00000000000..ed96a8a53e3 --- /dev/null +++ b/idea/testData/refactoring/inline/inlineVariableOrProperty/whenSubject.kt @@ -0,0 +1,7 @@ +fun test(s: String) { + when (val x = s.length) { + 1 -> println("one") + 2 -> println("two") + else -> println(x) + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/inline/inlineVariableOrProperty/whenSubject.kt.after b/idea/testData/refactoring/inline/inlineVariableOrProperty/whenSubject.kt.after new file mode 100644 index 00000000000..24b8a28d127 --- /dev/null +++ b/idea/testData/refactoring/inline/inlineVariableOrProperty/whenSubject.kt.after @@ -0,0 +1,7 @@ +fun test(s: String) { + when (s.length) { + 1 -> println("one") + 2 -> println("two") + else -> println(s.length) + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 5a4a9754de4..94b35ec342d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -12924,6 +12924,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { public void testVarCopy() throws Exception { 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") 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 deb2566318b..851a491d6b9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java @@ -620,6 +620,11 @@ public class InlineTestGenerated extends AbstractInlineTest { 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") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)