[FE 1.0] Report warning on leaking anonymous object type from private inline lambda
^KT-33917 ^KT-56490 Fixed
This commit is contained in:
committed by
Space Team
parent
4c7f8ba196
commit
3c42521ce7
+20
-12
@@ -5,31 +5,39 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve.checkers
|
||||
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
|
||||
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
object PrivateInlineFunctionsReturningAnonymousObjectsChecker : DeclarationChecker {
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
if (context.languageVersionSettings.supportsFeature(LanguageFeature.ApproximateAnonymousReturnTypesInPrivateInlineFunctions))
|
||||
return
|
||||
|
||||
if (descriptor !is SimpleFunctionDescriptor || !descriptor.isInline || !DescriptorVisibilities.isPrivate(descriptor.visibility) || declaration !is KtNamedFunction)
|
||||
return
|
||||
|
||||
val returnTypeConstructor = descriptor.returnType?.constructor ?: return
|
||||
|
||||
if (returnTypeConstructor.supertypes.singleOrNull { it.isAnyOrNullableAny() } == null) return
|
||||
|
||||
val nameIdentifier = declaration.nameIdentifier ?: return
|
||||
val returnTypeDeclarationDescriptor = returnTypeConstructor.declarationDescriptor ?: return
|
||||
val returnType = descriptor.returnType ?: return
|
||||
|
||||
checkTypeAndArguments(returnType, nameIdentifier, context)
|
||||
}
|
||||
|
||||
private fun checkTypeAndArguments(type: KotlinType, reportOn: PsiElement, context: DeclarationCheckerContext) {
|
||||
checkType(type, reportOn, context)
|
||||
for (argument in type.arguments) {
|
||||
checkTypeAndArguments(argument.type, reportOn, context)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkType(type: KotlinType, reportOn: PsiElement, context: DeclarationCheckerContext) {
|
||||
val returnTypeConstructor = type.constructor
|
||||
val returnTypeDeclarationDescriptor = returnTypeConstructor.declarationDescriptor ?: return
|
||||
if (DescriptorUtils.isAnonymousObject(returnTypeDeclarationDescriptor)) {
|
||||
context.trace.report(Errors.PRIVATE_INLINE_FUNCTIONS_RETURNING_ANONYMOUS_OBJECTS.on(nameIdentifier))
|
||||
context.trace.report(Errors.PRIVATE_INLINE_FUNCTIONS_RETURNING_ANONYMOUS_OBJECTS.on(reportOn))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user