From 44cf0a9f72543639836d54bb2011858a235806a8 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Thu, 6 Feb 2020 15:47:09 +0100 Subject: [PATCH] Don't insert import if deprecation replacement contains stdlib typealias Fixed #KT-14781 --- .../kotlin/idea/codeInliner/CodeToInlineBuilder.kt | 10 +++++++++- .../deprecatedSymbolUsage/typeAliases/stdlibAliased.kt | 8 ++++++++ .../typeAliases/stdlibAliased.kt.after | 8 ++++++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 5 +++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt index 74b8a0db2e5..ec0a21bc2af 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.references.canBeResolvedViaImport import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.util.getResolutionScope +import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType @@ -34,10 +35,12 @@ import org.jetbrains.kotlin.renderer.render import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver +import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.utils.sure @@ -167,7 +170,12 @@ class CodeToInlineBuilder( target.importableFqName } if (importableFqName != null) { - codeToInline.fqNamesToImport.add(importableFqName) + val lexicalScope = (expression?.containingFile as? KtFile)?.getResolutionScope(bindingContext, resolutionFacade) + val lookupName = importableFqName?.let { + lexicalScope?.findClassifier(it.shortName(), NoLookupLocation.FROM_IDE)?.typeConstructor + ?.declarationDescriptor?.fqNameOrNull() + } + codeToInline.fqNamesToImport.add(lookupName ?: importableFqName) } } diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt b/idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt new file mode 100644 index 00000000000..0f5ef26c3c1 --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt @@ -0,0 +1,8 @@ +// "Replace with 'Exception()'" "true" +// RUNTIME_WITH_FULL_JDK +package ppp + +@Deprecated("do not use", ReplaceWith("Exception()")) +fun x(): Throwable = RuntimeException() + +val e = x() \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt.after new file mode 100644 index 00000000000..5713863273b --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt.after @@ -0,0 +1,8 @@ +// "Replace with 'Exception()'" "true" +// RUNTIME_WITH_FULL_JDK +package ppp + +@Deprecated("do not use", ReplaceWith("Exception()")) +fun x(): Throwable = RuntimeException() + +val e = Exception() \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index e013ac5fb25..d525e9eeb41 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -6980,6 +6980,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/onlyAliasDeprecated.kt"); } + @TestMetadata("stdlibAliased.kt") + public void testStdlibAliased() throws Exception { + runTest("idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/stdlibAliased.kt"); + } + @TestMetadata("transitiveFromClass.kt") public void testTransitiveFromClass() throws Exception { runTest("idea/testData/quickfix/deprecatedSymbolUsage/typeAliases/transitiveFromClass.kt");