From edd51908ec348ccc73efd74cc0658f1f558bed3d Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Wed, 18 Mar 2015 22:15:19 +0300 Subject: [PATCH] KT-7021 Don't mark companion object as unused if some stuff from it is used #KT-7021 fixed --- .../inspections/UnusedSymbolInspection.kt | 27 +++++++++---------- .../defaultObjectUsedInContainingClass.kt | 21 +++++++++++++++ .../object/inspectionData/expected.xml | 2 -- 3 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 idea/testData/inspections/unusedSymbol/object/defaultObjectUsedInContainingClass.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 4c249f2dab6..dea2a28b896 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -107,9 +107,6 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() { if (declaration is JetParameter && (declaration.getParent()?.getParent() !is JetClass || !declaration.hasValOrVarNode())) return if (declaration is JetNamedFunction && isConventionalName(declaration)) return - // TODO companion objects are temporarily disabled - if (isCompanionObject) return - // More expensive, resolve-based checks if (isEntryPoint(declaration)) return // properties can be referred by component1/component2, which is too expensive to search, don't mark them as unused @@ -165,24 +162,26 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() { val psiSearchHelper = PsiSearchHelper.SERVICE.getInstance(declaration.getProject()) val useScope = declaration.getUseScope() - if (useScope is GlobalSearchScope) { - var zeroOccurrences = true + if (!(declaration is JetObjectDeclaration && declaration.isCompanion())) { + if (useScope is GlobalSearchScope) { + var zeroOccurrences = true - for (name in listOf(declaration.getName()) + declaration.getAccessorNames() + declaration.getClassNameForCompanionObject().singletonOrEmptyList()) { - when (psiSearchHelper.isCheapEnoughToSearch(name, useScope, null, null)) { - ZERO_OCCURRENCES -> {} // go on, check other names - FEW_OCCURRENCES -> zeroOccurrences = false - TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used + for (name in listOf(declaration.getName()) + declaration.getAccessorNames() + declaration.getClassNameForCompanionObject().singletonOrEmptyList()) { + when (psiSearchHelper.isCheapEnoughToSearch(name, useScope, null, null)) { + ZERO_OCCURRENCES -> {} // go on, check other names + FEW_OCCURRENCES -> zeroOccurrences = false + TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used + } } - } - if (zeroOccurrences) { - return false + if (zeroOccurrences) { + return false + } } } val searchHelper: UsagesSearchHelper = when (declaration) { - is JetClass -> ClassUsagesSearchHelper(constructorUsages = true, nonConstructorUsages = true, skipImports = true) + is JetClassOrObject -> ClassUsagesSearchHelper(constructorUsages = true, nonConstructorUsages = true, skipImports = true) is JetNamedFunction -> FunctionUsagesSearchHelper(skipImports = true) is JetProperty, is JetParameter -> PropertyUsagesSearchHelper(skipImports = true) else -> DefaultSearchHelper() diff --git a/idea/testData/inspections/unusedSymbol/object/defaultObjectUsedInContainingClass.kt b/idea/testData/inspections/unusedSymbol/object/defaultObjectUsedInContainingClass.kt new file mode 100644 index 00000000000..5ac2b393a15 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/object/defaultObjectUsedInContainingClass.kt @@ -0,0 +1,21 @@ +class A { + init { + foo() + v + v + + v[v] // using companion object function by convention + } + + companion object { + fun foo() { + } + + val v = 42 + + fun Int.get(a: Int) = this + a + } +} + +fun main(args: Array) { + A() +} diff --git a/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml b/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml index 2702ba8ef35..46f1f56dd7f 100644 --- a/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml +++ b/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml @@ -32,7 +32,6 @@ Object 'A' is never used -