diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt index 3b0e1b8f4c..b46f04df56 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt @@ -68,7 +68,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { val name = importDirective.importPath?.importedName?.asString() val importPath = importDirective.importPath?.pathStr!! if (importDirective.aliasName == null && - importPath.startsWith(packageName) && + (packageName.isEmpty() || importPath.startsWith("$packageName.")) && importPath.substring(packageName.length + 1).indexOf('.') == -1) { emit(importDirective.startOffset, "Unnecessary import", true) if (autoCorrect) { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt index 98ed57799f..ad5f6cc9f8 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt @@ -47,6 +47,21 @@ class NoUnusedImportsRuleTest { )).isEmpty() } + @Test + fun testLintIssue204() { + assertThat(NoUnusedImportsRule().lint( + """ + package com.example.another + + import com.example.anotherThing + + class Foo { + val bar = anotherThing + } + """.trimIndent() + )).isEmpty() + } + @Test fun testLintDestructuringAssignment() { assertThat(NoUnusedImportsRule().lint(