Skip to content

Commit

Permalink
NoUnusedImportsRule: fix false positive for static java function imports
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama committed Sep 7, 2020
1 parent b01ba62 commit fcdb9db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
private val conditionalWhitelist = mapOf<(String) -> Boolean, (node: ASTNode) -> Boolean>(
Pair(
// Ignore provideDelegate if there is a `by` anywhere in the file
{ importPath -> importPath.endsWith("provideDelegate") },
{ importPath -> importPath.endsWith(".provideDelegate") },
{ rootNode ->
var hasByKeyword = false
rootNode.visit { child ->
Expand Down Expand Up @@ -103,7 +103,7 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
val directCalls = ref.filter { !it.inDotQualifiedExpression }.map { it.text }
parentExpressions.forEach { parent ->
imports.removeIf { imp ->
imp.endsWith(parent) && directCalls.none { imp.endsWith(it) }
imp.endsWith(".$parent") && directCalls.none { imp.endsWith(".$it") }
}
}
} else if (node.elementType == PACKAGE_DIRECTIVE) {
Expand Down Expand Up @@ -160,12 +160,10 @@ class NoUnusedImportsRule : Rule("no-unused-imports") {
return false
}

val staticImports = imports.filter { it.endsWith(text) }
val staticImports = imports.filter { it.endsWith(".$text") }
staticImports.forEach { import ->
var count = 0
imports.forEach {
val startsWith = it.startsWith(import.substringBefore(text))
if (startsWith) count += 1
val count = imports.count {
it.startsWith(import.substringBefore(text))
}
// Parent import and static import both are present
if (count > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,21 @@ class NoUnusedImportsRuleTest {
)
).isEmpty()
}

@Test
fun `only redundant static java function imports should be removed`() {
assertThat(
NoUnusedImportsRule().lint(
"""
import com.google.cloud.bigtable.data.v2.models.Mutation
import com.google.cloud.bigtable.data.v2.models.Row
import com.google.cloud.bigtable.data.v2.models.RowMutation.create
fun test(row: Row) {
create("string", "string", Mutation.create())
}
""".trimIndent()
)
).isEmpty()
}
}

0 comments on commit fcdb9db

Please sign in to comment.