diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt index d809fdd0414..e60b00ce5ce 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt @@ -28,18 +28,15 @@ import com.intellij.psi.PsiDocumentManager import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.intentions.ChooseStringExpression import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention -import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNotNullExpression -import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNullExpression -import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceValueForCondition -import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableSimpleExpression +import org.jetbrains.kotlin.idea.intentions.branchedTransformations.* import org.jetbrains.kotlin.lexer.KtTokens -import org.jetbrains.kotlin.psi.KtPostfixExpression -import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.psi.KtPsiUtil -import org.jetbrains.kotlin.psi.KtThrowExpression +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement -class DoubleBangToIfThenIntention : SelfTargetingRangeIntention(KtPostfixExpression::class.java, "Replace '!!' expression with 'if' expression"), LowPriorityAction { +class DoubleBangToIfThenIntention : + SelfTargetingRangeIntention(KtPostfixExpression::class.java, "Replace '!!' expression with 'if' expression"), + LowPriorityAction { override fun applicabilityRange(element: KtPostfixExpression): TextRange? { return if (element.operationToken == KtTokens.EXCLEXCL && element.baseExpression != null) element.operationReference.textRange @@ -60,12 +57,31 @@ class DoubleBangToIfThenIntention : SelfTargetingRangeIntention) { + val a: A? = A() + a!!.f() +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression.kt.after new file mode 100644 index 00000000000..ddc41cac9e7 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression.kt.after @@ -0,0 +1,15 @@ +// WITH_RUNTIME +fun foo(): String? { + return "foo" +} + +class A { + fun f(): Int { + return 42 + } +} + +fun main(args: Array) { + val a: A? = A() + if (a != null) a.f() else throw NullPointerException("Expression 'a' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression2.kt b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression2.kt new file mode 100644 index 00000000000..e5eb50538bf --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression2.kt @@ -0,0 +1,16 @@ +// WITH_RUNTIME +fun foo(): String? { + return "foo" +} + +class A { + val g = 44 + fun f(): Int { + return 42 + } +} + +fun main(args: Array) { + val a: A? = A() + a!!.g +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression2.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression2.kt.after new file mode 100644 index 00000000000..86fc56e349d --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression2.kt.after @@ -0,0 +1,16 @@ +// WITH_RUNTIME +fun foo(): String? { + return "foo" +} + +class A { + val g = 44 + fun f(): Int { + return 42 + } +} + +fun main(args: Array) { + val a: A? = A() + if (a != null) a.g else throw NullPointerException("Expression 'a' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression3.kt b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression3.kt new file mode 100644 index 00000000000..d970a8be5e0 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression3.kt @@ -0,0 +1,15 @@ +// WITH_RUNTIME +fun foo(): String? { + return "foo" +} + +class A { + fun f(): Int { + return 42 + } +} + +fun main(args: Array) { + val a: A? = A() + val b = a!!.f() +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression3.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression3.kt.after new file mode 100644 index 00000000000..dcad1e3b949 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression3.kt.after @@ -0,0 +1,15 @@ +// WITH_RUNTIME +fun foo(): String? { + return "foo" +} + +class A { + fun f(): Int { + return 42 + } +} + +fun main(args: Array) { + val a: A? = A() + val b = if (a != null) a.f() else throw NullPointerException("Expression 'a' must not be null") +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 21c4af23cef..4424f6169bd 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2059,6 +2059,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt"); } + @TestMetadata("replaceParentExpression.kt") + public void testReplaceParentExpression() throws Exception { + runTest("idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression.kt"); + } + + @TestMetadata("replaceParentExpression2.kt") + public void testReplaceParentExpression2() throws Exception { + runTest("idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression2.kt"); + } + + @TestMetadata("replaceParentExpression3.kt") + public void testReplaceParentExpression3() throws Exception { + runTest("idea/testData/intentions/branched/doubleBangToIfThen/replaceParentExpression3.kt"); + } + @TestMetadata("simpleNameExpressionInParens.kt") public void testSimpleNameExpressionInParens() throws Exception { runTest("idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt");