From b014c5d73b1486bf65fdff8591a802c46c33eb06 Mon Sep 17 00:00:00 2001 From: aktsay Date: Fri, 11 Dec 2020 11:35:16 +0300 Subject: [PATCH] bugfix/local-variable-loops(#581) ### What's done: * Fixed bugs --- .../rules/ExtensionFunctionsSameNameRule.kt | 5 +---- .../cqfn/diktat/ruleset/rules/LineLength.kt | 6 +++--- .../diktat/ruleset/rules/SmartCastRule.kt | 2 +- .../org/cqfn/diktat/ruleset/rules/SortRule.kt | 2 +- .../rules/identifiers/LocalVariablesRule.kt | 21 +++++++++++-------- .../cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 3 +-- .../chapter3/LocalVariablesWarnTest.kt | 8 +------ 7 files changed, 20 insertions(+), 27 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/ExtensionFunctionsSameNameRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/ExtensionFunctionsSameNameRule.kt index 2b852b4784..3d6d6432be 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/ExtensionFunctionsSameNameRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/ExtensionFunctionsSameNameRule.kt @@ -76,10 +76,7 @@ class ExtensionFunctionsSameNameRule(private val configRules: List) return pairs } - /** - * FixMe: warning suppressed until https://github.com/cqfn/diKTat/issues/581 - */ - @Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION", "TYPE_ALIAS") + @Suppress("UnsafeCallOnNullableType", "TYPE_ALIAS") private fun collectAllExtensionFunctions(node: ASTNode): SimilarSignatures { val extensionFunctionList = node.findAllNodesWithSpecificType(FUN).filter { it.hasChildOfType(TYPE_REFERENCE) && it.hasChildOfType(DOT) } val distinctFunctionSignatures: MutableMap = mutableMapOf() // maps function signatures on node it is used by diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/LineLength.kt index d3deb5d092..a12dc91858 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/LineLength.kt @@ -81,7 +81,7 @@ class LineLength(private val configRules: List) : Rule("line-length } } - @Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION") + @Suppress("UnsafeCallOnNullableType") private fun checkLength(node: ASTNode, configuration: LineLengthConfiguration) { var offset = 0 node.text.lines().forEach { @@ -239,7 +239,7 @@ class LineLength(private val configRules: List) : Rule("line-length } } - @Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION") + @Suppress("UnsafeCallOnNullableType") private fun findAllText(astNode: ASTNode): String { var text = "" var node = astNode @@ -277,7 +277,7 @@ class LineLength(private val configRules: List) : Rule("line-length return text } - @Suppress("UnsafeCallOnNullableType", "LOCAL_VARIABLE_EARLY_DECLARATION") + @Suppress("UnsafeCallOnNullableType") private fun getBraceAndBeforeText(node: ASTNode, prevNode: ASTNode): String { var text = "" val par = prevNode.prevSibling { it.elementType == OPERATION_REFERENCE }?.let { LPAR } ?: RPAR diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SmartCastRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SmartCastRule.kt index ec94aa9bb7..ad4a3fd533 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SmartCastRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SmartCastRule.kt @@ -65,7 +65,7 @@ class SmartCastRule(private val configRules: List) : Rule("smart-ca } // Divide in is and as expr - @Suppress("TYPE_ALIAS", "LOCAL_VARIABLE_EARLY_DECLARATION") + @Suppress("TYPE_ALIAS") private fun handleProp(propMap: Map>) { propMap.forEach { (property, references) -> val isExpr: MutableList = mutableListOf() diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt index 9837d2b705..5033b1e60d 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt @@ -90,7 +90,7 @@ class SortRule(private val configRules: List) : Rule("sort-rule") { } } - @Suppress("LOCAL_VARIABLE_EARLY_DECLARATION", "TYPE_ALIAS") + @Suppress("TYPE_ALIAS") private fun createOrderListOfList(propertyList: List): List> { val orderListOfList: MutableList> = mutableListOf() var oneOrderList = mutableListOf(propertyList.first()) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/identifiers/LocalVariablesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/identifiers/LocalVariablesRule.kt index e89d668398..d76bb760ee 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/identifiers/LocalVariablesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/identifiers/LocalVariablesRule.kt @@ -96,26 +96,29 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc } private fun handleConsecutiveDeclarations(statement: PsiElement, properties: List) { - val numLinesAfterLastProp = if (properties.last().node.treeNext.elementType == WHITE_SPACE) { - // minus one is needed to except \n after property - properties + val numLinesAfterLastProp = + properties .last() .node .treeNext - .numNewLines() - 1 - } else { - 0 - } + .takeIf { it.elementType == WHITE_SPACE } + ?.let { + // minus one is needed to except \n after property + it.numNewLines() - 1 + } + ?: 0 // need to check that properties are declared consecutively with only maybe empty lines properties .sortedBy { it.node.getLineNumber() } - .let { it as MutableList } - .zip(((properties.size - 1) downTo 0).map { it + numLinesAfterLastProp }) + .zip( + (properties.size - 1 downTo 0).map { it + numLinesAfterLastProp } + ) .forEachIndexed { index, (property, offset) -> if (index != properties.lastIndex) { checkLineNumbers(property, statement.node.getLineNumber(), offset) } else { + // since offset after last property is calculated in this method, we pass offset = 0 checkLineNumbers(property, statement.node.getLineNumber(), 0) } } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt index 999632ddd4..bd75c5a075 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt @@ -636,7 +636,7 @@ fun List.handleIncorrectOrder( * This method returns text of this [ASTNode] plus text from it's siblings after last and until next newline, if present in siblings. * I.e., if this node occupies no more than a single line, this whole line or it's part will be returned. */ -@Suppress("LOCAL_VARIABLE_EARLY_DECLARATION", "WRONG_NEWLINES") +@Suppress("WRONG_NEWLINES") fun ASTNode.extractLineOfText(): String { var text: MutableList = mutableListOf() siblings(false) @@ -743,7 +743,6 @@ fun ASTNode.getLineNumber(): Int = * This function calculates line number instead of using cached values. * It should be used when AST could be previously mutated by auto fixers. */ -@Suppress("LOCAL_VARIABLE_EARLY_DECLARATION") private fun ASTNode.calculateLineNumber(): Int { var count = 0 // todo use runningFold or something similar when we migrate to apiVersion 1.4 diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LocalVariablesWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LocalVariablesWarnTest.kt index 15c900d924..977313324d 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LocalVariablesWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LocalVariablesWarnTest.kt @@ -471,7 +471,7 @@ class LocalVariablesWarnTest : LintTestBase(::LocalVariablesRule) { @Test @Tag(WarningNames.LOCAL_VARIABLE_EARLY_DECLARATION) - fun `should emit only one warning when same variables are used more than once`() { + fun `should not trigger on properties`() { lintMethod( """ |private fun checkDoc(node: ASTNode, warning: Warnings) { @@ -583,12 +583,6 @@ class LocalVariablesWarnTest : LintTestBase(::LocalVariablesRule) { | val extensionFunctionsPairs = mutableListOf>() // pairs extension functions with same signature | | extensionFunctionList.forEach { func -> - | val functionName = (func.psi as KtNamedFunction).name!! - | // List is used to show param names in warning - | val params = (func.getFirstChildWithType(VALUE_PARAMETER_LIST)!!.psi as KtParameterList).parameters.map { it.name!! } - | val returnType = func.findChildAfter(COLON, TYPE_REFERENCE)?.text - | val className = func.findChildBefore(DOT, TYPE_REFERENCE)!!.text - | val signature = FunctionSignature(functionName, params, returnType) | if (distinctFunctionSignatures.contains(signature)) { | val secondFuncClassName = distinctFunctionSignatures[signature]!!.findChildBefore(DOT, TYPE_REFERENCE)!!.text | extensionFunctionsPairs.add(Pair(