From c9181df275dd71e2663da7fa9579067be3bb991f Mon Sep 17 00:00:00 2001 From: Stanley Shyiko Date: Wed, 2 May 2018 13:27:08 -0700 Subject: [PATCH] Fixed #204 - Unused import false positive (x.y.zNNN import inside x.y.z package) --- .../ruleset/standard/NoUnusedImportsRule.kt | 2 +- .../ruleset/standard/NoUnusedImportsRuleTest.kt | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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(