Fix several false positives and make safer "redundant companion ref"
Don't report it on an import directive #KT-23520 Fixed Don't report it if companion nested class is referenced #KT-23519 Fixed Check companion reference both by descriptor and by name
This commit is contained in:
+13
-9
@@ -11,24 +11,28 @@ import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtObjectDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.referenceExpressionVisitor
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
|
||||
class RedundantCompanionReferenceInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return referenceExpressionVisitor(fun(expression) {
|
||||
val parent = expression.parent as? KtDotQualifiedExpression ?: return
|
||||
if (expression == parent.selectorExpression && parent.parent !is KtDotQualifiedExpression) return
|
||||
if (parent.getStrictParentOfType<KtImportDirective>() != null) return
|
||||
|
||||
val descriptor = (expression.mainReference.resolve() as? KtObjectDeclaration)?.descriptor ?: return
|
||||
if (!DescriptorUtils.isCompanionObject(descriptor)) return
|
||||
val objectDeclaration = expression.mainReference.resolve() as? KtObjectDeclaration ?: return
|
||||
if (!objectDeclaration.isCompanion()) return
|
||||
if (expression.text != objectDeclaration.name) return
|
||||
|
||||
if (expression == parent.receiverExpression && expression.text == descriptor.containingDeclaration?.name?.asString()) return
|
||||
val grandParent = parent.parent as? KtQualifiedExpression
|
||||
if (grandParent != null) {
|
||||
val grandParentDescriptor = grandParent.resolveToCall()?.resultingDescriptor ?: return
|
||||
if (grandParentDescriptor is ConstructorDescriptor) return
|
||||
}
|
||||
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
|
||||
Reference in New Issue
Block a user