From a901de51125744f647ce322bc5bdf46295dadb7a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 25 Oct 2016 16:14:43 +0300 Subject: [PATCH] KT-14084 related : replaceFirstReceiver now replaces ALL calls to safe calls if necessary Fixes relevant "replace let" and "if to elvis" cases --- idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt | 6 +++--- .../branched/ifThenToElvis/isCheckWithSelectorChain.kt | 5 +++++ .../ifThenToElvis/isCheckWithSelectorChain.kt.after | 5 +++++ .../letWithMultipleMethodCall.kt | 2 +- .../letWithMultipleMethodCall.kt.after | 2 +- .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 6 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt create mode 100644 idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index e5586ce8ed7..1ee3e81fab4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -271,14 +271,14 @@ fun KtDotQualifiedExpression.replaceFirstReceiver( safeAccess: Boolean = false ): KtExpression { val receiver = receiverExpression + if (safeAccess) { + operationTokenNode.psi.replace(factory.createSafeCallNode().psi) + } when (receiver) { is KtDotQualifiedExpression -> { receiver.replaceFirstReceiver(factory, newReceiver, safeAccess) } else -> { - if (safeAccess) { - operationTokenNode.psi.replace(factory.createSafeCallNode().psi) - } receiver.replace(newReceiver) } } diff --git a/idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt b/idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt new file mode 100644 index 00000000000..f548c0d765c --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt @@ -0,0 +1,5 @@ +class My(val x: Int) + +fun foo(arg: Any?): Int { + return if (arg is My) arg.x.hashCode() else 42 +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt.after b/idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt.after new file mode 100644 index 00000000000..65e5e784923 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt.after @@ -0,0 +1,5 @@ +class My(val x: Int) + +fun foo(arg: Any?): Int { + return (arg as? My)?.x?.hashCode() ?: 42 +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt b/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt index 97e796cf2d4..07ace3e0b5d 100644 --- a/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt +++ b/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt @@ -4,6 +4,6 @@ fun foo() { val foo: String? = null foo?.let { - it.to("").to("") + it.hashCode().hashCode() } } \ No newline at end of file diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt.after b/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt.after index 8b822fa52f9..2ad7d4ba2bc 100644 --- a/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt.after +++ b/idea/testData/intentions/replaceSingleLineLetIntention/letWithMultipleMethodCall.kt.after @@ -3,5 +3,5 @@ fun foo() { val foo: String? = null - foo?.to("").to("") + foo?.hashCode()?.hashCode() } \ 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 bc7d811766a..ebdd738418f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -1308,6 +1308,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("isCheckWithSelectorChain.kt") + public void testIsCheckWithSelectorChain() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelectorChain.kt"); + doTest(fileName); + } + @TestMetadata("lhsEqualsNull.kt") public void testLhsEqualsNull() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/lhsEqualsNull.kt");