Unused symbol: handle imports from nested objects correctly
So #KT-17437 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
7e3d3bde74
commit
37d2386a0b
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user