Skip to content

Commit

Permalink
New warning: Unused imports should be removed
Browse files Browse the repository at this point in the history
### What's done:
* Added new warning
* Added new test
  • Loading branch information
Cheshiriks committed Jan 21, 2021
1 parent 3026e13 commit 7ddf1b0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package org.cqfn.diktat.plugin.maven

import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider

import com.pinterest.ktlint.core.KtLint
import org.apache.maven.plugins.annotations.Mojo

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
}
private val refSet: MutableSet<String> = mutableSetOf()
private var packageName = ""
private val operatorMap = mapOf(
"unaryPlus" to "+", "unaryMinus" to "-", "not" to "!",
"plus" to "+", "minus" to "-", "times" to "*", "div" to "/", "rem" to "%", "mod" to "%", "rangeTo" to "..",
"inc" to "++", "dec" to "--", "contains" to "in",
"plusAssign" to "+=", "minusAssign" to "-=", "timesAssign" to "*=", "divAssign" to "/=", "modAssign" to "%="
)
private lateinit var emitWarn: EmitType

override fun visit(node: ASTNode,
Expand Down Expand Up @@ -237,7 +243,10 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
node.startOffset, node
) { ktImportDirective.delete() }
}
} else if (importName != null && !refSet.contains(importName)) {
} else if (importName != null && !refSet.contains(getValueMap(importName)) && !refSet.contains(
importName
)
) {
if (unusedImportConfig.deleteUnusedImport) {
Warnings.UNUSED_IMPORT.warnAndFix(
configRules, emitWarn, isFixMode,
Expand All @@ -249,6 +258,15 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
}
}

private fun getValueMap(key: String): String? {
val value: String? = try {
operatorMap.getValue(key)
} catch (e: NoSuchElementException) {
null
}
return value
}

private fun findAllReferences(node: ASTNode) {
node.findAllNodesWithSpecificType(REFERENCE_EXPRESSION)?.forEach {
if (!it.isPartOf(IMPORT_DIRECTIVE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,21 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) {
""".trimMargin(),
)
}

@Test
@Tag(WarningNames.UNUSED_IMPORT)
fun `Operator overloading`() {
lintMethod(
"""
|package org.cqfn.diktat.example
|
|import kotlin.io.path.div
|
|class Example {
|val pom = kotlin.io.path.createTempFile().toFile()
|val x = listOf(pom.parentFile.toPath() / "src/main/kotlin/exclusion")
|}
""".trimMargin(),
)
}
}

0 comments on commit 7ddf1b0

Please sign in to comment.