Unused import: do not report for 'invoke' function import that is used from same file
#KT-24281 Fixed
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
8aa120576b
commit
8c4fdd1edd
@@ -47,11 +47,12 @@ import org.jetbrains.kotlin.idea.core.targetDescriptors
|
|||||||
import org.jetbrains.kotlin.idea.imports.KotlinImportOptimizer
|
import org.jetbrains.kotlin.idea.imports.KotlinImportOptimizer
|
||||||
import org.jetbrains.kotlin.idea.imports.OptimizedImportsBuilder
|
import org.jetbrains.kotlin.idea.imports.OptimizedImportsBuilder
|
||||||
import org.jetbrains.kotlin.idea.imports.importableFqName
|
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||||
|
import org.jetbrains.kotlin.idea.references.KtInvokeFunctionReference
|
||||||
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
|
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.KtCodeFragment
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
|
||||||
import org.jetbrains.kotlin.psi.KtImportDirective
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
||||||
import org.jetbrains.kotlin.resolve.ImportPath
|
import org.jetbrains.kotlin.resolve.ImportPath
|
||||||
|
|
||||||
@@ -86,6 +87,11 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val invokeFunctionCallFqNames = optimizerData.references.mapNotNull {
|
||||||
|
val reference = (it.element as? KtCallExpression)?.mainReference as? KtInvokeFunctionReference ?: return@mapNotNull null
|
||||||
|
(reference.resolve() as? KtNamedFunction)?.descriptor?.importableFqName
|
||||||
|
}
|
||||||
|
|
||||||
val importPaths = HashSet<ImportPath>(directives.size)
|
val importPaths = HashSet<ImportPath>(directives.size)
|
||||||
val unusedImports = ArrayList<KtImportDirective>()
|
val unusedImports = ArrayList<KtImportDirective>()
|
||||||
|
|
||||||
@@ -97,6 +103,7 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() {
|
|||||||
!importPaths.add(importPath) -> false
|
!importPaths.add(importPath) -> false
|
||||||
importPath.isAllUnder -> importPath.fqName in parentFqNames
|
importPath.isAllUnder -> importPath.fqName in parentFqNames
|
||||||
importPath.fqName in fqNames -> importPath.importedName?.let { it in fqNames.getValue(importPath.fqName) } ?: false
|
importPath.fqName in fqNames -> importPath.importedName?.let { it in fqNames.getValue(importPath.fqName) } ?: false
|
||||||
|
importPath.fqName in invokeFunctionCallFqNames -> true
|
||||||
// case for type alias
|
// case for type alias
|
||||||
else -> directive.targetDescriptors(resolutionFacade).firstOrNull()?.let { it.importableFqName in fqNames } ?: false
|
else -> directive.targetDescriptors(resolutionFacade).firstOrNull()?.let { it.importableFqName in fqNames } ?: false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// "Optimize imports" "false"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
// ACTION: Introduce import alias
|
||||||
|
package ppp
|
||||||
|
|
||||||
|
import ppp.invoke<caret>
|
||||||
|
|
||||||
|
object Bar
|
||||||
|
object Foo {
|
||||||
|
val bar = Bar
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Foo.bar() = 1
|
||||||
|
operator fun Bar.invoke() = 2
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
println(Foo.bar())
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// "Optimize imports" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
package ppp
|
||||||
|
|
||||||
|
import ppp.invoke<caret>
|
||||||
|
|
||||||
|
object Bar
|
||||||
|
object Foo {
|
||||||
|
// val bar = Bar
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Foo.bar() = 1
|
||||||
|
operator fun Bar.invoke() = 2
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
println(Foo.bar())
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// "Optimize imports" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
package ppp
|
||||||
|
|
||||||
|
object Bar
|
||||||
|
object Foo {
|
||||||
|
// val bar = Bar
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Foo.bar() = 1
|
||||||
|
operator fun Bar.invoke() = 2
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
println(Foo.bar())
|
||||||
|
}
|
||||||
@@ -9726,6 +9726,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
public void testImportAlias() throws Exception {
|
public void testImportAlias() throws Exception {
|
||||||
runTest("idea/testData/quickfix/optimizeImports/importAlias.kt");
|
runTest("idea/testData/quickfix/optimizeImports/importAlias.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("invoke.kt")
|
||||||
|
public void testInvoke() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/optimizeImports/invoke.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("invoke2.kt")
|
||||||
|
public void testInvoke2() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/optimizeImports/invoke2.kt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/override")
|
@TestMetadata("idea/testData/quickfix/override")
|
||||||
|
|||||||
Reference in New Issue
Block a user