Fix false positive "Unused import" for type alias
#KT-29977 Fixed
This commit is contained in:
@@ -41,6 +41,7 @@ import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import com.intellij.psi.util.PsiUtilBase
|
||||
import com.intellij.util.DocumentUtil
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.core.targetDescriptors
|
||||
import org.jetbrains.kotlin.idea.imports.KotlinImportOptimizer
|
||||
import org.jetbrains.kotlin.idea.imports.OptimizedImportsBuilder
|
||||
@@ -91,6 +92,7 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() {
|
||||
val importPaths = HashSet<ImportPath>(directives.size)
|
||||
val unusedImports = ArrayList<KtImportDirective>()
|
||||
|
||||
val resolutionFacade = file.getResolutionFacade()
|
||||
for (directive in directives) {
|
||||
val importPath = directive.importPath ?: continue
|
||||
if (importPath.alias != null) continue // highlighting of unused alias imports not supported yet
|
||||
@@ -98,11 +100,13 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() {
|
||||
val isUsed = when {
|
||||
!importPaths.add(importPath) -> false
|
||||
importPath.isAllUnder -> importPath.fqName in parentFqNames
|
||||
else -> importPath.fqName in fqNames
|
||||
importPath.fqName in fqNames -> true
|
||||
// case for type alias
|
||||
else -> directive.targetDescriptors(resolutionFacade).firstOrNull()?.let { it.importableFqName in fqNames } ?: false
|
||||
}
|
||||
|
||||
if (!isUsed) {
|
||||
if (directive.targetDescriptors().isEmpty()) continue // do not highlight unresolved imports as unused
|
||||
if (directive.targetDescriptors(resolutionFacade).isEmpty()) continue // do not highlight unresolved imports as unused
|
||||
unusedImports += directive
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
package test
|
||||
|
||||
import MyEnum
|
||||
|
||||
typealias Other = MyEnum
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Optimize imports" "false"
|
||||
// ACTION: Introduce import alias
|
||||
|
||||
import test.Other.HELLO<caret>
|
||||
|
||||
val ONE = HELLO
|
||||
|
||||
enum class MyEnum {
|
||||
HELLO
|
||||
}
|
||||
+5
@@ -3291,6 +3291,11 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
public void testNotRemoveImportsForTypeAliases() throws Exception {
|
||||
runTest("idea/testData/quickfix/optimizeImports/notRemoveImportsForTypeAliases.before.Main.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("notRemoveImportsForTypeAliases2.before.Main.kt")
|
||||
public void testNotRemoveImportsForTypeAliases2() throws Exception {
|
||||
runTest("idea/testData/quickfix/optimizeImports/notRemoveImportsForTypeAliases2.before.Main.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/override")
|
||||
|
||||
Reference in New Issue
Block a user