diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 1b6551bee45..56a2b3173c8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -27,7 +27,6 @@ import com.intellij.codeInspection.ex.EntryPointsManager import com.intellij.codeInspection.ex.EntryPointsManagerBase import com.intellij.codeInspection.ex.EntryPointsManagerImpl import com.intellij.openapi.project.Project -import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiElement import com.intellij.psi.PsiElementVisitor @@ -96,10 +95,6 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { return false } - private fun KtObjectDeclaration.hasSerializationImplicitlyUsedField(): Boolean { - return declarations.any { it is KtProperty && it.isSerializationImplicitlyUsedField() } - } - // variation of IDEA's AnnotationUtil.checkAnnotatedUsingPatterns() private fun checkAnnotatedUsingPatterns(annotated: Annotated, annotationPatterns: Collection): Boolean { val annotationsPresent = annotated.annotations @@ -140,7 +135,7 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { if (!ProjectRootsUtil.isInProjectSource(declaration)) return // Simple PSI-based checks - val isCompanionObject = declaration is KtObjectDeclaration && declaration.isCompanion() + if (declaration is KtObjectDeclaration && declaration.isCompanion()) return // never mark companion object as unused (there are too many reasons it can be needed for) if (declaration.name == null) return if (declaration is KtEnumEntry) return if (declaration.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return @@ -152,7 +147,6 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { if (declaration.resolveToDescriptorIfAny() == null) return if (isEntryPoint(declaration)) return if (declaration is KtProperty && declaration.isSerializationImplicitlyUsedField()) return - if (isCompanionObject && (declaration as KtObjectDeclaration).hasSerializationImplicitlyUsedField()) return // properties can be referred by component1/component2, which is too expensive to search, don't mark them as unused if (declaration is KtParameter && declaration.dataClassComponentFunction() != null) return @@ -160,16 +154,9 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { if (hasNonTrivialUsages(declaration)) return if (declaration is KtClassOrObject && classOrObjectHasTextUsages(declaration)) return - val (inspectionTarget, textRange) = if (isCompanionObject && declaration.nameIdentifier == null) { - val objectKeyword = (declaration as KtObjectDeclaration).getObjectKeyword()!! - Pair(declaration, TextRange(0, objectKeyword.startOffsetInParent + objectKeyword.textLength)) - } else { - Pair(declaration.nameIdentifier!!, null) - } - val problemDescriptor = holder.manager.createProblemDescriptor( - inspectionTarget, - textRange, + declaration.nameIdentifier!!, + null, KotlinBundle.message(messageKey, declaration.name), ProblemHighlightType.LIKE_UNUSED_SYMBOL, true, diff --git a/idea/testData/inspections/unusedSymbol/object/companionObjectBaseMemberUsed.kt b/idea/testData/inspections/unusedSymbol/object/companionObjectBaseMemberUsed.kt new file mode 100644 index 00000000000..77aa4bc675d --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/object/companionObjectBaseMemberUsed.kt @@ -0,0 +1,19 @@ +class Logger { + fun log(){} +} + +abstract class WithLogging { + val logger = Logger() +} + +class C { + companion object: WithLogging() + + fun foo() { + logger.log() + } +} + +fun main(args: Array) { + C() +} diff --git a/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml b/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml index a94bb72e695..4f234e1dfe5 100644 --- a/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml +++ b/idea/testData/inspections/unusedSymbol/object/inspectionData/expected.xml @@ -35,24 +35,6 @@ Object 'A' is never used - - companionObjectUnused.kt - 2 - light_idea_test_case - - Unused Symbol - Object 'Companion' is never used - - - - companionObjectUnused.kt - 7 - light_idea_test_case - - Unused Symbol - Object 'Named' is never used - - usedViaMemberImport.kt 11