diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index 28baadfc333..36e1604b5fc 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -130,8 +130,7 @@ fun KtPostfixExpression.inlineBaseExpressionIfApplicableWithPrompt(editor: Edito (this.baseExpression as? KtNameReferenceExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor) } -fun KtExpression.isStableVariable(): Boolean { - val context = this.analyze() +fun KtExpression.isStableVariable(context: BindingContext = this.analyze()): Boolean { val descriptor = BindingContextUtils.extractVariableDescriptorFromReference(context, this) return descriptor is VariableDescriptor && DataFlowValueFactory.isStableValue(descriptor, DescriptorUtils.getContainingModule(descriptor)) 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 9e2b3b6d970..877bce6f667 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 @@ -27,8 +27,10 @@ import org.jetbrains.kotlin.idea.intentions.getLeftMostReceiverExpression import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getType import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode.Companion.PARTIAL +import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.TypeNullability import org.jetbrains.kotlin.types.typeUtil.nullability @@ -39,9 +41,9 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention when (condition.operationToken) { - KtTokens.EQEQ -> checkedExpression.clausesReplaceableByElvis(thenClause, elseClause) - KtTokens.EXCLEQ -> checkedExpression.clausesReplaceableByElvis(elseClause, thenClause) + KtTokens.EQEQ -> checkedExpression.clausesReplaceableByElvis(thenClause, elseClause, context) + KtTokens.EXCLEQ -> checkedExpression.clausesReplaceableByElvis(elseClause, thenClause, context) else -> false } - is KtIsExpression -> when (condition.isNegated) { - true -> checkedExpression.clausesReplaceableByElvis(thenClause, elseClause) - false -> checkedExpression.clausesReplaceableByElvis(elseClause, thenClause) + is KtIsExpression -> { + if (!context[BindingContext.TYPE, condition.typeReference].isNotNull()) return false + + when (condition.isNegated) { + true -> checkedExpression.clausesReplaceableByElvis(thenClause, elseClause, context) + false -> checkedExpression.clausesReplaceableByElvis(elseClause, thenClause, context) + } } else -> false } @@ -77,7 +85,9 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 8dc916f7802..dbdfcb338f2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -1302,6 +1302,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("isCheckForNullableType.kt") + public void testIsCheckForNullableType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheckForNullableType.kt"); + doTest(fileName); + } + @TestMetadata("isCheckWithSelector.kt") public void testIsCheckWithSelector() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelector.kt");