diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt index a7fcd20d5ae..c0d470a45dc 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantCompanionReferenceInspection.kt @@ -40,6 +40,7 @@ class RedundantCompanionReferenceInspection : AbstractKotlinInspection() { if (expression.text != objectDeclaration.name) return val containingClass = objectDeclaration.containingClass() ?: return + if (parent.getStrictParentOfType()?.containingClass() == containingClass) return val containingClassDescriptor = containingClass.descriptor as? ClassDescriptor ?: return val selectorDescriptor = selectorExpression?.getCallableDescriptor() when (selectorDescriptor) { @@ -52,9 +53,9 @@ class RedundantCompanionReferenceInspection : AbstractKotlinInspection() { is FunctionDescriptor -> { val name = selectorDescriptor.name val function = containingClass.findFunctionByName(name.asString())?.descriptor - ?: expression.getResolutionScope().findFunction(name, NoLookupLocation.FROM_IDE)?.takeIf { - it.isLocalOrExtension(containingClassDescriptor) - } + ?: expression.getResolutionScope().findFunction(name, NoLookupLocation.FROM_IDE)?.takeIf { + it.isLocalOrExtension(containingClassDescriptor) + } if (function is FunctionDescriptor) { val functionParams = function.valueParameters val calleeParams = diff --git a/idea/testData/inspectionsLocal/redundantCompanionReference/inEnum.kt b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnum.kt new file mode 100644 index 00000000000..0c694db0453 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnum.kt @@ -0,0 +1,13 @@ +enum class E { + E1; + + fun test() { + bar(Companion.foo) + } + + fun bar(s: String) {} + + companion object { + const val foo = "" + } +} diff --git a/idea/testData/inspectionsLocal/redundantCompanionReference/inEnum.kt.after b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnum.kt.after new file mode 100644 index 00000000000..e98816c3586 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnum.kt.after @@ -0,0 +1,13 @@ +enum class E { + E1; + + fun test() { + bar(foo) + } + + fun bar(s: String) {} + + companion object { + const val foo = "" + } +} diff --git a/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry.kt b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry.kt new file mode 100644 index 00000000000..ddae5c1f4ed --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry.kt @@ -0,0 +1,10 @@ +// PROBLEM: none +// DISABLE-ERRORS + +enum class E(val value: String) { + E1(Companion.foo); + + companion object { + const val foo = "" + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry2.kt b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry2.kt new file mode 100644 index 00000000000..d8b70fc5dfe --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry2.kt @@ -0,0 +1,9 @@ +enum class E(val value: String) { + E1(A.Companion.foo); +} + +class A { + companion object { + const val foo = "" + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry2.kt.after b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry2.kt.after new file mode 100644 index 00000000000..6292b6b9047 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry2.kt.after @@ -0,0 +1,9 @@ +enum class E(val value: String) { + E1(A.foo); +} + +class A { + companion object { + const val foo = "" + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 3c5037c88de..7c82c425738 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -3926,6 +3926,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/redundantCompanionReference/import.kt"); } + @TestMetadata("inEnum.kt") + public void testInEnum() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantCompanionReference/inEnum.kt"); + } + + @TestMetadata("inEnumEntry.kt") + public void testInEnumEntry() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry.kt"); + } + + @TestMetadata("inEnumEntry2.kt") + public void testInEnumEntry2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantCompanionReference/inEnumEntry2.kt"); + } + @TestMetadata("methodArgument.kt") public void testMethodArgument() throws Exception { runTest("idea/testData/inspectionsLocal/redundantCompanionReference/methodArgument.kt");