Add inspection to highlight unnecessary explicit companion references
So #KT-22971 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
4c5913f5a3
commit
3fbf85dc37
@@ -0,0 +1,17 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports redundant <b>Companion</b> reference, for example:
|
||||
<br /><br />
|
||||
|
||||
<pre>
|
||||
<b>class</b> A {
|
||||
<b>companion object</b> {
|
||||
<b>fun</b> create() = "Hello"
|
||||
}
|
||||
}
|
||||
<b>fun</b> test() {
|
||||
<b>val</b> s = A.Companion.create() // redundant Companion reference
|
||||
}
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
||||
@@ -2746,6 +2746,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantCompanionReferenceInspection"
|
||||
displayName="Redundant Companion reference"
|
||||
groupPath="Kotlin"
|
||||
groupName="Redundant constructs"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
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.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
|
||||
class RedundantCompanionReferenceInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return referenceExpressionVisitor(fun(expression) {
|
||||
|
||||
val descriptor = (expression.mainReference.resolve() as? KtObjectDeclaration)?.descriptor ?: return
|
||||
if (!DescriptorUtils.isCompanionObject(descriptor)) return
|
||||
|
||||
val parent = expression.getStrictParentOfType<KtDotQualifiedExpression>() ?: return
|
||||
if (expression == parent.receiverExpression && expression.text == descriptor.containingDeclaration?.name?.asString()) return
|
||||
if (expression == parent.selectorExpression && parent.parent !is KtDotQualifiedExpression) return
|
||||
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
"Redundant Companion reference",
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
||||
RemoveRedundantCompanionReferenceFix()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private class RemoveRedundantCompanionReferenceFix : LocalQuickFix {
|
||||
override fun getName() = "Remove redundant Companion reference"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val expression = descriptor.psiElement as? KtReferenceExpression ?: return
|
||||
val parent = expression.getStrictParentOfType<KtDotQualifiedExpression>() ?: return
|
||||
val selector = parent.selectorExpression ?: return
|
||||
val receiver = parent.receiverExpression
|
||||
if (expression == receiver) parent.replace(selector) else parent.replace(receiver)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RedundantCompanionReferenceInspection
|
||||
@@ -0,0 +1,9 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun create() = C()
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
C.<caret>Companion.create()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun create() = C()
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
C.create()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun create() = C()
|
||||
}
|
||||
fun test() {
|
||||
<caret>Companion.create()
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun create() = C()
|
||||
}
|
||||
fun test() {
|
||||
create()
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// PROBLEM: none
|
||||
class C {
|
||||
companion object {
|
||||
fun foo() {}
|
||||
}
|
||||
}
|
||||
|
||||
fun test() = C.<caret>Companion::foo
|
||||
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
class C {
|
||||
companion object {
|
||||
fun create() = C()
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
<caret>C.create()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
class C {
|
||||
companion object {
|
||||
fun create() = C()
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
C.<caret>Companion
|
||||
}
|
||||
+39
@@ -2515,6 +2515,45 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantCompanionReference")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RedundantCompanionReference extends AbstractLocalInspectionTest {
|
||||
public void testAllFilesPresentInRedundantCompanionReference() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantCompanionReference"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("basic.kt")
|
||||
public void testBasic() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/basic.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("directCompanion.kt")
|
||||
public void testDirectCompanion() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/directCompanion.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("functionReference.kt")
|
||||
public void testFunctionReference() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/functionReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notCompanion.kt")
|
||||
public void testNotCompanion() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/notCompanion.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("onlyCompanion.kt")
|
||||
public void testOnlyCompanion() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantCompanionReference/onlyCompanion.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantExplicitType")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user