diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java index dd54c87bed3..c138b6fe6de 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java @@ -503,7 +503,10 @@ public class KtPsiUtil { if (parentElement instanceof KtLabeledExpression) return false; // 'x ?: ...' case - if (parentElement instanceof KtBinaryExpression && parentOperation == KtTokens.ELVIS && currentInner == ((KtBinaryExpression) parentElement).getRight()) { + if (parentElement instanceof KtBinaryExpression && + parentOperation == KtTokens.ELVIS && + !(innerExpression instanceof KtBinaryExpression) && + currentInner == ((KtBinaryExpression) parentElement).getRight()) { return false; } diff --git a/idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt b/idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt new file mode 100644 index 00000000000..8f3f22e0178 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt @@ -0,0 +1,9 @@ +// See KT-15227 +class My(val local: Boolean) + +class Your(val my: My?, val parent: Any?) + +fun foo(your: Your): Boolean { + val my = your.my + return if (my != null) my.local else your.parent != null +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt.after b/idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt.after new file mode 100644 index 00000000000..a643234547e --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt.after @@ -0,0 +1,9 @@ +// See KT-15227 +class My(val local: Boolean) + +class Your(val my: My?, val parent: Any?) + +fun foo(your: Your): Boolean { + val my = your.my + return my?.local ?: (your.parent != null) +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml index 9b06f6aa724..b57d4d3b862 100644 --- a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml +++ b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/expected.xml @@ -87,4 +87,12 @@ If-Then foldable to '?:' Replace 'if' expression with elvis expression + + comparisonInElse.kt + 8 + light_idea_test_case + + If-Then foldable to '?:' + Replace 'if' expression with elvis expression + diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index fc4ba7d01ab..cef5bdcfddf 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -1294,6 +1294,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("comparisonInElse.kt") + public void testComparisonInElse() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt"); + doTest(fileName); + } + @TestMetadata("conditionComparesNullWithNull.kt") public void testConditionComparesNullWithNull() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/conditionComparesNullWithNull.kt");