diff --git a/diktat-maven-plugin/src/main/kotlin/org/cqfn/diktat/plugin/maven/DiktatMojo.kt b/diktat-maven-plugin/src/main/kotlin/org/cqfn/diktat/plugin/maven/DiktatMojo.kt index 114c0f619d..d84e368541 100644 --- a/diktat-maven-plugin/src/main/kotlin/org/cqfn/diktat/plugin/maven/DiktatMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/org/cqfn/diktat/plugin/maven/DiktatMojo.kt @@ -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 diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt index 01f4d93b9e..388f2b920b 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt @@ -73,6 +73,12 @@ class FileStructureRule(private val configRules: List) : Rule("file } private val refSet: MutableSet = 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, @@ -237,7 +243,10 @@ class FileStructureRule(private val configRules: List) : 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, @@ -249,6 +258,15 @@ class FileStructureRule(private val configRules: List) : 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)) { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt index dcc195cf3a..5b6cba2c9d 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt @@ -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(), + ) + } }