From 19f518c037c7b31141b1ded9a6582abb9802166a Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Sat, 24 Oct 2020 00:18:32 +0900 Subject: [PATCH] Revert "IfThenToSafeAccessInspection: fix it works correctly for variable/operator call" This reverts commit 5095caee --- .../IfThenToSafeAccessInspection.kt | 2 -- .../branchedTransformations/IfThenUtils.kt | 31 ++----------------- .../ifThenToSafeAccess/callInvokeOperator.kt | 8 ----- .../callInvokeOperator.kt.after | 8 ----- .../ifThenToSafeAccess/callInvokeOperator2.kt | 11 ------- .../callInvokeOperator2.kt.after | 9 ------ .../ifThenToSafeAccess/callVariable.kt | 4 --- .../ifThenToSafeAccess/callVariable.kt.after | 4 --- .../ifThenToSafeAccess/callVariable2.kt | 7 ----- .../ifThenToSafeAccess/callVariable2.kt.after | 5 --- .../LocalInspectionTestGenerated.java | 20 ------------ 11 files changed, 2 insertions(+), 107 deletions(-) delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt.after delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt.after delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt.after delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt delete mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt.after 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 d584a9927f0..6ea1b614163 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt @@ -104,8 +104,6 @@ private fun IfThenToSelectData.clausesReplaceableBySafeCall(): Boolean = when { context.diagnostics.forElement(condition) .any { it.factory == Errors.SENSELESS_COMPARISON || it.factory == Errors.USELESS_IS_CHECK } -> false baseClause.evaluatesTo(receiverExpression) -> true - (baseClause as? KtCallExpression)?.calleeExpression?.evaluatesTo(receiverExpression) == true - && baseClause.isCallingInvokeFunction(context) -> true baseClause.hasFirstReceiverOf(receiverExpression) -> withoutResultInCallChain(baseClause, context) baseClause.anyArgumentEvaluatesTo(receiverExpression) -> true receiverExpression is KtThisExpression -> getImplicitReceiver()?.let { it.type == receiverExpression.getType(context) } == true 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 d0ec1386765..f7532c08e2f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -12,15 +12,12 @@ import com.intellij.openapi.util.TextRange import com.intellij.psi.search.LocalSearchScope import com.intellij.psi.search.searches.ReferencesSearch import org.jetbrains.kotlin.KtNodeTypes -import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor -import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.idea.core.KotlinNameSuggester import org.jetbrains.kotlin.idea.core.replaced -import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.idea.intentions.getLeftMostReceiverExpression import org.jetbrains.kotlin.idea.intentions.replaceFirstReceiver import org.jetbrains.kotlin.idea.refactoring.inline.KotlinInlineValHandler @@ -245,36 +242,12 @@ data class IfThenToSelectData( receiverExpression, baseClause ).insertSafeCalls(factory) - baseClause is KtCallExpression -> { - val callee = baseClause.calleeExpression - if (callee != null && baseClause.isCallingInvokeFunction(context)) { - factory.createExpressionByPattern("$0?.invoke()", callee) - } else { - baseClause.replaceCallWithLet(receiverExpression, factory) - } - } - else -> { - var replaced = baseClause.insertSafeCalls(factory) - if (replaced is KtQualifiedExpression) { - val call = replaced.callExpression - val callee = call?.calleeExpression - if (callee != null && call.isCallingInvokeFunction(context)) { - replaced = factory.createExpressionByPattern("$0?.${callee.text}?.invoke()", replaced.receiverExpression) - } - } - replaced - } + baseClause is KtCallExpression -> baseClause.replaceCallWithLet(receiverExpression, factory) + else -> baseClause.insertSafeCalls(factory) } } } - internal fun KtExpression.isCallingInvokeFunction(context: BindingContext): Boolean { - if (this !is KtCallExpression) return false - val resolvedCall = getResolvedCall(context) ?: resolveToCall() ?: return false - val descriptor = resolvedCall.resultingDescriptor as? SimpleFunctionDescriptor ?: return false - return descriptor is FunctionInvokeDescriptor || descriptor.isOperator && descriptor.name.asString() == "invoke" - } - internal fun getImplicitReceiver(): ImplicitReceiver? { val resolvedCall = baseClause.getResolvedCall(context) ?: return null if (resolvedCall.getExplicitReceiverValue() != null) return null diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt deleted file mode 100644 index 08d3c4e6b2e..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt +++ /dev/null @@ -1,8 +0,0 @@ -// HIGHLIGHT: INFORMATION -fun test(foo: Foo?) { - if (foo != null) foo() -} - -class Foo { - operator fun invoke() {} -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt.after deleted file mode 100644 index 88c18c3a9a0..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt.after +++ /dev/null @@ -1,8 +0,0 @@ -// HIGHLIGHT: INFORMATION -fun test(foo: Foo?) { - foo?.invoke() -} - -class Foo { - operator fun invoke() {} -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt deleted file mode 100644 index f70bbc6c25b..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt +++ /dev/null @@ -1,11 +0,0 @@ -class Foo(val bar: Bar) - -class Bar { - operator fun invoke() {} -} - -fun test(foo: Foo?) { - if (foo != null) { - foo.bar() - } -} diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt.after deleted file mode 100644 index eff2fd7e317..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt.after +++ /dev/null @@ -1,9 +0,0 @@ -class Foo(val bar: Bar) - -class Bar { - operator fun invoke() {} -} - -fun test(foo: Foo?) { - foo?.bar?.invoke() -} diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt deleted file mode 100644 index 62b4f34d582..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt +++ /dev/null @@ -1,4 +0,0 @@ -// HIGHLIGHT: INFORMATION -fun test(foo: (() -> Unit)?) { - if (foo != null) foo() -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt.after deleted file mode 100644 index b0d96e75a07..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt.after +++ /dev/null @@ -1,4 +0,0 @@ -// HIGHLIGHT: INFORMATION -fun test(foo: (() -> Unit)?) { - foo?.invoke() -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt deleted file mode 100644 index 825925928e8..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt +++ /dev/null @@ -1,7 +0,0 @@ -class Foo(val f: () -> Unit) - -fun test(foo: Foo?) { - if (foo != null) { - foo.f() - } -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt.after deleted file mode 100644 index 75d61e92dab..00000000000 --- a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt.after +++ /dev/null @@ -1,5 +0,0 @@ -class Foo(val f: () -> Unit) - -fun test(foo: Foo?) { - foo?.f?.invoke() -} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index c1280dff6d1..948d1fb1c55 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -522,26 +522,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/call4.kt"); } - @TestMetadata("callInvokeOperator.kt") - public void testCallInvokeOperator() throws Exception { - runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator.kt"); - } - - @TestMetadata("callInvokeOperator2.kt") - public void testCallInvokeOperator2() throws Exception { - runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callInvokeOperator2.kt"); - } - - @TestMetadata("callVariable.kt") - public void testCallVariable() throws Exception { - runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable.kt"); - } - - @TestMetadata("callVariable2.kt") - public void testCallVariable2() throws Exception { - runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/callVariable2.kt"); - } - @TestMetadata("conditionComparesNullWithNull.kt") public void testConditionComparesNullWithNull() throws Exception { runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionComparesNullWithNull.kt");