diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRule.kt index ec4b229d15..0f8d6b5a28 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRule.kt @@ -19,7 +19,6 @@ import org.jetbrains.kotlin.psi.KtCallExpression import org.jetbrains.kotlin.psi.KtDotQualifiedExpression import org.jetbrains.kotlin.psi.KtImportDirective import org.jetbrains.kotlin.psi.KtPackageDirective -import org.jetbrains.kotlin.resolve.ImportPath class NoUnusedImportsRule : Rule("no-unused-imports") { @@ -71,7 +70,6 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { private val ref = mutableSetOf() private val parentExpressions = mutableSetOf() private val imports = mutableSetOf() - private val usedImportPaths = mutableSetOf() private var packageName = "" private var rootNode: ASTNode? = null @@ -101,11 +99,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { ) { ref.add(Reference(text.removeBackticks(), psi.parentDotQualifiedExpression() != null)) } else if (type == IMPORT_DIRECTIVE) { - val importPath = (vnode.psi as KtImportDirective).importPath!! - if (!usedImportPaths.add(importPath)) { - emit(vnode.startOffset, "Unused import", false) - } - imports += importPath.pathStr.removeBackticks().trim() + imports += (vnode.psi as KtImportDirective).importPath!!.pathStr.removeBackticks().trim() } } val directCalls = ref.filter { !it.inDotQualifiedExpression }.map { it.text } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt index 231c8443e5..9c36ef5b82 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt @@ -192,88 +192,6 @@ class NoUnusedImportsRuleTest { ) } - @Test - fun testRepeatedImport() { - assertThat( - NoUnusedImportsRule().lint( - """ - - - import org.repository.RepositoryPolicy - import org.repository.any - import org.repository.all - import org.repository.any - fun main() { - RepositoryPolicy( - any(false), all("trial") - ) - } - """.trimIndent() - ) - ).isEqualTo( - listOf( - LintError(6, 1, "no-unused-imports", "Unused import") - ) - ) - } - - @Test - fun testRepeatedImportTwice() { - assertThat( - NoUnusedImportsRule().lint( - """ - - - import org.repository.RepositoryPolicy - import org.repository.any - import org.repository.all - import org.repository.any - import org.repository.any - fun main() { - RepositoryPolicy( - any(false), all("trial") - ) - } - """.trimIndent() - ) - ).isEqualTo( - listOf( - LintError(6, 1, "no-unused-imports", "Unused import"), - LintError(7, 1, "no-unused-imports", "Unused import") - - ) - ) - } - - @Test - fun testRepeatedImportsWithDistinctIncidences() { - assertThat( - NoUnusedImportsRule().lint( - """ - - - import org.repository.RepositoryPolicy - import org.repository.any - import org.repository.all - import org.repository.many - import org.repository.any - import org.repository.none - import org.repository.all - fun main() { - RepositoryPolicy( - any(false), all("trial"), many("hello"), none("goodbye") - ) - } - """.trimIndent() - ) - ).isEqualTo( - listOf( - LintError(7, 1, "no-unused-imports", "Unused import"), - LintError(9, 1, "no-unused-imports", "Unused import") - ) - ) - } - @Test fun testFormat() { assertThat(