From 75dae5baf0530e8d04277664f1bd6d6dd53822bb Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 12 Oct 2020 18:55:57 +0300 Subject: [PATCH 1/9] Update to kotlin 1.4 ### What's done: * Try to rollback to stdlib from stdlib-jdk8 --- diktat-common/pom.xml | 2 +- diktat-maven-plugin/pom.xml | 2 +- diktat-rules/pom.xml | 2 +- diktat-ruleset/pom.xml | 4 ---- diktat-test-framework/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 5 insertions(+), 9 deletions(-) diff --git a/diktat-common/pom.xml b/diktat-common/pom.xml index ed937403f2..34f89ab148 100644 --- a/diktat-common/pom.xml +++ b/diktat-common/pom.xml @@ -15,7 +15,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jdk8 + kotlin-stdlib com.fasterxml.jackson.core diff --git a/diktat-maven-plugin/pom.xml b/diktat-maven-plugin/pom.xml index 6dbb74c741..6058426c98 100644 --- a/diktat-maven-plugin/pom.xml +++ b/diktat-maven-plugin/pom.xml @@ -35,7 +35,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jdk8 + kotlin-stdlib org.cqfn.diktat diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index 0a902034dc..76317acc73 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -40,7 +40,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jdk8 + kotlin-stdlib diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index 3c5defabbd..91944d495e 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -22,10 +22,6 @@ org.jetbrains.kotlin kotlin-stdlib-common - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - org.jetbrains.kotlin kotlin-stdlib diff --git a/diktat-test-framework/pom.xml b/diktat-test-framework/pom.xml index fe477467d7..bbdf32a72f 100644 --- a/diktat-test-framework/pom.xml +++ b/diktat-test-framework/pom.xml @@ -20,7 +20,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jdk8 + kotlin-stdlib com.fasterxml.jackson.core diff --git a/pom.xml b/pom.xml index 8c5a003e3b..7486323a08 100644 --- a/pom.xml +++ b/pom.xml @@ -81,7 +81,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jdk8 + kotlin-stdlib ${kotlin.version} From 51bf5bf4ae5fbe8db4a4d180313150b635aa5575 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 12 Oct 2020 19:01:20 +0300 Subject: [PATCH 2/9] Revert "Remove stdlib-jdk8" ### What's done: This reverts commit 75dae5ba --- diktat-common/pom.xml | 2 +- diktat-maven-plugin/pom.xml | 2 +- diktat-rules/pom.xml | 2 +- diktat-ruleset/pom.xml | 4 ++++ diktat-test-framework/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/diktat-common/pom.xml b/diktat-common/pom.xml index 34f89ab148..ed937403f2 100644 --- a/diktat-common/pom.xml +++ b/diktat-common/pom.xml @@ -15,7 +15,7 @@ org.jetbrains.kotlin - kotlin-stdlib + kotlin-stdlib-jdk8 com.fasterxml.jackson.core diff --git a/diktat-maven-plugin/pom.xml b/diktat-maven-plugin/pom.xml index 6058426c98..6dbb74c741 100644 --- a/diktat-maven-plugin/pom.xml +++ b/diktat-maven-plugin/pom.xml @@ -35,7 +35,7 @@ org.jetbrains.kotlin - kotlin-stdlib + kotlin-stdlib-jdk8 org.cqfn.diktat diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index 76317acc73..0a902034dc 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -40,7 +40,7 @@ org.jetbrains.kotlin - kotlin-stdlib + kotlin-stdlib-jdk8 diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index 91944d495e..3c5defabbd 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -22,6 +22,10 @@ org.jetbrains.kotlin kotlin-stdlib-common + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + org.jetbrains.kotlin kotlin-stdlib diff --git a/diktat-test-framework/pom.xml b/diktat-test-framework/pom.xml index bbdf32a72f..fe477467d7 100644 --- a/diktat-test-framework/pom.xml +++ b/diktat-test-framework/pom.xml @@ -20,7 +20,7 @@ org.jetbrains.kotlin - kotlin-stdlib + kotlin-stdlib-jdk8 com.fasterxml.jackson.core diff --git a/pom.xml b/pom.xml index 7486323a08..8c5a003e3b 100644 --- a/pom.xml +++ b/pom.xml @@ -81,7 +81,7 @@ org.jetbrains.kotlin - kotlin-stdlib + kotlin-stdlib-jdk8 ${kotlin.version} From d7355347b11cbb095b935259c3e2d21424d43900 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 12 Oct 2020 19:03:01 +0300 Subject: [PATCH 3/9] Update to kotlin 1.4 ### What's done: * Use old methods for min --- .../diktat/ruleset/rules/identifiers/LocalVariablesRule.kt | 6 +++--- .../kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) 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 72169d118f..6a105e2058 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 @@ -95,7 +95,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc val declarationScope = property.getDeclarationScope() val firstUsageStatementLine = getFirstUsageStatementOrBlock(usages, declarationScope).node.lineNumber()!! - val firstUsage = usages.minByOrNull { it.node.lineNumber()!! }!! + val firstUsage = usages.minBy { it.node.lineNumber()!! }!! checkLineNumbers(property, firstUsageStatementLine, firstUsageLine = firstUsage.node.lineNumber()!!) } @@ -130,7 +130,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc */ @Suppress("UnsafeCallOnNullableType") private fun getFirstUsageStatementOrBlock(usages: List, declarationScope: KtBlockExpression?): PsiElement { - val firstUsage = usages.minByOrNull { it.node.lineNumber()!! }!! + val firstUsage = usages.minBy { it.node.lineNumber()!! }!! val firstUsageScope = firstUsage.getParentOfType(true) return if (firstUsageScope == declarationScope) { @@ -140,7 +140,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc .find { it.parent == declarationScope }!! } else { // first usage is in deeper block compared to declaration, need to check how close is declaration to the first line of the block - usages.minByOrNull { it.node.lineNumber()!! }!! + usages.minBy { it.node.lineNumber()!! }!! .parentsWithSelf .find { it.parent == declarationScope }!! } 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 84d2cf9a22..052afd7496 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 @@ -437,7 +437,7 @@ fun ASTNode.areChildrenBeforeChild(children: List, beforeChild: ASTNode @Suppress("UnsafeCallOnNullableType") fun ASTNode.areChildrenBeforeGroup(children: List, group: List): Boolean { require(children.isNotEmpty() && group.isNotEmpty()) { "no sense to operate on empty lists" } - return children.map { getChildren(null).indexOf(it) }.maxOrNull()!! < group.map { getChildren(null).indexOf(it) }.minOrNull()!! + return children.map { getChildren(null).indexOf(it) }.maxOrNull()!! < group.map { getChildren(null).indexOf(it) }.min()!! } fun List.handleIncorrectOrder(getSiblingBlocks: ASTNode.() -> Pair, From 7726e05a2b2df074304ea917902c291a544afef8 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Tue, 13 Oct 2020 10:30:57 +0300 Subject: [PATCH 4/9] Update to kotlin 1.4 ### What's done: * Compile with api version 1.3 --- diktat-common/pom.xml | 7 ------- diktat-maven-plugin/pom.xml | 8 -------- diktat-rules/pom.xml | 9 --------- .../kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 4 ++-- diktat-test-framework/pom.xml | 1 - pom.xml | 9 +++++++++ 6 files changed, 11 insertions(+), 27 deletions(-) diff --git a/diktat-common/pom.xml b/diktat-common/pom.xml index ed937403f2..ab4af2d20e 100644 --- a/diktat-common/pom.xml +++ b/diktat-common/pom.xml @@ -73,13 +73,6 @@ compile - - - src/main/kotlin - src/main/resources - - 1.8 - test-compile diff --git a/diktat-maven-plugin/pom.xml b/diktat-maven-plugin/pom.xml index 6dbb74c741..65344dadb1 100644 --- a/diktat-maven-plugin/pom.xml +++ b/diktat-maven-plugin/pom.xml @@ -63,13 +63,6 @@ compile - - - src/main/kotlin - src/main/resources - - 1.8 - test-compile @@ -81,7 +74,6 @@ src/test/kotlin - 1.8 diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index 0a902034dc..7c472d50bd 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -88,7 +88,6 @@ ${project.basedir}/src/main/kotlin/generated - 1.8 @@ -97,13 +96,6 @@ compile - - - src/main/kotlin - src/main/resources - - 1.8 - test-compile @@ -115,7 +107,6 @@ src/test/kotlin - 1.8 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 052afd7496..d267aa1dc1 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 @@ -426,7 +426,7 @@ fun ASTNode.isChildAfterAnother(child: ASTNode, afterChild: ASTNode): Boolean = getChildren(null).indexOf(child) > getChildren(null).indexOf(afterChild) fun ASTNode.isChildAfterGroup(child: ASTNode, group: List): Boolean = - getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.maxOrNull() ?: 0) + getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.max() ?: 0) fun ASTNode.isChildBeforeAnother(child: ASTNode, beforeChild: ASTNode): Boolean = areChildrenBeforeGroup(listOf(child), listOf(beforeChild)) @@ -437,7 +437,7 @@ fun ASTNode.areChildrenBeforeChild(children: List, beforeChild: ASTNode @Suppress("UnsafeCallOnNullableType") fun ASTNode.areChildrenBeforeGroup(children: List, group: List): Boolean { require(children.isNotEmpty() && group.isNotEmpty()) { "no sense to operate on empty lists" } - return children.map { getChildren(null).indexOf(it) }.maxOrNull()!! < group.map { getChildren(null).indexOf(it) }.min()!! + return children.map { getChildren(null).indexOf(it) }.max()!! < group.map { getChildren(null).indexOf(it) }.min()!! } fun List.handleIncorrectOrder(getSiblingBlocks: ASTNode.() -> Pair, diff --git a/diktat-test-framework/pom.xml b/diktat-test-framework/pom.xml index fe477467d7..a046f85e05 100644 --- a/diktat-test-framework/pom.xml +++ b/diktat-test-framework/pom.xml @@ -88,7 +88,6 @@ src/main/kotlin src/main/resources - 1.8 diff --git a/pom.xml b/pom.xml index 8c5a003e3b..fe4487d27d 100644 --- a/pom.xml +++ b/pom.xml @@ -237,6 +237,15 @@ org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} + + 1.8 + 1.3 + + src/main/kotlin + src/main/resources + + + true org.codehaus.mojo From dc7feb845b6d322db7e627b709a32d54c298a989 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Tue, 13 Oct 2020 10:38:00 +0300 Subject: [PATCH 5/9] Update to kotlin 1.4 ### What's done: * Use current kotlin api version --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index fe4487d27d..86d093dd19 100644 --- a/pom.xml +++ b/pom.xml @@ -239,7 +239,6 @@ ${kotlin.version} 1.8 - 1.3 src/main/kotlin src/main/resources From e4b4521359548c4b7c605cc5ed4d36a8f457ceb3 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Tue, 13 Oct 2020 11:07:13 +0300 Subject: [PATCH 6/9] Update to kotlin 1.4 ### What's done: * Use new methods instead of deprecated --- .../diktat/ruleset/rules/identifiers/LocalVariablesRule.kt | 6 +++--- .../kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) 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 6a105e2058..72169d118f 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 @@ -95,7 +95,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc val declarationScope = property.getDeclarationScope() val firstUsageStatementLine = getFirstUsageStatementOrBlock(usages, declarationScope).node.lineNumber()!! - val firstUsage = usages.minBy { it.node.lineNumber()!! }!! + val firstUsage = usages.minByOrNull { it.node.lineNumber()!! }!! checkLineNumbers(property, firstUsageStatementLine, firstUsageLine = firstUsage.node.lineNumber()!!) } @@ -130,7 +130,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc */ @Suppress("UnsafeCallOnNullableType") private fun getFirstUsageStatementOrBlock(usages: List, declarationScope: KtBlockExpression?): PsiElement { - val firstUsage = usages.minBy { it.node.lineNumber()!! }!! + val firstUsage = usages.minByOrNull { it.node.lineNumber()!! }!! val firstUsageScope = firstUsage.getParentOfType(true) return if (firstUsageScope == declarationScope) { @@ -140,7 +140,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc .find { it.parent == declarationScope }!! } else { // first usage is in deeper block compared to declaration, need to check how close is declaration to the first line of the block - usages.minBy { it.node.lineNumber()!! }!! + usages.minByOrNull { it.node.lineNumber()!! }!! .parentsWithSelf .find { it.parent == declarationScope }!! } 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 d267aa1dc1..84d2cf9a22 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 @@ -426,7 +426,7 @@ fun ASTNode.isChildAfterAnother(child: ASTNode, afterChild: ASTNode): Boolean = getChildren(null).indexOf(child) > getChildren(null).indexOf(afterChild) fun ASTNode.isChildAfterGroup(child: ASTNode, group: List): Boolean = - getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.max() ?: 0) + getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.maxOrNull() ?: 0) fun ASTNode.isChildBeforeAnother(child: ASTNode, beforeChild: ASTNode): Boolean = areChildrenBeforeGroup(listOf(child), listOf(beforeChild)) @@ -437,7 +437,7 @@ fun ASTNode.areChildrenBeforeChild(children: List, beforeChild: ASTNode @Suppress("UnsafeCallOnNullableType") fun ASTNode.areChildrenBeforeGroup(children: List, group: List): Boolean { require(children.isNotEmpty() && group.isNotEmpty()) { "no sense to operate on empty lists" } - return children.map { getChildren(null).indexOf(it) }.max()!! < group.map { getChildren(null).indexOf(it) }.min()!! + return children.map { getChildren(null).indexOf(it) }.maxOrNull()!! < group.map { getChildren(null).indexOf(it) }.minOrNull()!! } fun List.handleIncorrectOrder(getSiblingBlocks: ASTNode.() -> Pair, From 5eca888acae235b92adfc00d173ea12bad76aeb8 Mon Sep 17 00:00:00 2001 From: Denis Kumar Date: Tue, 13 Oct 2020 11:50:24 +0300 Subject: [PATCH 7/9] Rule 5.2.3 Use default values for function arguments instead of overloading them (#370) * Rule 5.2.3 ### What's done: Implemented rule Added tests and information into documentations --- diktat-analysis.yml | 6 +- .../src/main/kotlin/generated/WarningNames.kt | 2 + .../cqfn/diktat/ruleset/constants/Warnings.kt | 1 + .../ruleset/rules/DiktatRuleSetProvider.kt | 1 + .../rules/OverloadingArgumentsFunction.kt | 46 +++++++ .../main/resources/diktat-analysis-huawei.yml | 6 +- .../src/main/resources/diktat-analysis.yml | 6 +- .../OverloadingArgumentsFunctionWarnTest.kt | 123 ++++++++++++++++++ info/available-rules.md | 3 +- 9 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/OverloadingArgumentsFunction.kt create mode 100644 diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/OverloadingArgumentsFunctionWarnTest.kt diff --git a/diktat-analysis.yml b/diktat-analysis.yml index 3cb3b0fb0c..d6bc4fe00a 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -309,4 +309,8 @@ - name: NESTED_BLOCK enabled: true configuration: - maxNestedBlockQuantity: '4' \ No newline at end of file + maxNestedBlockQuantity: '4' +# Checks that function use default values, instead overloading +- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS + enabled: true + configuration: {} \ No newline at end of file diff --git a/diktat-rules/src/main/kotlin/generated/WarningNames.kt b/diktat-rules/src/main/kotlin/generated/WarningNames.kt index fb6dd01d59..4a69fb0823 100644 --- a/diktat-rules/src/main/kotlin/generated/WarningNames.kt +++ b/diktat-rules/src/main/kotlin/generated/WarningNames.kt @@ -179,4 +179,6 @@ object WarningNames { const val TOO_MANY_PARAMETERS: String = "TOO_MANY_PARAMETERS" const val NESTED_BLOCK: String = "NESTED_BLOCK" + + const val WRONG_OVERLOADING_FUNCTION_ARGUMENTS: String = "WRONG_OVERLOADING_FUNCTION_ARGUMENTS" } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt index 3c7e430146..532c80f3ef 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt @@ -113,6 +113,7 @@ enum class Warnings(private val canBeAutoCorrected: Boolean, private val warn: S LAMBDA_IS_NOT_LAST_PARAMETER(false, "lambda inside function parameters should be in the end"), TOO_MANY_PARAMETERS(false, "function has too many parameters"), NESTED_BLOCK(false, "function has too many nested blocks and should be simplified"), + WRONG_OVERLOADING_FUNCTION_ARGUMENTS(false, "use default argument instead of function overloading"), ; /** diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt index deb1ec8979..cabeb6afde 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt @@ -57,6 +57,7 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = "diktat-analy ::AccurateCalculationsRule, ::LineLength, ::TypeAliasRule, + ::OverloadingArgumentsFunction, ::FunctionLength, ::LambdaParameterOrder, ::FunctionArgumentsSize, diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/OverloadingArgumentsFunction.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/OverloadingArgumentsFunction.kt new file mode 100644 index 0000000000..f36dd97096 --- /dev/null +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/OverloadingArgumentsFunction.kt @@ -0,0 +1,46 @@ +package org.cqfn.diktat.ruleset.rules + +import com.pinterest.ktlint.core.Rule +import com.pinterest.ktlint.core.ast.ElementType.FUN +import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER +import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE +import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_OVERLOADING_FUNCTION_ARGUMENTS +import org.cqfn.diktat.ruleset.utils.allSiblings +import org.cqfn.diktat.ruleset.utils.findChildAfter +import org.cqfn.diktat.ruleset.utils.findChildBefore +import org.jetbrains.kotlin.com.intellij.lang.ASTNode +import org.jetbrains.kotlin.psi.KtFunction +import org.jetbrains.kotlin.psi.psiUtil.startOffset + +class OverloadingArgumentsFunction(private val configRules: List) : Rule("overloading-default-values") { + + private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) + private var isFixMode: Boolean = false + + override fun visit(node: ASTNode, + autoCorrect: Boolean, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { + isFixMode = autoCorrect + emitWarn = emit + + if (node.elementType == FUN) { + checkFun(node.psi as KtFunction) + } + } + + @Suppress("UnsafeCallOnNullableType") + private fun checkFun(funPsi: KtFunction) { + val allOverloadFunction = funPsi.node.allSiblings(withSelf = false) + .asSequence() + .filter { it.elementType == FUN } + .map { it.psi as KtFunction } + .filter { it.nameIdentifier!!.text == funPsi.nameIdentifier!!.text && it.valueParameters.containsAll(funPsi.valueParameters) } + .filter { funPsi.node.findChildBefore(IDENTIFIER, TYPE_REFERENCE)?.text == it.node.findChildBefore(IDENTIFIER, TYPE_REFERENCE)?.text } + .filter {funPsi.node.findChildAfter(IDENTIFIER, TYPE_REFERENCE)?.text == it.node.findChildAfter(IDENTIFIER, TYPE_REFERENCE)?.text } + .toList() + if (allOverloadFunction.isNotEmpty()) { + WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warn(configRules, emitWarn, isFixMode, funPsi.node.findChildByType(IDENTIFIER)!!.text, funPsi.startOffset, funPsi.node) + } + } +} diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index c89f39229a..39c4644155 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -307,4 +307,8 @@ - name: NESTED_BLOCK enabled: true configuration: - maxNestedBlockQuantity: '4' \ No newline at end of file + maxNestedBlockQuantity: '4' +# Checks that function use default values, instead overloading +- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS + enabled: true + configuration: {} \ No newline at end of file diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index 0df7c9a149..9cdae7eb74 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -310,4 +310,8 @@ - name: NESTED_BLOCK enabled: true configuration: - maxNestedBlockQuantity: '4' \ No newline at end of file + maxNestedBlockQuantity: '4' +# Checks that function use default values, instead overloading +- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS + enabled: true + configuration: {} \ No newline at end of file diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/OverloadingArgumentsFunctionWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/OverloadingArgumentsFunctionWarnTest.kt new file mode 100644 index 0000000000..836929adff --- /dev/null +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/OverloadingArgumentsFunctionWarnTest.kt @@ -0,0 +1,123 @@ +package org.cqfn.diktat.ruleset.chapter5 + +import com.pinterest.ktlint.core.LintError +import generated.WarningNames +import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID +import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_OVERLOADING_FUNCTION_ARGUMENTS +import org.cqfn.diktat.ruleset.rules.OverloadingArgumentsFunction +import org.cqfn.diktat.util.LintTestBase +import org.junit.jupiter.api.Tag +import org.junit.jupiter.api.Test + +class OverloadingArgumentsFunctionWarnTest : LintTestBase(::OverloadingArgumentsFunction) { + + private val ruleId = "$DIKTAT_RULE_SET_ID:overloading-default-values" + + @Test + @Tag(WarningNames.WRONG_OVERLOADING_FUNCTION_ARGUMENTS) + fun `check simple example`() { + lintMethod( + """ + |fun foo() {} + | + |fun foo(a: Int) {} + | + |fun goo(a: Double) {} + | + |fun goo(a: Float, b: Double) {} + | + |fun goo(b: Float, a: Double, c: Int) {} + | + |@Suppress("WRONG_OVERLOADING_FUNCTION_ARGUMENTS") + |fun goo(a: Float) + | + |fun goo(a: Double? = 0.0) {} + | + |override fun goo() {} + | + |class A { + | fun foo() {} + |} + | + |abstract class B { + | abstract fun foo(a: Int) + | + | fun foo(){} + |} + """.trimMargin(), + LintError(1,1, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} foo", false), + LintError(16,1, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} goo", false), + LintError(25,4, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} foo", false) + ) + } + + @Test + @Tag(WarningNames.WRONG_OVERLOADING_FUNCTION_ARGUMENTS) + fun `check simple`() { + lintMethod( + """ + private fun isComparisonWithAbs(psiElement: PsiElement) = + when (psiElement) { + is KtBinaryExpression -> psiElement.isComparisonWithAbs() + is KtDotQualifiedExpression -> psiElement.isComparisonWithAbs() + else -> false + } + + private fun KtBinaryExpression.isComparisonWithAbs() = + takeIf { it.operationToken in comparisonOperators } + ?.run { left as? KtCallExpression ?: right as? KtCallExpression } + ?.run { calleeExpression as? KtNameReferenceExpression } + ?.getReferencedName() + ?.equals("abs") + ?: false + + private fun KtBinaryExpression.isComparisonWithAbs(a: Int) = + takeIf { it.operationToken in comparisonOperators } + ?.run { left as? KtCallExpression ?: right as? KtCallExpression } + ?.run { calleeExpression as? KtNameReferenceExpression } + ?.getReferencedName() + ?.equals("abs") + ?: false + + private fun KtBinaryExpression.isComparisonWithAbs(a: Int): Boolean { + return takeIf { it.operationToken in comparisonOperators } + ?.run { left as? KtCallExpression ?: right as? KtCallExpression } + ?.run { calleeExpression as? KtNameReferenceExpression } + ?.getReferencedName() + ?.equals("abs") + ?: false + } + """.trimMargin(), + LintError(8,21, ruleId, "${WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warnText()} isComparisonWithAbs", false) + ) + } + + @Test + @Tag(WarningNames.WRONG_OVERLOADING_FUNCTION_ARGUMENTS) + fun `check methods with different return types`() { + lintMethod( + """ + private fun KtBinaryExpression.isComparisonWithAbs(a: Int): Boolean { + return takeIf { it.operationToken in comparisonOperators } + ?.run { left as? KtCallExpression ?: right as? KtCallExpression } + ?.run { calleeExpression as? KtNameReferenceExpression } + ?.getReferencedName() + ?.equals("abs") + ?: false + } + + private fun KtBinaryExpression.isComparisonWithAbs(a: Int): Int { + val q = takeIf { it.operationToken in comparisonOperators } + ?.run { left as? KtCallExpression ?: right as? KtCallExpression } + ?.run { calleeExpression as? KtNameReferenceExpression } + ?.getReferencedName() + ?.equals("abs") + ?: false + + if (q) return 10 + return 11 + } + """.trimMargin() + ) + } +} diff --git a/info/available-rules.md b/info/available-rules.md index 8956b5df38..246381ba22 100644 --- a/info/available-rules.md +++ b/info/available-rules.md @@ -87,4 +87,5 @@ | 5 | 5.1.2 | NESTED_BLOCK | Check if function has more nested blocks than expected | no | maxNestedBlockQuantity | 5 | 5.1.3 | AVOID_NESTED_FUNCTIONS | Check: if there are any nested functions
Fix: declare function in the outer scope | yes | - | + | | 5 | 5.2.1 | LAMBDA_IS_NOT_LAST_PARAMETER | Checks that lambda inside function parameters isn't in the end | no | - | -| 5 | 5.2.2 | TOO_MANY_PARAMETERS | Check: if function contains more parameters than allowed | no | maxParameterListSize | \ No newline at end of file +| 5 | 5.2.2 | TOO_MANY_PARAMETERS | Check: if function contains more parameters than allowed | no | maxParameterListSize | +| 5 | 5.2.3 | WRONG_OVERLOADING_FUNCTION_ARGUMENTS | Check: function has overloading instead use default arguments | no | -| From 65d36d30831fde2b9d9f1565def1545c02456fbf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 13 Oct 2020 12:08:01 +0300 Subject: [PATCH 8/9] Update versions in pom.xml and documentation after 0.1.2 release (#391) ### What's done: * Prepare next development iteration after release 0.1.2 * Updated docs after release * Use diktat-maven-plugin in diktat.yml and functional_tests.yml * Remove antrun from pom.xml Co-authored-by: GitHub Co-authored-by: Peter Trifanov --- .github/workflows/diktat.yml | 5 +- .github/workflows/functional_tests.yml | 5 +- README.md | 11 ++--- build.gradle.kts | 2 +- diktat-common/pom.xml | 2 +- diktat-maven-plugin/pom.xml | 2 +- diktat-rules/pom.xml | 2 +- diktat-ruleset/pom.xml | 2 +- diktat-test-framework/pom.xml | 2 +- pom.xml | 65 +------------------------- 10 files changed, 17 insertions(+), 81 deletions(-) diff --git a/.github/workflows/diktat.yml b/.github/workflows/diktat.yml index 72af43eedb..f59b58daca 100644 --- a/.github/workflows/diktat.yml +++ b/.github/workflows/diktat.yml @@ -23,6 +23,5 @@ jobs: key: ${{ runner.os }}-maven-diktat-${{ hashFiles('**/pom.xml') }} restore-keys: | ${{ runner.os }}-maven-diktat- - - name: Run diktat itself via ktlint - # fixme: change to diktat:check when diktat plugin is released - run: mvn -B antrun:run@diktat + - name: Run diktat via maven plugin + run: mvn -B diktat:check@diktat diff --git a/.github/workflows/functional_tests.yml b/.github/workflows/functional_tests.yml index 6b87f95e0d..146af2f1ff 100644 --- a/.github/workflows/functional_tests.yml +++ b/.github/workflows/functional_tests.yml @@ -7,7 +7,7 @@ on: branches: [ master ] env: - DIKTAT_VERSION: 0.1.0 + DIKTAT_VERSION: 0.1.2 KTLINT_VERSION: 0.37.1 jobs: @@ -43,8 +43,7 @@ jobs: - name: Run diKTat from maven continue-on-error: true - # fixme: change to diktat:check when diktat plugin is released - run: mvn -B antrun:run@functional-test &> out + run: mvn -B diktat:check@functional-test &> out - name: Check maven run: | diff --git a/README.md b/README.md index 7d52828269..5d6cc73516 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,11 @@ The full list of available supported rules and inspections is [here](info/availa # another option is "brew install ktlint" ``` -2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.1.1/diktat.jar) +2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.1.2/diktat.jar) **OR** use curl: ```bash - $ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.1/diktat-0.1.1.jar + $ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.2/diktat-0.1.2.jar ``` 3. Finally, run KTlint (with diKTat injected) to check your `*.kt` files in `dir/your/dir`: @@ -100,7 +100,7 @@ Add this plugin to your pom.xml: org.cqfn.diktat diktat-rules - 0.1.1 + 0.1.2 org.slf4j @@ -125,8 +125,7 @@ To run diktat to check/fix code style - run `$ mvn antrun:run@diktat`. ### Use the new diktat-maven-plugin You can see how it is configured in our project for self-checks: [pom.xml](pom.xml). -This plugin should be available since version 0.1.2. It requires less configuration but may contain bugs. -If you use it and encounter any problems, feel free to open issues on [github](https://github.com/cqfn/diktat/issues). +This plugin is available since version 0.1.2. If you use it and encounter any problems, feel free to open issues on [github](https://github.com/cqfn/diktat/issues). Add this plugin to your pom.xml:
@@ -179,7 +178,7 @@ dependencies { } // diktat ruleset - ktlint("org.cqfn.diktat:diktat-rules:0.1.1") + ktlint("org.cqfn.diktat:diktat-rules:0.1.2") } val outputDir = "${project.buildDir}/reports/diktat/" diff --git a/build.gradle.kts b/build.gradle.kts index 0fd2c6557a..f1c7a419c4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ dependencies { } // diktat ruleset - ktlint("org.cqfn.diktat:diktat-rules:0.1.1") + ktlint("org.cqfn.diktat:diktat-rules:0.1.2") } val outputDir = "${project.buildDir}/reports/diktat/" diff --git a/diktat-common/pom.xml b/diktat-common/pom.xml index db1168e8b5..d0e5385136 100644 --- a/diktat-common/pom.xml +++ b/diktat-common/pom.xml @@ -9,7 +9,7 @@ org.cqfn.diktat diktat-parent - 0.1.2-SNAPSHOT + 0.1.3-SNAPSHOT diff --git a/diktat-maven-plugin/pom.xml b/diktat-maven-plugin/pom.xml index 6058426c98..14dcad8d39 100644 --- a/diktat-maven-plugin/pom.xml +++ b/diktat-maven-plugin/pom.xml @@ -5,7 +5,7 @@ diktat-parent org.cqfn.diktat - 0.1.2-SNAPSHOT + 0.1.3-SNAPSHOT 4.0.0 diff --git a/diktat-rules/pom.xml b/diktat-rules/pom.xml index 8835182240..73887786fd 100644 --- a/diktat-rules/pom.xml +++ b/diktat-rules/pom.xml @@ -9,7 +9,7 @@ org.cqfn.diktat diktat-parent - 0.1.2-SNAPSHOT + 0.1.3-SNAPSHOT diff --git a/diktat-ruleset/pom.xml b/diktat-ruleset/pom.xml index 91944d495e..0eded69708 100644 --- a/diktat-ruleset/pom.xml +++ b/diktat-ruleset/pom.xml @@ -8,7 +8,7 @@ org.cqfn.diktat diktat-parent - 0.1.2-SNAPSHOT + 0.1.3-SNAPSHOT diff --git a/diktat-test-framework/pom.xml b/diktat-test-framework/pom.xml index bbdf32a72f..d536095269 100644 --- a/diktat-test-framework/pom.xml +++ b/diktat-test-framework/pom.xml @@ -9,7 +9,7 @@ org.cqfn.diktat diktat-parent - 0.1.2-SNAPSHOT + 0.1.3-SNAPSHOT diff --git a/pom.xml b/pom.xml index f6a529ad14..3ef764246a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.cqfn.diktat diktat-parent - 0.1.2-SNAPSHOT + 0.1.3-SNAPSHOT pom diktat @@ -49,7 +49,7 @@ 2.11.3 1.7.30 1.4 - 0.1.1 + 0.1.2 1.6.0 1.14.0 1.4.10 @@ -350,67 +350,6 @@ - - - org.apache.maven.plugins - maven-antrun-plugin - 3.0.0 - - - diktat - none - - - - - - - - - - run - - - - functional-test - none - false - - - - - - - - - run - - - - - - com.pinterest - ktlint - ${ktlint.version} - - - com.pinterest.ktlint - ktlint-ruleset-standard - - - - - org.cqfn.diktat - diktat-rules - ${diktat-check.version} - - - - org.jacoco jacoco-maven-plugin From 91d7958c98d98169924f254f917c4a7d8bf31f7f Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Tue, 13 Oct 2020 12:15:07 +0300 Subject: [PATCH 9/9] Update to kotlin 1.4 ### What's done: * Use apiVersion 1.3 --- .../diktat/ruleset/rules/identifiers/LocalVariablesRule.kt | 6 +++--- .../kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 4 ++-- pom.xml | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) 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 72169d118f..6a105e2058 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 @@ -95,7 +95,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc val declarationScope = property.getDeclarationScope() val firstUsageStatementLine = getFirstUsageStatementOrBlock(usages, declarationScope).node.lineNumber()!! - val firstUsage = usages.minByOrNull { it.node.lineNumber()!! }!! + val firstUsage = usages.minBy { it.node.lineNumber()!! }!! checkLineNumbers(property, firstUsageStatementLine, firstUsageLine = firstUsage.node.lineNumber()!!) } @@ -130,7 +130,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc */ @Suppress("UnsafeCallOnNullableType") private fun getFirstUsageStatementOrBlock(usages: List, declarationScope: KtBlockExpression?): PsiElement { - val firstUsage = usages.minByOrNull { it.node.lineNumber()!! }!! + val firstUsage = usages.minBy { it.node.lineNumber()!! }!! val firstUsageScope = firstUsage.getParentOfType(true) return if (firstUsageScope == declarationScope) { @@ -140,7 +140,7 @@ class LocalVariablesRule(private val configRules: List) : Rule("loc .find { it.parent == declarationScope }!! } else { // first usage is in deeper block compared to declaration, need to check how close is declaration to the first line of the block - usages.minByOrNull { it.node.lineNumber()!! }!! + usages.minBy { it.node.lineNumber()!! }!! .parentsWithSelf .find { it.parent == declarationScope }!! } 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 84d2cf9a22..d267aa1dc1 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 @@ -426,7 +426,7 @@ fun ASTNode.isChildAfterAnother(child: ASTNode, afterChild: ASTNode): Boolean = getChildren(null).indexOf(child) > getChildren(null).indexOf(afterChild) fun ASTNode.isChildAfterGroup(child: ASTNode, group: List): Boolean = - getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.maxOrNull() ?: 0) + getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.max() ?: 0) fun ASTNode.isChildBeforeAnother(child: ASTNode, beforeChild: ASTNode): Boolean = areChildrenBeforeGroup(listOf(child), listOf(beforeChild)) @@ -437,7 +437,7 @@ fun ASTNode.areChildrenBeforeChild(children: List, beforeChild: ASTNode @Suppress("UnsafeCallOnNullableType") fun ASTNode.areChildrenBeforeGroup(children: List, group: List): Boolean { require(children.isNotEmpty() && group.isNotEmpty()) { "no sense to operate on empty lists" } - return children.map { getChildren(null).indexOf(it) }.maxOrNull()!! < group.map { getChildren(null).indexOf(it) }.minOrNull()!! + return children.map { getChildren(null).indexOf(it) }.max()!! < group.map { getChildren(null).indexOf(it) }.min()!! } fun List.handleIncorrectOrder(getSiblingBlocks: ASTNode.() -> Pair, diff --git a/pom.xml b/pom.xml index 86d093dd19..fe4487d27d 100644 --- a/pom.xml +++ b/pom.xml @@ -239,6 +239,7 @@ ${kotlin.version} 1.8 + 1.3 src/main/kotlin src/main/resources