diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt index 3e610b2cf27..32f96ced99a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfThenToElvisIntention.kt @@ -46,14 +46,19 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention + false + negatedClause is KtThrowExpression && negatedClause.throwsNullPointerExceptionWithNoArguments() -> + false + baseClause.evaluatesTo(receiverExpression) -> + true + receiverExpression is KtThisExpression && hasImplicitReceiver() || baseClause.hasFirstReceiverOf(receiverExpression) -> + !baseClause.hasNullableType(context) + else -> + false + } override fun isApplicableTo(element: KtIfExpression): Boolean { val ifThenToSelectData = element.buildSelectTransformationData() ?: return false diff --git a/idea/testData/intentions/branched/ifThenToElvis/nullableResultWithImplicitReceiver.kt b/idea/testData/intentions/branched/ifThenToElvis/nullableResultWithImplicitReceiver.kt new file mode 100644 index 00000000000..cec960ba1da --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/nullableResultWithImplicitReceiver.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +class Something { + fun nullable(): Int? = null +} +fun Something?.nullable(value: Int): Int? = + if (this == null) value else nullable() \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index da2f7aba771..6f5c52f6162 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2358,6 +2358,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/ifThenToElvis/nullCheckWithSelectorCallChain.kt"); } + @TestMetadata("nullableResultWithImplicitReceiver.kt") + public void testNullableResultWithImplicitReceiver() throws Exception { + runTest("idea/testData/intentions/branched/ifThenToElvis/nullableResultWithImplicitReceiver.kt"); + } + @TestMetadata("nullableThen.kt") public void testNullableThen() throws Exception { runTest("idea/testData/intentions/branched/ifThenToElvis/nullableThen.kt");