Unused symbol: handle imports from nested objects correctly

So #KT-17437 Fixed
This commit is contained in:
Mikhail Glukhikh
2017-09-08 18:56:14 +03:00
committed by Mikhail Glukhikh
parent 7e3d3bde74
commit 37d2386a0b
3 changed files with 27 additions and 3 deletions
@@ -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)
}
}
}
}
@@ -0,0 +1,14 @@
// PROBLEM: none
import My.Companion.create
class <caret>My {
companion object {
fun create() = My()
}
}
fun test() {
val my = create()
my.hashCode()
}
@@ -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");