Fix false positive in redundant companion reference #KT-23435 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
3bc7eefad9
commit
e96b5f3117
+6
-4
@@ -13,8 +13,10 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
import org.jetbrains.kotlin.idea.references.mainReference
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
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.resolve.DescriptorUtils
|
||||||
|
|
||||||
class RedundantCompanionReferenceInspection : AbstractKotlinInspection() {
|
class RedundantCompanionReferenceInspection : AbstractKotlinInspection() {
|
||||||
@@ -24,7 +26,7 @@ class RedundantCompanionReferenceInspection : AbstractKotlinInspection() {
|
|||||||
val descriptor = (expression.mainReference.resolve() as? KtObjectDeclaration)?.descriptor ?: return
|
val descriptor = (expression.mainReference.resolve() as? KtObjectDeclaration)?.descriptor ?: return
|
||||||
if (!DescriptorUtils.isCompanionObject(descriptor)) return
|
if (!DescriptorUtils.isCompanionObject(descriptor)) return
|
||||||
|
|
||||||
val parent = expression.getStrictParentOfType<KtDotQualifiedExpression>() ?: return
|
val parent = expression.parent as? KtDotQualifiedExpression ?: return
|
||||||
if (expression == parent.receiverExpression && expression.text == descriptor.containingDeclaration?.name?.asString()) return
|
if (expression == parent.receiverExpression && expression.text == descriptor.containingDeclaration?.name?.asString()) return
|
||||||
if (expression == parent.selectorExpression && parent.parent !is KtDotQualifiedExpression) return
|
if (expression == parent.selectorExpression && parent.parent !is KtDotQualifiedExpression) return
|
||||||
|
|
||||||
@@ -45,7 +47,7 @@ private class RemoveRedundantCompanionReferenceFix : LocalQuickFix {
|
|||||||
|
|
||||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
val expression = descriptor.psiElement as? KtReferenceExpression ?: return
|
val expression = descriptor.psiElement as? KtReferenceExpression ?: return
|
||||||
val parent = expression.getStrictParentOfType<KtDotQualifiedExpression>() ?: return
|
val parent = expression.parent as? KtDotQualifiedExpression ?: return
|
||||||
val selector = parent.selectorExpression ?: return
|
val selector = parent.selectorExpression ?: return
|
||||||
val receiver = parent.receiverExpression
|
val receiver = parent.receiverExpression
|
||||||
if (expression == receiver) parent.replace(selector) else parent.replace(receiver)
|
if (expression == receiver) parent.replace(selector) else parent.replace(receiver)
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
|
||||||
|
class C {
|
||||||
|
companion object {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
fun foo(i: Any) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
this.foo(<caret>C)
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -2565,6 +2565,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("methodArgument.kt")
|
||||||
|
public void testMethodArgument() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/methodArgument.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("notCompanion.kt")
|
@TestMetadata("notCompanion.kt")
|
||||||
public void testNotCompanion() throws Exception {
|
public void testNotCompanion() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/notCompanion.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/notCompanion.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user