(UnusedSymbolInspection) lazy parameter evaluation fix

This commit is contained in:
Vladimir Ilmov
2020-05-25 15:09:32 +02:00
parent 4a7854a948
commit ed6aceca87
@@ -108,9 +108,9 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
this is KtNamedDeclaration && checkAnnotatedUsingPatterns(this, KOTLIN_ADDITIONAL_ANNOTATIONS) this is KtNamedDeclaration && checkAnnotatedUsingPatterns(this, KOTLIN_ADDITIONAL_ANNOTATIONS)
fun isEntryPoint(declaration: KtNamedDeclaration): Boolean = fun isEntryPoint(declaration: KtNamedDeclaration): Boolean =
isEntryPoint(declaration, isCheapEnoughToSearchUsages(declaration)) isEntryPoint(declaration, lazy(LazyThreadSafetyMode.NONE) { isCheapEnoughToSearchUsages(declaration) })
private fun isEntryPoint(declaration: KtNamedDeclaration, isCheapEnough: SearchCostResult): Boolean { private fun isEntryPoint(declaration: KtNamedDeclaration, isCheapEnough: Lazy<SearchCostResult>): Boolean {
if (declaration.hasKotlinAdditionalAnnotation()) return true if (declaration.hasKotlinAdditionalAnnotation()) return true
if (declaration is KtClass && declaration.declarations.any { it.hasKotlinAdditionalAnnotation() }) return true if (declaration is KtClass && declaration.declarations.any { it.hasKotlinAdditionalAnnotation() }) return true
@@ -136,7 +136,7 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
if (lightElement == null) return false if (lightElement == null) return false
if (isCheapEnough == TOO_MANY_OCCURRENCES) return false if (isCheapEnough.value == TOO_MANY_OCCURRENCES) return false
return javaInspection.isEntryPoint(lightElement) return javaInspection.isEntryPoint(lightElement)
} }
@@ -275,7 +275,7 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
// More expensive, resolve-based checks // More expensive, resolve-based checks
val descriptor = declaration.resolveToDescriptorIfAny() ?: return val descriptor = declaration.resolveToDescriptorIfAny() ?: return
if (descriptor is FunctionDescriptor && descriptor.isOperator) return if (descriptor is FunctionDescriptor && descriptor.isOperator) return
val isCheapEnough by lazy { val isCheapEnough = lazy(LazyThreadSafetyMode.NONE) {
isCheapEnoughToSearchUsages(declaration) isCheapEnoughToSearchUsages(declaration)
} }
if (isEntryPoint(declaration, isCheapEnough)) return if (isEntryPoint(declaration, isCheapEnough)) return
@@ -328,17 +328,17 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
} }
private fun hasNonTrivialUsages(declaration: KtNamedDeclaration, descriptor: DeclarationDescriptor? = null): Boolean { private fun hasNonTrivialUsages(declaration: KtNamedDeclaration, descriptor: DeclarationDescriptor? = null): Boolean {
val isCheapEnough by lazy { isCheapEnoughToSearchUsages(declaration) } val isCheapEnough = lazy(LazyThreadSafetyMode.NONE) { isCheapEnoughToSearchUsages(declaration) }
return hasNonTrivialUsages(declaration, isCheapEnough, descriptor) return hasNonTrivialUsages(declaration, isCheapEnough, descriptor)
} }
private fun hasNonTrivialUsages(declaration: KtNamedDeclaration, enoughToSearchUsages: SearchCostResult, descriptor: DeclarationDescriptor? = null): Boolean { private fun hasNonTrivialUsages(declaration: KtNamedDeclaration, enoughToSearchUsages: Lazy<SearchCostResult>, descriptor: DeclarationDescriptor? = null): Boolean {
val project = declaration.project val project = declaration.project
val psiSearchHelper = PsiSearchHelper.getInstance(project) val psiSearchHelper = PsiSearchHelper.getInstance(project)
val useScope = psiSearchHelper.getUseScope(declaration) val useScope = psiSearchHelper.getUseScope(declaration)
val restrictedScope = if (useScope is GlobalSearchScope) { val restrictedScope = if (useScope is GlobalSearchScope) {
val zeroOccurrences = when (enoughToSearchUsages) { val zeroOccurrences = when (enoughToSearchUsages.value) {
ZERO_OCCURRENCES -> true ZERO_OCCURRENCES -> true
FEW_OCCURRENCES -> false FEW_OCCURRENCES -> false
TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used