KotlinUnusedImportInspection: support unresolved references
#KT-31331 Fixed
This commit is contained in:
@@ -100,8 +100,11 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() {
|
|||||||
val importPath = directive.importPath ?: continue
|
val importPath = directive.importPath ?: continue
|
||||||
|
|
||||||
val isUsed = when {
|
val isUsed = when {
|
||||||
|
importPath.importedName in optimizerData.unresolvedNames &&
|
||||||
|
directive.targetDescriptors(resolutionFacade).isEmpty() -> true
|
||||||
|
|
||||||
!importPaths.add(importPath) -> false
|
!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 fqNames -> importPath.importedName?.let { it in fqNames.getValue(importPath.fqName) } ?: false
|
||||||
importPath.fqName in invokeFunctionCallFqNames -> true
|
importPath.fqName in invokeFunctionCallFqNames -> true
|
||||||
// case for type alias
|
// case for type alias
|
||||||
@@ -109,7 +112,6 @@ class KotlinUnusedImportInspection : AbstractKotlinInspection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isUsed) {
|
if (!isUsed) {
|
||||||
if (directive.targetDescriptors(resolutionFacade).isEmpty()) continue // do not highlight unresolved imports as unused
|
|
||||||
unusedImports += directive
|
unusedImports += directive
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -4,8 +4,8 @@ import java.sql.* // used
|
|||||||
import java.util.HashMap // unused
|
import java.util.HashMap // unused
|
||||||
import java.util.ArrayList // used
|
import java.util.ArrayList // used
|
||||||
|
|
||||||
import java.unresolved.* // unused but unresolved
|
import java.unresolved.* // unresolved and unused
|
||||||
import java.net.Unresolved // unused but unresolved
|
import java.net.Unresolved // unresolved and unused
|
||||||
|
|
||||||
import java.net.ConnectException as CE // unused
|
import java.net.ConnectException as CE // unused
|
||||||
import java.net.ConnectException as ConExc // used
|
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<String>, p: RootPackageClass, e: ConExc) {
|
fun foo(list: ArrayList<String>, p: RootPackageClass, e: ConExc) {
|
||||||
list.add("")
|
list.add("")
|
||||||
Date()
|
Date(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
class RootPackageClass
|
class RootPackageClass
|
||||||
|
|||||||
+169
-104
@@ -1,118 +1,183 @@
|
|||||||
<problems>
|
<problems>
|
||||||
<problem>
|
<problem>
|
||||||
<file>file.kt</file>
|
<file>file.kt</file>
|
||||||
<line>1</line>
|
<line>1</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="simple.kt" />
|
<entry_point TYPE="file" FQNAME="simple.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>file.kt</file>
|
<file>file.kt</file>
|
||||||
<line>4</line>
|
<line>4</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="simple.kt" />
|
<entry_point TYPE="file" FQNAME="simple.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>file.kt</file>
|
<file>file.kt</file>
|
||||||
<line>10</line>
|
<line>10</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="simple.kt" />
|
<entry_point TYPE="file" FQNAME="simple.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>fileInPack1.kt</file>
|
<file>fileInPack1.kt</file>
|
||||||
<line>3</line>
|
<line>3</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="fileInPack1.kt" />
|
<entry_point TYPE="file" FQNAME="fileInPack1.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>fileInPack2.kt</file>
|
<file>fileInPack2.kt</file>
|
||||||
<line>3</line>
|
<line>3</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="fileInPack2.kt" />
|
<entry_point TYPE="file" FQNAME="fileInPack2.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>unusedStarWhenExplicit.kt</file>
|
<file>unusedStarWhenExplicit.kt</file>
|
||||||
<line>2</line>
|
<line>2</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="unusedStarWhenExplicit.kt" />
|
<entry_point TYPE="file" FQNAME="unusedStarWhenExplicit.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>unusedStarWhenExplicit.kt</file>
|
<file>unusedStarWhenExplicit.kt</file>
|
||||||
<line>4</line>
|
<line>4</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="unusedStarWhenExplicit.kt" />
|
<entry_point TYPE="file" FQNAME="unusedStarWhenExplicit.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>duplicatedImports.kt</file>
|
<file>duplicatedImports.kt</file>
|
||||||
<line>2</line>
|
<line>2</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="duplicatedImports.kt" />
|
<entry_point TYPE="file" FQNAME="duplicatedImports.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>duplicatedImports.kt</file>
|
<file>duplicatedImports.kt</file>
|
||||||
<line>5</line>
|
<line>5</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="duplicatedImports.kt" />
|
<entry_point TYPE="file" FQNAME="duplicatedImports.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>duplicatedImports.kt</file>
|
<file>duplicatedImports.kt</file>
|
||||||
<line>7</line>
|
<line>7</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="duplicatedImports.kt" />
|
<entry_point TYPE="file" FQNAME="duplicatedImports.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>duplicatedImports.kt</file>
|
<file>duplicatedImports.kt</file>
|
||||||
<line>8</line>
|
<line>8</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="duplicatedImports.kt" />
|
<entry_point TYPE="file" FQNAME="duplicatedImports.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>kDocReferences.kt</file>
|
<file>kDocReferences.kt</file>
|
||||||
<line>2</line>
|
<line>2</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="kDocReferences.kt" />
|
<entry_point TYPE="file" FQNAME="kDocReferences.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
<problem>
|
<problem>
|
||||||
<file>provideDelegate.kt</file>
|
<file>provideDelegate.kt</file>
|
||||||
<line>2</line>
|
<line>2</line>
|
||||||
<module>light_idea_test_case</module>
|
<module>light_idea_test_case</module>
|
||||||
<entry_point TYPE="file" FQNAME="provideDelegate.kt" />
|
<entry_point TYPE="file" FQNAME="provideDelegate.kt"/>
|
||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
<description>Unused import directive</description>
|
<description>Unused import directive</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>file.kt</file>
|
||||||
|
<line>7</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<package>file</package>
|
||||||
|
<entry_point TYPE="file" FQNAME="file.kt"/>
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
|
<description>Unused import directive</description>
|
||||||
|
<highlighted_element>import java.unresolved.* // unused but unresolved</highlighted_element>
|
||||||
|
<offset>0</offset>
|
||||||
|
<length>49</length>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>file.kt</file>
|
||||||
|
<line>8</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<package>file</package>
|
||||||
|
<entry_point TYPE="file" FQNAME="file.kt"/>
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
|
<description>Unused import directive</description>
|
||||||
|
<highlighted_element>import java.net.Unresolved // unused but unresolved</highlighted_element>
|
||||||
|
<offset>0</offset>
|
||||||
|
<length>51</length>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>file.kt</file>
|
||||||
|
<line>13</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<package>file</package>
|
||||||
|
<entry_point TYPE="file" FQNAME="file.kt"/>
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
|
<description>Unused import directive</description>
|
||||||
|
<highlighted_element>import RootPackageClass // unused because it's in the current package</highlighted_element>
|
||||||
|
<offset>0</offset>
|
||||||
|
<length>69</length>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>noUnusedImportForImportsWithSameName.kt</file>
|
||||||
|
<line>2</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<package>noUnusedImportForImportsWithSameName</package>
|
||||||
|
<entry_point TYPE="file" FQNAME="noUnusedImportForImportsWithSameName.kt"/>
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
|
<description>Unused import directive</description>
|
||||||
|
<highlighted_element>import name as names</highlighted_element>
|
||||||
|
<offset>0</offset>
|
||||||
|
<length>20</length>
|
||||||
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>unresolvedAndUsed.kt</file>
|
||||||
|
<line>3</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<package>unresolvedAndUsed</package>
|
||||||
|
<entry_point TYPE="file" FQNAME="unresolvedAndUsed.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused import directive</problem_class>
|
||||||
|
<description>Unused import directive</description>
|
||||||
|
<highlighted_element>import test.B // unresolved and unused</highlighted_element>
|
||||||
|
<offset>0</offset>
|
||||||
|
<length>38</length>
|
||||||
|
</problem>
|
||||||
</problems>
|
</problems>
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|
||||||
|
}
|
||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"mainFile": "a/main.kt",
|
"mainFile": "a/main.kt",
|
||||||
"type": "MOVE_FILES",
|
"type": "MOVE_FILES",
|
||||||
"targetPackage": "b"
|
"targetPackage": "b",
|
||||||
|
"withRuntime": "true"
|
||||||
}
|
}
|
||||||
|
|||||||
-2
@@ -1,4 +1,2 @@
|
|||||||
package b
|
package b
|
||||||
|
|
||||||
import java.util.function.IntPredicate
|
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
-1
@@ -4,6 +4,6 @@ import b.test
|
|||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
fun foo() {
|
fun foo() {
|
||||||
test()
|
test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+1
-1
@@ -25,6 +25,6 @@ var <caret>test: String
|
|||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
fun foo() {
|
fun foo() {
|
||||||
test()
|
test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"mainFile": "a/main.kt",
|
"mainFile": "a/main.kt",
|
||||||
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
|
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
|
||||||
"targetFile": "b/dependency.kt"
|
"targetFile": "b/dependency.kt",
|
||||||
|
"withRuntime": "true"
|
||||||
}
|
}
|
||||||
|
|||||||
idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/after/a/main.kt
Vendored
+1
-1
@@ -4,6 +4,6 @@ import b.test
|
|||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
fun foo() {
|
fun foo() {
|
||||||
test()
|
test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -25,6 +25,6 @@ var <caret>test: String
|
|||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
fun foo() {
|
fun foo() {
|
||||||
test()
|
test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"mainFile": "a/main.kt",
|
"mainFile": "a/main.kt",
|
||||||
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
|
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
|
||||||
"targetPackage": "b"
|
"targetPackage": "b",
|
||||||
|
"withRuntime": "true"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user