From 2610dedcbd6a635ca31c632c605646cf8faff3fc Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 17 Jun 2020 22:02:30 +0700 Subject: [PATCH] KotlinUnusedImportInspection: support unresolved references #KT-31331 Fixed --- .../KotlinUnusedImportInspection.kt | 6 +- .../testData/inspections/unusedImport/file.kt | 6 +- .../unusedImport/inspectionData/expected.xml | 275 +++++++++++------- .../unusedImport/unresolvedAndUsed.kt | 7 + .../moveFileWithPackageRename.test | 3 +- .../shortenCompanionObject2/after/b/foo.kt | 2 - .../movePropertyToFile/after/a/main.kt | 2 +- .../movePropertyToFile/before/a/main.kt | 2 +- .../movePropertyToFile.test | 3 +- .../movePropertyToPackage/after/a/main.kt | 2 +- .../movePropertyToPackage/before/a/main.kt | 2 +- .../movePropertyToPackage.test | 3 +- 12 files changed, 194 insertions(+), 119 deletions(-) create mode 100644 idea/testData/inspections/unusedImport/unresolvedAndUsed.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinUnusedImportInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinUnusedImportInspection.kt index aab28c3f25a..bed3ada8347 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinUnusedImportInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinUnusedImportInspection.kt @@ -100,8 +100,11 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() { val importPath = directive.importPath ?: continue val isUsed = when { + importPath.importedName in optimizerData.unresolvedNames && + directive.targetDescriptors(resolutionFacade).isEmpty() -> true + !importPaths.add(importPath) -> false - importPath.isAllUnder -> importPath.fqName in parentFqNames + importPath.isAllUnder -> optimizerData.unresolvedNames.isNotEmpty() || importPath.fqName in parentFqNames importPath.fqName in fqNames -> importPath.importedName?.let { it in fqNames.getValue(importPath.fqName) } ?: false importPath.fqName in invokeFunctionCallFqNames -> true // case for type alias @@ -109,7 +112,6 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() { } if (!isUsed) { - if (directive.targetDescriptors(resolutionFacade).isEmpty()) continue // do not highlight unresolved imports as unused unusedImports += directive } } diff --git a/idea/testData/inspections/unusedImport/file.kt b/idea/testData/inspections/unusedImport/file.kt index 2e7ee69ba51..816731f1110 100644 --- a/idea/testData/inspections/unusedImport/file.kt +++ b/idea/testData/inspections/unusedImport/file.kt @@ -4,8 +4,8 @@ import java.sql.* // used import java.util.HashMap // unused import java.util.ArrayList // used -import java.unresolved.* // unused but unresolved -import java.net.Unresolved // unused but unresolved +import java.unresolved.* // unresolved and unused +import java.net.Unresolved // unresolved and unused import java.net.ConnectException as CE // unused import java.net.ConnectException as ConExc // used @@ -14,7 +14,7 @@ import RootPackageClass // unused because it's in the current package fun foo(list: ArrayList, p: RootPackageClass, e: ConExc) { list.add("") - Date() + Date(4) } class RootPackageClass diff --git a/idea/testData/inspections/unusedImport/inspectionData/expected.xml b/idea/testData/inspections/unusedImport/inspectionData/expected.xml index e713fe171cd..8bb7d560039 100644 --- a/idea/testData/inspections/unusedImport/inspectionData/expected.xml +++ b/idea/testData/inspections/unusedImport/inspectionData/expected.xml @@ -1,118 +1,183 @@ - - file.kt - 1 - light_idea_test_case - - Unused import directive - Unused import directive - + + file.kt + 1 + light_idea_test_case + + Unused import directive + Unused import directive + - - file.kt - 4 - light_idea_test_case - - Unused import directive - Unused import directive - + + file.kt + 4 + light_idea_test_case + + Unused import directive + Unused import directive + - - file.kt - 10 - light_idea_test_case - - Unused import directive - Unused import directive - + + file.kt + 10 + light_idea_test_case + + Unused import directive + Unused import directive + - - fileInPack1.kt - 3 - light_idea_test_case - - Unused import directive - Unused import directive - + + fileInPack1.kt + 3 + light_idea_test_case + + Unused import directive + Unused import directive + - - fileInPack2.kt - 3 - light_idea_test_case - - Unused import directive - Unused import directive - - - - unusedStarWhenExplicit.kt - 2 - light_idea_test_case - - Unused import directive - Unused import directive - + + fileInPack2.kt + 3 + light_idea_test_case + + Unused import directive + Unused import directive + - - unusedStarWhenExplicit.kt - 4 - light_idea_test_case - - Unused import directive - Unused import directive - + + unusedStarWhenExplicit.kt + 2 + light_idea_test_case + + Unused import directive + Unused import directive + - - duplicatedImports.kt - 2 - light_idea_test_case - - Unused import directive - Unused import directive - + + unusedStarWhenExplicit.kt + 4 + light_idea_test_case + + Unused import directive + Unused import directive + - - duplicatedImports.kt - 5 - light_idea_test_case - - Unused import directive - Unused import directive - + + duplicatedImports.kt + 2 + light_idea_test_case + + Unused import directive + Unused import directive + - - duplicatedImports.kt - 7 - light_idea_test_case - - Unused import directive - Unused import directive - + + duplicatedImports.kt + 5 + light_idea_test_case + + Unused import directive + Unused import directive + - - duplicatedImports.kt - 8 - light_idea_test_case - - Unused import directive - Unused import directive - + + duplicatedImports.kt + 7 + light_idea_test_case + + Unused import directive + Unused import directive + - - kDocReferences.kt - 2 - light_idea_test_case - - Unused import directive - Unused import directive - + + duplicatedImports.kt + 8 + light_idea_test_case + + Unused import directive + Unused import directive + - - provideDelegate.kt - 2 - light_idea_test_case - - Unused import directive - Unused import directive - + + kDocReferences.kt + 2 + light_idea_test_case + + Unused import directive + Unused import directive + + + + provideDelegate.kt + 2 + light_idea_test_case + + Unused import directive + Unused import directive + + + + file.kt + 7 + light_idea_test_case + file + + Unused import directive + Unused import directive + import java.unresolved.* // unused but unresolved + 0 + 49 + + + + file.kt + 8 + light_idea_test_case + file + + Unused import directive + Unused import directive + import java.net.Unresolved // unused but unresolved + 0 + 51 + + + + file.kt + 13 + light_idea_test_case + file + + Unused import directive + Unused import directive + import RootPackageClass // unused because it's in the current package + 0 + 69 + + + + noUnusedImportForImportsWithSameName.kt + 2 + light_idea_test_case + noUnusedImportForImportsWithSameName + + Unused import directive + Unused import directive + import name as names + 0 + 20 + + + + unresolvedAndUsed.kt + 3 + light_idea_test_case + unresolvedAndUsed + + Unused import directive + Unused import directive + import test.B // unresolved and unused + 0 + 38 + diff --git a/idea/testData/inspections/unusedImport/unresolvedAndUsed.kt b/idea/testData/inspections/unusedImport/unresolvedAndUsed.kt new file mode 100644 index 00000000000..525af0e1701 --- /dev/null +++ b/idea/testData/inspections/unusedImport/unresolvedAndUsed.kt @@ -0,0 +1,7 @@ +import test.* // unresolved and used +import test.A // unresolved and used +import test.B // unresolved and unused + +fun a(p: A) { + +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveFile/moveFileWithPackageRename/moveFileWithPackageRename.test b/idea/testData/refactoring/move/kotlin/moveFile/moveFileWithPackageRename/moveFileWithPackageRename.test index 7a1f1a0a1bf..12ba840109c 100644 --- a/idea/testData/refactoring/move/kotlin/moveFile/moveFileWithPackageRename/moveFileWithPackageRename.test +++ b/idea/testData/refactoring/move/kotlin/moveFile/moveFileWithPackageRename/moveFileWithPackageRename.test @@ -1,5 +1,6 @@ { "mainFile": "a/main.kt", "type": "MOVE_FILES", - "targetPackage": "b" + "targetPackage": "b", + "withRuntime": "true" } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/foo.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/foo.kt index 7fd9c9fc037..9496b470747 100644 --- a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/foo.kt +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/foo.kt @@ -1,4 +1,2 @@ package b -import java.util.function.IntPredicate - diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/after/a/main.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/after/a/main.kt index 38f0bec9dde..4cfe652f4cf 100644 --- a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/after/a/main.kt +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/after/a/main.kt @@ -4,6 +4,6 @@ import b.test class Test { fun foo() { - test() + test } } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/before/a/main.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/before/a/main.kt index 3b47d299009..0d7bcbebb2a 100644 --- a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/before/a/main.kt +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/before/a/main.kt @@ -25,6 +25,6 @@ var test: String class Test { fun foo() { - test() + test } } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/movePropertyToFile.test b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/movePropertyToFile.test index 351facb56ee..b26e41926cc 100644 --- a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/movePropertyToFile.test +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToFile/movePropertyToFile.test @@ -1,5 +1,6 @@ { "mainFile": "a/main.kt", "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", - "targetFile": "b/dependency.kt" + "targetFile": "b/dependency.kt", + "withRuntime": "true" } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/after/a/main.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/after/a/main.kt index 38f0bec9dde..4cfe652f4cf 100644 --- a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/after/a/main.kt +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/after/a/main.kt @@ -4,6 +4,6 @@ import b.test class Test { fun foo() { - test() + test } } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/before/a/main.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/before/a/main.kt index 3b47d299009..0d7bcbebb2a 100644 --- a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/before/a/main.kt +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/before/a/main.kt @@ -25,6 +25,6 @@ var test: String class Test { fun foo() { - test() + test } } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/movePropertyToPackage.test b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/movePropertyToPackage.test index 10641ec2d49..ce7b179fd48 100644 --- a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/movePropertyToPackage.test +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/movePropertyToPackage.test @@ -1,5 +1,6 @@ { "mainFile": "a/main.kt", "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", - "targetPackage": "b" + "targetPackage": "b", + "withRuntime": "true" }