Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unused import false positive in kdoc - closes#201 #203

Merged
merged 1 commit into from Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -64,11 +64,18 @@ class UnusedImports(config: Config) : Rule(config) {
dcl.docComment?.getDefaultSection()?.getContent()?.let {
kotlinDocReferencesRegExp.findAll(it, 0)
.map { it.groupValues[1] }
.forEach { imports.removeIf { pair -> pair.second.identifier() == it } }
.forEach { checkImports(it) }
}
super.visitDeclaration(dcl)
}

private fun checkImports(it: String) {
imports.removeIf { pair ->
val identifier = pair.second.identifier()
identifier != null && it.startsWith(identifier)
}
}

private fun KtImportDirective.identifier() = this.importPath?.importedName?.identifier

}
Expand Up @@ -10,10 +10,12 @@ import org.junit.jupiter.api.Test

/**
* @author Shyiko
* @author Artur Bosch
* @author Mauin
*/
class UnusedImportsTest : RuleTest {

override val rule: Rule = UnusedImports(Config.Companion.empty)
override val rule: Rule = UnusedImports(Config.empty)

@Test
fun infixOperators() {
Expand Down Expand Up @@ -73,6 +75,27 @@ class UnusedImportsTest : RuleTest {
)).hasSize(1)
}

@Test
fun considerKdocReferencesWithMethodCalls() {
val code = """
package com.example

import android.text.TextWatcher

class Test {
/**
* [TextWatcher.beforeTextChanged]
*/
fun test() {
TODO()
}
}
"""

assertThat(rule.lint(code)).hasSize(0)
}


@Test
fun testLint() {
assertThat(rule.lint(
Expand Down