diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt index 9f8fb08ad5b..993c646ae04 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt @@ -17,13 +17,11 @@ import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInWriteAction import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport import org.jetbrains.kotlin.idea.imports.getImportableTargets +import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.util.* import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer -import org.jetbrains.kotlin.psi.psiUtil.getParentOfType -import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver -import org.jetbrains.kotlin.psi.psiUtil.parents +import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getCall @@ -60,19 +58,19 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT val RETAIN_COMPANION = ShortenReferences { Options(removeExplicitCompanion = false) } fun canBePossibleToDropReceiver(element: KtDotQualifiedExpression, bindingContext: BindingContext): Boolean { - val receiver = element.receiverExpression - val nameRef = when (receiver) { + val nameRef = when (val receiver = element.receiverExpression) { is KtThisExpression -> return true is KtNameReferenceExpression -> receiver is KtDotQualifiedExpression -> receiver.selectorExpression as? KtNameReferenceExpression ?: return false else -> return false } - val targetDescriptor = bindingContext[BindingContext.REFERENCE_TARGET, nameRef] - when (targetDescriptor) { + when (val targetDescriptor = bindingContext[BindingContext.REFERENCE_TARGET, nameRef]) { is ClassDescriptor -> { if (targetDescriptor.kind != ClassKind.OBJECT) return true // for object receiver we should additionally check that it's dispatch receiver (that is the member is inside the object) or not a receiver at all - val resolvedCall = element.getResolvedCall(bindingContext) ?: return false + val resolvedCall = element.getResolvedCall(bindingContext) + ?: return element.getQualifiedElementSelector()?.mainReference?.resolveToDescriptors(bindingContext) != null + val receiverKind = resolvedCall.explicitReceiverKind return receiverKind == ExplicitReceiverKind.DISPATCH_RECEIVER || receiverKind == ExplicitReceiverKind.NO_EXPLICIT_RECEIVER } diff --git a/idea/testData/inspectionsLocal/removeRedundantQualifierName/objectCallChain.kt b/idea/testData/inspectionsLocal/removeRedundantQualifierName/objectCallChain.kt new file mode 100644 index 00000000000..f903562d032 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantQualifierName/objectCallChain.kt @@ -0,0 +1,17 @@ +package my.ada.adad.ad + +import my.ada.adad.ad.Fixtures.Register.Domain.UserRepository + +object Fixtures { + object Register { + object Domain { + object UserRepository { + const val authSuccess = true + } + } + } +} + +fun test() { + my.ada.adad.ad.Fixtures.Register.Domain.UserRepository.authSuccess +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantQualifierName/objectCallChain.kt.after b/idea/testData/inspectionsLocal/removeRedundantQualifierName/objectCallChain.kt.after new file mode 100644 index 00000000000..3c61baab0a6 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantQualifierName/objectCallChain.kt.after @@ -0,0 +1,17 @@ +package my.ada.adad.ad + +import my.ada.adad.ad.Fixtures.Register.Domain.UserRepository + +object Fixtures { + object Register { + object Domain { + object UserRepository { + const val authSuccess = true + } + } + } +} + +fun test() { + UserRepository.authSuccess +} \ 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 80570fe22c2..854b01fb2e2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -8234,6 +8234,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableTypeWithRuntime2.kt"); } + @TestMetadata("objectCallChain.kt") + public void testObjectCallChain() throws Exception { + runTest("idea/testData/inspectionsLocal/removeRedundantQualifierName/objectCallChain.kt"); + } + @TestMetadata("secondaryConstructor.kt") public void testSecondaryConstructor() throws Exception { runTest("idea/testData/inspectionsLocal/removeRedundantQualifierName/secondaryConstructor.kt");