KT-18538 Cleanup in RemoveRedundantQualifierNameInspection

This commit is contained in:
Roman Golyshev
2020-05-26 18:38:32 +03:00
parent 4a511c7721
commit 16bdfa4fee
@@ -43,20 +43,18 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean
if (expressionParent is KtDotQualifiedExpression || expressionParent is KtPackageDirective || expressionParent is KtImportDirective) return if (expressionParent is KtDotQualifiedExpression || expressionParent is KtPackageDirective || expressionParent is KtImportDirective) return
val expressionForAnalyze = expression.firstExpressionWithoutReceiver() ?: return val expressionForAnalyze = expression.firstExpressionWithoutReceiver() ?: return
val parent = expressionForAnalyze.parent val originalExpression: KtExpression = expressionForAnalyze.parent as? KtClassLiteralExpression ?: expressionForAnalyze
@Suppress("USELESS_CAST") val originalExpression = if (parent is KtClassLiteralExpression)
parent as KtExpression
else
expressionForAnalyze
val receiverReference = expressionForAnalyze.receiverExpression.let { val receiverReference = expressionForAnalyze.receiverExpression.let {
it.safeAs<KtQualifiedExpression>()?.receiverExpression ?: it it.safeAs<KtQualifiedExpression>()?.receiverExpression ?: it
}.mainReference?.resolve() }.mainReference?.resolve()
val parentEnumEntry = expressionForAnalyze.getStrictParentOfType<KtEnumEntry>() val parentEnumEntry = expressionForAnalyze.getStrictParentOfType<KtEnumEntry>()
if (parentEnumEntry != null) { if (parentEnumEntry != null) {
val companionObject = (receiverReference as? KtObjectDeclaration)?.takeIf { it.isCompanion() } val companionObject = (receiverReference as? KtObjectDeclaration)?.takeIf { it.isCompanion() }
if (companionObject?.containingClass() == parentEnumEntry.getStrictParentOfType<KtClass>()) return if (companionObject?.containingClass() == parentEnumEntry.getStrictParentOfType<KtClass>()) return
} }
if (receiverReference?.safeAs<KtClass>()?.isEnum() == true if (receiverReference?.safeAs<KtClass>()?.isEnum() == true
&& expressionForAnalyze.getParentOfTypesAndPredicate(true, KtClass::class.java) { it.isEnum() } != receiverReference && expressionForAnalyze.getParentOfTypesAndPredicate(true, KtClass::class.java) { it.isEnum() } != receiverReference
&& (expressionForAnalyze.isEnumStaticMethodCall() || expressionForAnalyze.isCompanionObjectReference()) && (expressionForAnalyze.isEnumStaticMethodCall() || expressionForAnalyze.isCompanionObjectReference())
@@ -68,11 +66,10 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean
?.mainReference?.resolveToDescriptors(context) ?.mainReference?.resolveToDescriptors(context)
?.firstOrNull() ?: return ?.firstOrNull() ?: return
val applicableExpression = expressionForAnalyze.firstApplicableExpression(validator = { val applicableExpression = expressionForAnalyze.firstApplicableExpression(
applicableExpression(originalExpression, context, originalDescriptor) validator = { applicableExpression(originalExpression, context, originalDescriptor) },
}) { generator = { firstChild as? KtDotQualifiedExpression }
firstChild as? KtDotQualifiedExpression ) ?: return
} ?: return
reportProblem(holder, applicableExpression) reportProblem(holder, applicableExpression)
} }
@@ -81,9 +78,10 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean
if (type.parent is KtUserType) return if (type.parent is KtUserType) return
val context = type.analyze() val context = type.analyze()
val applicableExpression = type.firstApplicableExpression(validator = { applicableExpression(context) }) { val applicableExpression = type.firstApplicableExpression(
firstChild as? KtUserType validator = { applicableExpression(context) },
} ?: return generator = { firstChild as? KtUserType }
) ?: return
reportProblem(holder, applicableExpression) reportProblem(holder, applicableExpression)
} }
@@ -110,11 +108,10 @@ private fun KtDotQualifiedExpression.applicableExpression(
oldContext: BindingContext, oldContext: BindingContext,
originalDescriptor: DeclarationDescriptor originalDescriptor: DeclarationDescriptor
): KtDotQualifiedExpression? { ): KtDotQualifiedExpression? {
if (!receiverExpression.isApplicableReceiver(oldContext) || !ShortenReferences.canBePossibleToDropReceiver( if (!receiverExpression.isApplicableReceiver(oldContext) || !ShortenReferences.canBePossibleToDropReceiver(this, oldContext)) {
this, return null
oldContext }
)
) return null
val expressionText = originalExpression.text.substring(lastChild.startOffset - originalExpression.startOffset) val expressionText = originalExpression.text.substring(lastChild.startOffset - originalExpression.startOffset)
val newExpression = KtPsiFactory(originalExpression).createExpressionIfPossible(expressionText) ?: return null val newExpression = KtPsiFactory(originalExpression).createExpressionIfPossible(expressionText) ?: return null
val newContext = newExpression.analyzeAsReplacement(originalExpression, oldContext) val newContext = newExpression.analyzeAsReplacement(originalExpression, oldContext)
@@ -187,7 +184,7 @@ class RemoveRedundantQualifierNameQuickFix : LocalQuickFix {
} }
val substring = file.text.substring(range.first, range.last) val substring = file.text.substring(range.first, range.last)
Regex.fromLiteral(substring).findAll(file.text, file.importList?.endOffset ?: 0).toList().reversed().forEach { Regex.fromLiteral(substring).findAll(file.text, file.importList?.endOffset ?: 0).toList().asReversed().forEach {
ShortenReferences.DEFAULT.process(file, it.range.first, it.range.last + 1) ShortenReferences.DEFAULT.process(file, it.range.first, it.range.last + 1)
} }
} }