diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt index e3379c63793..fad57a81d6d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt @@ -59,15 +59,14 @@ fun KtWhenExpression.getSubjectToIntroduce(): KtExpression? { for (condition in conditions) { if (condition !is KtWhenConditionWithExpression) return null - val candidate = condition.expression?.getWhenConditionSubjectCandidate() as? KtNameReferenceExpression ?: return null + val candidate = condition.expression?.getWhenConditionSubjectCandidate() ?: return null + if (candidate !is KtNameReferenceExpression && candidate !is KtThisExpression) return null if (lastCandidate == null) { lastCandidate = candidate - } - else if (!lastCandidate.matches(candidate)) { + } else if (!lastCandidate.matches(candidate)) { return null } - } } diff --git a/idea/testData/inspectionsLocal/branched/introduceWhenSubject/this.kt b/idea/testData/inspectionsLocal/branched/introduceWhenSubject/this.kt new file mode 100644 index 00000000000..7a290a9c3b3 --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/introduceWhenSubject/this.kt @@ -0,0 +1,7 @@ +fun Number.test() { + when { + this is Int -> {} + this is Long -> {} + this is Short -> {} + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/introduceWhenSubject/this.kt.after b/idea/testData/inspectionsLocal/branched/introduceWhenSubject/this.kt.after new file mode 100644 index 00000000000..50b9335106e --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/introduceWhenSubject/this.kt.after @@ -0,0 +1,7 @@ +fun Number.test() { + when (this) { + is Int -> {} + is Long -> {} + is Short -> {} + } +} \ 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 af6ecfad32c..38e62810593 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -460,6 +460,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/branched/introduceWhenSubject/lineBreaksAndComments.kt"); } + @TestMetadata("this.kt") + public void testThis() throws Exception { + runTest("idea/testData/inspectionsLocal/branched/introduceWhenSubject/this.kt"); + } + @TestMetadata("whenWithEqualityTests.kt") public void testWhenWithEqualityTests() throws Exception { runTest("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithEqualityTests.kt");