From c5c0cbccde1e53a651b6a2815129f4075e77eb96 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 29 Nov 2018 14:31:37 +0300 Subject: [PATCH] Redundant companion reference: simplify name conflict checking code Before this commit, function with same name but different signature wasn't counted as name conflict, now it is - just because that exact check in this place could be too complex and error-prone Enhancement for KT-27539 --- .../RedundantCompanionReferenceInspection.kt | 47 +++++++------------ .../sameNameDifferentArgsFunction.kt | 2 + .../sameNameDifferentArgsFunction.kt.after | 11 ----- 3 files changed, 19 insertions(+), 41 deletions(-) delete mode 100644 idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt index 1ea0beec247..af3e3855c7e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt @@ -47,25 +47,15 @@ class RedundantCompanionReferenceInspection : AbstractKotlinInspection() { when (selectorDescriptor) { is PropertyDescriptor -> { val name = selectorDescriptor.name - if (containingClassDescriptor.findMemberVariable(name.asString()) != null) return + if (containingClassDescriptor.findMemberVariable(name) != null) return val variable = expression.getResolutionScope().findVariable(name, NoLookupLocation.FROM_IDE) if (variable != null && variable.isLocalOrExtension(containingClassDescriptor)) return } is FunctionDescriptor -> { val name = selectorDescriptor.name - val functions = containingClassDescriptor.collectMemberFunction(name.asString()) + listOfNotNull( - expression.getResolutionScope().findFunction(name, NoLookupLocation.FROM_IDE)?.takeIf { - it.isLocalOrExtension(containingClassDescriptor) - } - ) - if (functions.any { - val functionParams = it.valueParameters - val calleeParams = (selectorExpression as? KtCallExpression)?.calleeExpression?.getCallableDescriptor() - ?.valueParameters.orEmpty() - functionParams.size == calleeParams.size && functionParams.zip(calleeParams).all { param -> - param.first.type == param.second.type - } - }) return + if (containingClassDescriptor.findMemberFunction(name) != null) return + val function = expression.getResolutionScope().findFunction(name, NoLookupLocation.FROM_IDE) + if (function != null && function.isLocalOrExtension(containingClassDescriptor)) return } } @@ -85,30 +75,27 @@ class RedundantCompanionReferenceInspection : AbstractKotlinInspection() { } } -private fun ClassDescriptor.findMemberVariable(name: String): PropertyDescriptor? { - val variable = unsubstitutedMemberScope.getContributedVariables(Name.identifier(name), NoLookupLocation.FROM_IDE).firstOrNull() - if (variable != null) return variable +private fun ClassDescriptor.findMemberByName(name: Name, find: ClassDescriptor.(Name) -> D?): D? { + val member = find(name) + if (member != null) return member - val variableInSuperClass = getSuperClassNotAny()?.findMemberVariable(name) - if (variableInSuperClass != null) return variableInSuperClass + val memberInSuperClass = getSuperClassNotAny()?.findMemberByName(name, find) + if (memberInSuperClass != null) return memberInSuperClass getSuperInterfaces().forEach { - val variableInInterface = it.findMemberVariable(name) - if (variableInInterface != null) return variableInInterface + val memberInInterface = it.findMemberByName(name, find) + if (memberInInterface != null) return memberInInterface } return null } -private fun ClassDescriptor.collectMemberFunction(name: String): MutableList { - val functions = mutableListOf() - fun collect(descriptor: ClassDescriptor) { - functions.addAll(descriptor.unsubstitutedMemberScope.getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_IDE)) - descriptor.getSuperClassNotAny()?.let { collect(it) } - descriptor.getSuperInterfaces().forEach { collect(it) } - } - collect(this) - return functions +private fun ClassDescriptor.findMemberVariable(name: Name): PropertyDescriptor? = findMemberByName(name) { + unsubstitutedMemberScope.getContributedVariables(it, NoLookupLocation.FROM_IDE).firstOrNull() +} + +private fun ClassDescriptor.findMemberFunction(name: Name): FunctionDescriptor? = findMemberByName(name) { + unsubstitutedMemberScope.getContributedFunctions(it, NoLookupLocation.FROM_IDE).firstOrNull() } private fun CallableDescriptor.isLocalOrExtension(extensionClassDescriptor: ClassDescriptor): Boolean { diff --git a/idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt b/idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt index 66afe4550ef..7db40bac99b 100644 --- a/idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt +++ b/idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt @@ -1,3 +1,5 @@ +// PROBLEM: none + class Test { companion object { fun f(x: Int, y: Int) = 1 diff --git a/idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt.after b/idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt.after deleted file mode 100644 index 51212a26c4c..00000000000 --- a/idea/testData/inspectionsLocal/redundantCompanionReference/sameNameDifferentArgsFunction.kt.after +++ /dev/null @@ -1,11 +0,0 @@ -class Test { - companion object { - fun f(x: Int, y: Int) = 1 - } - - fun f(x: Int, y: String) = 2 - - fun test() { - f(1, 2) - } -} \ No newline at end of file