Unused symbol: handling of import as alias not only for objects / enums #KT-11933 Fixed
This commit is contained in:
@@ -244,6 +244,9 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
|
|||||||
|
|
||||||
val import = ref.element.getParentOfType<KtImportDirective>(false)
|
val import = ref.element.getParentOfType<KtImportDirective>(false)
|
||||||
if (import != null) {
|
if (import != null) {
|
||||||
|
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
|
// check if we import member(s) from object or enum and search for their usages
|
||||||
if (declaration is KtObjectDeclaration || (declaration is KtClass && declaration.isEnum())) {
|
if (declaration is KtObjectDeclaration || (declaration is KtClass && declaration.isEnum())) {
|
||||||
if (import.isAllUnder) {
|
if (import.isAllUnder) {
|
||||||
@@ -251,9 +254,6 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
|
|||||||
as? KtClassOrObject ?: return true
|
as? KtClassOrObject ?: return true
|
||||||
return importedFrom.declarations.none { it is KtNamedDeclaration && hasNonTrivialUsages(it) }
|
return importedFrom.declarations.none { it is KtNamedDeclaration && hasNonTrivialUsages(it) }
|
||||||
}
|
}
|
||||||
else if (import.aliasName != null && import.aliasName != declaration.name) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
if (import.importedFqName != declaration.fqName) {
|
if (import.importedFqName != declaration.fqName) {
|
||||||
val importedDeclaration = import.importedReference?.getQualifiedElementSelector()?.mainReference?.resolve() as? KtNamedDeclaration
|
val importedDeclaration = import.importedReference?.getQualifiedElementSelector()?.mainReference?.resolve() as? KtNamedDeclaration
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// "Safe delete 'Imported'" "false"
|
||||||
|
// ACTION: Create test
|
||||||
|
// ACTION: Move 'ImportedClass' to separate file
|
||||||
|
import ImportedClass as ClassAlias
|
||||||
|
|
||||||
|
class <caret>ImportedClass
|
||||||
|
|
||||||
|
fun use() {
|
||||||
|
ClassAlias().hashCode()
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// "Safe delete 'useMe'" "false"
|
||||||
|
// ACTION: Convert function to property
|
||||||
|
// ACTION: Convert to block body
|
||||||
|
// ACTION: Create test
|
||||||
|
// ACTION: Specify return type explicitly
|
||||||
|
|
||||||
|
import useMe as used
|
||||||
|
|
||||||
|
fun <caret>useMe() = 0
|
||||||
|
|
||||||
|
fun foo() = used()
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
import Imported as Alias
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
Alias.hashCode()
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// "Safe delete 'Imported'" "false"
|
||||||
|
// ACTION: Create test
|
||||||
|
object <caret>Imported
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// "Safe delete 'usedVar'" "false"
|
||||||
|
// ACTION: Specify type explicitly
|
||||||
|
import usedVar as used
|
||||||
|
|
||||||
|
var <caret>usedVar = 0
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
used++
|
||||||
|
}
|
||||||
@@ -1529,6 +1529,21 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/quickfix/removeUnused")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class RemoveUnused extends AbstractQuickFixMultiFileTest {
|
||||||
|
public void testAllFilesPresentInRemoveUnused() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeUnused"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedObjectAsAliasMulti.before.Main.kt")
|
||||||
|
public void testUsedObjectAsAliasMulti() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/usedObjectAsAliasMulti.before.Main.kt");
|
||||||
|
doTestWithExtraFile(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/suppress")
|
@TestMetadata("idea/testData/quickfix/suppress")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
@@ -6856,11 +6856,29 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedClassAsAlias.kt")
|
||||||
|
public void testUsedClassAsAlias() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/usedClassAsAlias.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedFunAsAlias.kt")
|
||||||
|
public void testUsedFunAsAlias() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/usedFunAsAlias.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("usedObjectAsAlias.kt")
|
@TestMetadata("usedObjectAsAlias.kt")
|
||||||
public void testUsedObjectAsAlias() throws Exception {
|
public void testUsedObjectAsAlias() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/usedObjectAsAlias.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/usedObjectAsAlias.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedVarAsAlias.kt")
|
||||||
|
public void testUsedVarAsAlias() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/usedVarAsAlias.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/removeUnusedReceiver")
|
@TestMetadata("idea/testData/quickfix/removeUnusedReceiver")
|
||||||
|
|||||||
Reference in New Issue
Block a user