diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 71a40fea87e..8db5b3cd097 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -252,8 +252,8 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { if (import.aliasName != null && import.aliasName != declaration.name) { return false } - // check if we import member(s) from object or enum and search for their usages - if (declaration is KtObjectDeclaration || (declaration is KtClass && declaration.isEnum())) { + // check if we import member(s) from object / nested object / enum and search for their usages + if (declaration is KtClassOrObject) { if (import.isAllUnder) { val importedFrom = import.importedReference?.getQualifiedElementSelector()?.mainReference?.resolve() as? KtClassOrObject ?: return true @@ -264,7 +264,11 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { val importedDeclaration = import.importedReference?.getQualifiedElementSelector()?.mainReference?.resolve() as? KtNamedDeclaration ?: return true - return declaration !in importedDeclaration.parentsWithSelf && !hasNonTrivialUsages(importedDeclaration) + if (declaration is KtObjectDeclaration || + (declaration is KtClass && declaration.isEnum()) || + importedDeclaration.containingClassOrObject is KtObjectDeclaration) { + return declaration !in importedDeclaration.parentsWithSelf && !hasNonTrivialUsages(importedDeclaration) + } } } } diff --git a/idea/testData/inspectionsLocal/unusedSymbol/companionViaImport.kt b/idea/testData/inspectionsLocal/unusedSymbol/companionViaImport.kt new file mode 100644 index 00000000000..e31d68950cd --- /dev/null +++ b/idea/testData/inspectionsLocal/unusedSymbol/companionViaImport.kt @@ -0,0 +1,14 @@ +// PROBLEM: none + +import My.Companion.create + +class My { + companion object { + fun create() = My() + } +} + +fun test() { + val my = create() + my.hashCode() +} \ 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 45a15659500..1ad3dae10a0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1850,6 +1850,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/unusedSymbol"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("companionViaImport.kt") + public void testCompanionViaImport() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedSymbol/companionViaImport.kt"); + doTest(fileName); + } + @TestMetadata("inAnonymous.kt") public void testInAnonymous() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedSymbol/inAnonymous.kt");