From 0b16c51baffd63e16de555b560963e6ac92b4a51 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Thu, 20 Jun 2019 19:50:16 +0700 Subject: [PATCH] RemoveRedundantQualifierNameInspection: fix false positive #KT-32112 Fixed --- .../RemoveRedundantQualifierNameInspection.kt | 18 +++++++++++------- .../notApplicableExpression4.kt | 14 ++++++++++++++ .../LocalInspectionTestGenerated.java | 5 +++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableExpression4.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantQualifierNameInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantQualifierNameInspection.kt index 75437c282f7..9bbcfa4919b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantQualifierNameInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantQualifierNameInspection.kt @@ -9,6 +9,7 @@ import com.intellij.codeInspection.* import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElementVisitor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.ShortenReferences @@ -16,10 +17,10 @@ import org.jetbrains.kotlin.idea.imports.importableFqName import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.incremental.components.NoLookupLocation -import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject import org.jetbrains.kotlin.resolve.scopes.utils.findFirstClassifierWithDeprecationStatus @@ -38,12 +39,12 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean val context = originalExpression.analyze() - val importableFqName = expressionForAnalyze.getQualifiedElementSelector() - ?.mainReference?.resolveToDescriptors(context)?.firstOrNull() - ?.importableFqName ?: return + val originalDescriptor = expressionForAnalyze.getQualifiedElementSelector() + ?.mainReference?.resolveToDescriptors(context) + ?.firstOrNull() ?: return val applicableExpression = expressionForAnalyze.firstApplicableExpression(validator = { - applicableExpression(originalExpression, context, importableFqName) + applicableExpression(originalExpression, context, originalDescriptor) }) { firstChild as? KtDotQualifiedExpression } ?: return @@ -74,7 +75,7 @@ private tailrec fun T.firstApplicableExpression(validator: T.() private fun KtDotQualifiedExpression.applicableExpression( originalExpression: KtExpression, oldContext: BindingContext, - importableFqName: FqName + originalDescriptor: DeclarationDescriptor ): KtDotQualifiedExpression? { if (!receiverExpression.isApplicableReceiver(oldContext) || !ShortenReferences.canBePossibleToDropReceiver( this, @@ -89,7 +90,10 @@ private fun KtDotQualifiedExpression.applicableExpression( ?.firstOrNull() ?: return null return takeIf { - importableFqName == newDescriptor.importableFqName + if (newDescriptor is ImportedFromObjectCallableDescriptor<*>) + originalDescriptor == newDescriptor.callableFromObject + else + originalDescriptor == newDescriptor } } diff --git a/idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableExpression4.kt b/idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableExpression4.kt new file mode 100644 index 00000000000..3c0692427de --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableExpression4.kt @@ -0,0 +1,14 @@ +// PROBLEM: none +package foo + +class Foo { + fun test() { + foo.myRun { + 42 + } + } +} + +inline fun myRun(block: () -> R): R = block() + +inline fun T.myRun(block: T.() -> R): R = block() \ 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 e9b69af783d..29a20476026 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -7826,6 +7826,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableExpression3.kt"); } + @TestMetadata("notApplicableExpression4.kt") + public void testNotApplicableExpression4() throws Exception { + runTest("idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableExpression4.kt"); + } + @TestMetadata("notApplicableLocalFun.kt") public void testNotApplicableLocalFun() throws Exception { runTest("idea/testData/inspectionsLocal/removeRedundantQualifierName/notApplicableLocalFun.kt");