Add a warning for a user-defined entries property call

^KT-53153
This commit is contained in:
Mikhail Zarechenskiy
2023-01-03 15:53:21 +01:00
committed by Space Team
parent 695a538529
commit 989fc886e1
24 changed files with 549 additions and 6 deletions
@@ -142,6 +142,8 @@ public interface Errors {
DiagnosticFactory1<PsiElement, PropertyDescriptor> DEPRECATED_ACCESS_TO_ENUM_COMPANION_PROPERTY = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<PsiElement> DEPRECATED_ACCESS_TO_ENUM_ENTRY_COMPANION_PROPERTY = DiagnosticFactory0.create(WARNING);
DiagnosticFactory2<PsiElement, PropertyDescriptor, ClassDescriptor> DEPRECATED_RESOLVE_WITH_AMBIGUOUS_ENUM_ENTRY = DiagnosticFactory2.create(WARNING);
DiagnosticFactory1<PsiElement, ConstructorDescriptor> PROTECTED_CONSTRUCTOR_NOT_IN_SUPER_CALL = DiagnosticFactory1.create(ERROR);
@@ -73,6 +73,7 @@ public class DefaultErrorMessages {
MAP.put(INVISIBLE_MEMBER, "Cannot access ''{0}'': it is {1} in {2}", NAME, VISIBILITY, NAME_OF_CONTAINING_DECLARATION_OR_FILE);
MAP.put(DEPRECATED_ACCESS_BY_SHORT_NAME, "Access to this type by short name is deprecated, and soon is going to be removed. Please, add explicit qualifier or import", NAME);
MAP.put(DEPRECATED_ACCESS_TO_ENUM_COMPANION_PROPERTY, "Ambiguous access to companion''s property ''{0}'' in enum is deprecated. Please, add explicit Companion qualifier to the class name", NAME);
MAP.put(DEPRECATED_ACCESS_TO_ENUM_ENTRY_COMPANION_PROPERTY, "Ambiguous access to companion''s property 'entries' in enum is deprecated. Please, add explicit Companion qualifier to the class name");
MAP.put(DEPRECATED_RESOLVE_WITH_AMBIGUOUS_ENUM_ENTRY, "Ambiguous access to property ''{0}'' is deprecated because similar enum entry ''{1}'' is available. Please add explicit named import or use fully qualified name", FQ_NAME, FQ_NAME);
MAP.put(PROTECTED_CONSTRUCTOR_NOT_IN_SUPER_CALL, "Protected constructor ''{0}'' from other classes can only be used in super-call", Renderers.SHORT_NAMES_IN_TYPES);
@@ -72,6 +72,7 @@ private val DEFAULT_CALL_CHECKERS = listOf(
NewSchemeOfIntegerOperatorResolutionChecker, EnumEntryVsCompanionPriorityCallChecker, CompanionInParenthesesLHSCallChecker,
ResolutionToPrivateConstructorOfSealedClassChecker, EqualityCallChecker, UnsupportedUntilOperatorChecker,
BuilderInferenceAssignmentChecker, IncorrectCapturedApproximationCallChecker, CompanionIncorrectlyUnboundedWhenUsedAsLHSCallChecker,
CustomEnumEntriesMigrationCallChecker,
)
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject
import org.jetbrains.kotlin.resolve.scopes.receivers.ClassValueReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
object CustomEnumEntriesMigrationCallChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val descriptor = resolvedCall.resultingDescriptor
if (descriptor !is PropertyDescriptor) return
if (descriptor.name != StandardNames.ENUM_ENTRIES) return
if (resolvedCall.isExtensionWithEnumClassQualifier() ||
resolvedCall.isCallViaCompanionOnEnumClassQualifier(descriptor)
) {
context.trace.report(Errors.DEPRECATED_ACCESS_TO_ENUM_ENTRY_COMPANION_PROPERTY.on(reportOn))
}
}
private fun ResolvedCall<*>.isExtensionWithEnumClassQualifier(): Boolean {
val receiver = extensionReceiver ?: return false
return receiver is ClassValueReceiver && DescriptorUtils.isEnumClass(receiver.classQualifier.descriptor)
}
private fun ResolvedCall<*>.isCallViaCompanionOnEnumClassQualifier(descriptor: PropertyDescriptor): Boolean {
val containingDeclaration = descriptor.containingDeclaration
if (!containingDeclaration.isCompanionObject()) return false
val grandParent = containingDeclaration.containingDeclaration ?: return false
if (grandParent !is ClassDescriptor || !DescriptorUtils.isEnumClass(grandParent)) return false
return dispatchReceiver.isQualifierFor(grandParent)
}
}
@@ -43,10 +43,10 @@ object EnumEntryVsCompanionPriorityCallChecker : CallChecker {
val foundDescriptor = unsubstitutedMemberScope.getContributedClassifier(name, NoLookupLocation.FOR_ALREADY_TRACKED)
return foundDescriptor is ClassDescriptor && foundDescriptor.kind == ClassKind.ENUM_ENTRY
}
private fun ReceiverValue?.isQualifierFor(classDescriptor: ClassDescriptor): Boolean {
if (this !is ClassValueReceiver) return false
val thisClass = this.classQualifier.descriptor as? ClassDescriptor ?: return false
return thisClass.typeConstructor == classDescriptor.typeConstructor
}
}
internal fun ReceiverValue?.isQualifierFor(classDescriptor: ClassDescriptor): Boolean {
if (this !is ClassValueReceiver) return false
val thisClass = this.classQualifier.descriptor as? ClassDescriptor ?: return false
return thisClass.typeConstructor == classDescriptor.typeConstructor
}