From d9cf92c983682c850e3ea5e77df16956e7aad77e Mon Sep 17 00:00:00 2001 From: Stanley Shyiko Date: Wed, 25 Jan 2017 09:43:19 -0800 Subject: [PATCH] Fixed unused infix function call import false positive --- .../shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt | 6 ++++-- .../ktlint/ruleset/standard/NoUnusedImportsRuleTest.kt | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/gihub/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/gihub/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt index dcc5145428..d37df05312 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/gihub/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/gihub/shyiko/ktlint/ruleset/standard/NoUnusedImportsRule.kt @@ -1,6 +1,7 @@ package com.gihub.shyiko.ktlint.ruleset.standard import com.github.shyiko.ktlint.core.Rule +import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.psi.KtImportDirective import org.jetbrains.kotlin.psi.psiUtil.startOffset @@ -17,8 +18,9 @@ class NoUnusedImportsRule : Rule("no-unused-imports") { emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { if (node.elementType == KtStubElementTypes.FILE) { node.visit { node -> - if (node.elementType == KtStubElementTypes.REFERENCE_EXPRESSION && - !node.psi.isPartOf(KtImportDirective::class)) { + val type = node.elementType + if ((type == KtNodeTypes.REFERENCE_EXPRESSION || type == KtNodeTypes.OPERATION_REFERENCE) && + !node.psi.isPartOf(KtImportDirective::class)) { ref.add(node.text.trim('`')) } } 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 65fabdce12..432d53778d 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 @@ -22,10 +22,12 @@ class NoUnusedImportsRuleTest { import p.a.* import escaped.`when` import escaped.`foo` + import infixfunc fun main() { println(a()) C.call(B()) + 1 infixfunc 2 `when`() } """.trimIndent()