diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt index baa15251169..38406129370 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt @@ -19,8 +19,10 @@ import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.calls.callUtil.getType import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.TypeUtils +import org.jetbrains.kotlin.types.typeUtil.isNothing class ElvisToIfThenIntention : SelfTargetingRangeIntention( KtBinaryExpression::class.java, @@ -71,9 +73,12 @@ class ElvisToIfThenIntention : SelfTargetingRangeIntention( if (leftSafeCastReceiver == null) { val property = (KtPsiUtil.safeDeparenthesize(element).parent as? KtProperty) val propertyName = property?.name - if ((right is KtReturnExpression || right is KtBreakExpression || right is KtContinueExpression || right is KtThrowExpression) - && propertyName != null - ) { + val rightIsReturnOrJumps = right is KtReturnExpression + || right is KtBreakExpression + || right is KtContinueExpression + || right is KtThrowExpression + || right.getType(context)?.isNothing() == true + if (rightIsReturnOrJumps && propertyName != null) { val parent = property.parent val factory = KtPsiFactory(element) factory.createExpressionByPattern("if ($0 == null) $1", propertyName, right) diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt new file mode 100644 index 00000000000..9df0c4accfc --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME +fun foo(list: List): String { + val first = list.firstOrNull() ?: error("empty") + return "First length: ${first.length}" +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt.after b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt.after new file mode 100644 index 00000000000..728eb771a11 --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun foo(list: List): String { + val first = list.firstOrNull() + if (first == null) error("empty") + return "First length: ${first.length}" +} \ 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 2dbe86643ba..373bc9190b0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2279,6 +2279,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt"); } + @TestMetadata("assignmentAndError.kt") + public void testAssignmentAndError() throws Exception { + runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt"); + } + @TestMetadata("assignmentAndReturn.kt") public void testAssignmentAndReturn() throws Exception { runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt");