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..6752e69ebc 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 @@ -86,6 +86,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { ref.add(Reference("*", false)) parentExpressions.clear() imports.clear() + usedImportPaths.clear() node.visit { vnode -> val psi = vnode.psi val type = vnode.elementType 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..8e5d2074bf 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 @@ -729,4 +729,34 @@ class NoUnusedImportsRuleTest { ) ).isEmpty() } + + @Test + fun `repeated invocations on the same rule instance should work`() { + val rule = NoUnusedImportsRule() + assertThat( + rule.lint( + """ + import foo.bar + + fun main() { + bar() + } + """.trimIndent() + ) + ).isEmpty() + + assertThat( + rule.lint( + """ + import foo.bar + import foo.baz + + fun main() { + bar() + baz() + } + """.trimIndent() + ) + ).isEmpty() + } }