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 388f2b920b..e02f214e45 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 @@ -5,7 +5,6 @@ import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getCommonConfiguration import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.EmitType -import org.cqfn.diktat.ruleset.constants.Warnings import org.cqfn.diktat.ruleset.constants.Warnings.FILE_CONTAINS_ONLY_COMMENTS import org.cqfn.diktat.ruleset.constants.Warnings.FILE_INCORRECT_BLOCKS_ORDER import org.cqfn.diktat.ruleset.constants.Warnings.FILE_NO_BLANK_LINE_BETWEEN_BLOCKS @@ -18,6 +17,7 @@ import org.cqfn.diktat.ruleset.utils.copyrightWords import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType import org.cqfn.diktat.ruleset.utils.handleIncorrectOrder import org.cqfn.diktat.ruleset.utils.moveChildBefore +import org.cqfn.diktat.ruleset.utils.operatorMap import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT @@ -73,12 +73,6 @@ 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, @@ -94,10 +88,7 @@ class FileStructureRule(private val configRules: List) : Rule("file val importsGroupingConfig = ImportsGroupingConfig( this.configRules.getRuleConfig(FILE_UNORDERED_IMPORTS)?.configuration ?: emptyMap() ) - val unusedImportConfig = UnusedImportConfig( - this.configRules.getRuleConfig(UNUSED_IMPORT)?.configuration ?: emptyMap() - ) - checkUnusedImport(node, unusedImportConfig) + checkUnusedImport(node) node.findChildByType(IMPORT_LIST) ?.let { checkImportsOrder(it, wildcardImportsConfig, importsGroupingConfig) } if (checkFileHasCode(node)) { @@ -222,8 +213,7 @@ class FileStructureRule(private val configRules: List) : Rule("file @Suppress("UnsafeCallOnNullableType") private fun checkUnusedImport( - node: ASTNode, - unusedImportConfig: UnusedImportConfig + node: ASTNode ) { findAllReferences(node) packageName = (node.findChildByType(PACKAGE_DIRECTIVE)?.psi as KtPackageDirective).qualifiedName @@ -236,35 +226,28 @@ class FileStructureRule(private val configRules: List) : Rule("file packageName.isNotEmpty() && importPath.startsWith("$packageName.") && importPath.substring(packageName.length + 1).indexOf('.') == -1 ) { - if (unusedImportConfig.deleteUnusedImport) { - Warnings.UNUSED_IMPORT.warnAndFix( - configRules, emitWarn, isFixMode, - "$importPath - unused import", - node.startOffset, node - ) { ktImportDirective.delete() } - } - } else if (importName != null && !refSet.contains(getValueMap(importName)) && !refSet.contains( + // this branch corresponds to imports from the same package + deleteImport(importPath, node, ktImportDirective) + } else if (importName != null && !refSet.contains(operatorMap.getOrDefault(importName, null)) && !refSet.contains( importName ) ) { - if (unusedImportConfig.deleteUnusedImport) { - Warnings.UNUSED_IMPORT.warnAndFix( - configRules, emitWarn, isFixMode, - "$importPath - unused import", - node.startOffset, node - ) { ktImportDirective.delete() } - } + // this import is not used anywhere + deleteImport(importPath, node, ktImportDirective) } } } - private fun getValueMap(key: String): String? { - val value: String? = try { - operatorMap.getValue(key) - } catch (e: NoSuchElementException) { - null - } - return value + private fun deleteImport( + importPath: String, + node: ASTNode, + ktImportDirective: KtImportDirective + ) { + UNUSED_IMPORT.warnAndFix( + configRules, emitWarn, isFixMode, + "$importPath - unused import", + node.startOffset, node + ) { ktImportDirective.delete() } } private fun findAllReferences(node: ASTNode) { @@ -390,14 +373,4 @@ class FileStructureRule(private val configRules: List) : Rule("file */ val useRecommendedImportsOrder = config["useRecommendedImportsOrder"]?.toBoolean() ?: true } - - /** - * [RuleConfiguration] for unused import - */ - class UnusedImportConfig(config: Map) : RuleConfiguration(config) { - /** - * delete unused import - */ - val deleteUnusedImport = config["deleteUnusedImport"]?.toBoolean() ?: true - } } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstConstants.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstConstants.kt index 89293a430a..dd52e901d3 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstConstants.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstConstants.kt @@ -42,3 +42,10 @@ enum class StandardPlatforms(val packages: List) { KOTLIN(listOf("kotlin", "kotlinx")), ; } + +internal 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 "%=" +)