diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt index 5eba8c5106d..00f640357b2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt @@ -93,12 +93,14 @@ class IfThenToSafeAccessInspection : AbstractApplicabilityBasedInspection false } - private fun KtSafeQualifiedExpression.renameLetParameter(editor: Editor) { - val callExpression = selectorExpression as? KtCallExpression ?: return - if (callExpression.calleeExpression?.text != "let") return - val parameter = callExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()?.valueParameters?.singleOrNull() ?: return - PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document) - editor.caretModel.moveToOffset(parameter.startOffset) - KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null) + companion object { + internal fun KtSafeQualifiedExpression.renameLetParameter(editor: Editor) { + val callExpression = selectorExpression as? KtCallExpression ?: return + if (callExpression.calleeExpression?.text != "let") return + val parameter = callExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()?.valueParameters?.singleOrNull() ?: return + PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document) + editor.caretModel.moveToOffset(parameter.startOffset) + KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null) + } } } 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 91b404ed7d5..76404bd944d 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 @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection +import org.jetbrains.kotlin.idea.inspections.branchedTransformations.IfThenToSafeAccessInspection import org.jetbrains.kotlin.idea.intentions.SelfTargetingOffsetIndependentIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.* import org.jetbrains.kotlin.idea.util.application.runWriteAction @@ -54,6 +55,8 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention true + baseClause.anyArgumentEvaluatesTo(receiverExpression) -> + true hasImplicitReceiverReplaceableBySafeCall() || baseClause.hasFirstReceiverOf(receiverExpression) -> !baseClause.hasNullableType(context) else -> @@ -95,6 +98,9 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntentionif (s != null) { + bar(s) + } + else { + 13 + } +} + +fun bar(s: String): Int = 42 \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLet.kt.after b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLet.kt.after new file mode 100644 index 00000000000..0fbd6187c09 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLet.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +fun foo(s: String?) { + val x = s?.let { bar(it) } ?: 13 +} + +fun bar(s: String): Int = 42 \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt new file mode 100644 index 00000000000..36c6bce3ab4 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt @@ -0,0 +1,12 @@ +// WITH_RUNTIME + +fun foo(it: String?) { + val x = if (it != null) { + bar(it) + } + else { + 13 + } +} + +fun bar(s: String): Int = 42 \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt.after b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt.after new file mode 100644 index 00000000000..ca8ade85b1f --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +fun foo(it: String?) { + val x = it?.let { it1 -> bar(it1) } ?: 13 +} + +fun bar(s: String): Int = 42 \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt index 17a7a7644d2..a7e3488b66a 100644 --- a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt +++ b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt @@ -1,4 +1,3 @@ -// IS_APPLICABLE: false // WITH_RUNTIME class Test { @@ -6,9 +5,8 @@ class Test { return param1 } - fun doAThingIfPresent(param1: String?) { - // In theory could propose transformation to 'let' - if (param1 != null) { + fun doAThingIfPresent(param1: String?): String { + return if (param1 != null) { doAThing(param1) } else { "" diff --git a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt.after b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt.after new file mode 100644 index 00000000000..310721e46c1 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt.after @@ -0,0 +1,11 @@ +// WITH_RUNTIME + +class Test { + fun doAThing(param1: String): String { + return param1 + } + + fun doAThingIfPresent(param1: String?): String { + return param1?.let { doAThing(it) } ?: "" + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMemberWithIs.kt b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMemberWithIs.kt index ec95d2b02cc..a21c96d28a3 100644 --- a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMemberWithIs.kt +++ b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMemberWithIs.kt @@ -1,4 +1,3 @@ -// IS_APPLICABLE: false // WITH_RUNTIME class Test { @@ -6,9 +5,8 @@ class Test { return param1 } - fun doAThingIfPresent(param1: String?) { - // In theory could propose transformation to 'let' - if (param1 is String) { + fun doAThingIfPresent(param1: Any?): String { + return if (param1 is String) { doAThing(param1) } else { "" diff --git a/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMemberWithIs.kt.after b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMemberWithIs.kt.after new file mode 100644 index 00000000000..aae7e405e95 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMemberWithIs.kt.after @@ -0,0 +1,11 @@ +// WITH_RUNTIME + +class Test { + fun doAThing(param1: String): String { + return param1 + } + + fun doAThingIfPresent(param1: Any?): String { + return (param1 as? String)?.let { doAThing(it) } ?: "" + } +} \ 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 e8cb98175c7..420dbe96060 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2441,6 +2441,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/ifThenToElvis/otherBlockHasMoreThanOneStatement.kt"); } + @TestMetadata("replaceWithLet.kt") + public void testReplaceWithLet() throws Exception { + runTest("idea/testData/intentions/branched/ifThenToElvis/replaceWithLet.kt"); + } + + @TestMetadata("replaceWithLetAndRenameIt.kt") + public void testReplaceWithLetAndRenameIt() throws Exception { + runTest("idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt"); + } + @TestMetadata("replaceWithLetInMember.kt") public void testReplaceWithLetInMember() throws Exception { runTest("idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt");