From a0c564eff1648506b155be90f0040d76762483b5 Mon Sep 17 00:00:00 2001 From: Alexander Drugakov Date: Tue, 19 Dec 2023 17:29:17 +0300 Subject: [PATCH] Fixed `MAGIC_NUMBER` (#1883) ### What's done: - Fixed case when configuration `ignoreLocalVariableDeclaration` didn't take into account variables with `val`. - Fixed case when searching for properties using configuration `ignorePropertyDeclaration` also found ranges and local variables, for which there are corresponding configurations `ignoreRanges` and `ignoreLocalVariableDeclaration`. - Added new configuration `ignorePairsCreatedUsingTo` for ignoring pairs created using `to`. - Reworked warnings tests with many different configuration presets. Closes #1826 --- diktat-analysis.yml | 4 +- .../ruleset/rules/chapter3/MagicNumberRule.kt | 29 +- .../main/resources/diktat-analysis-huawei.yml | 4 +- .../src/main/resources/diktat-analysis.yml | 4 + .../chapter3/MagicNumberRuleWarnTest.kt | 388 ++++++++++++++---- .../gradle-groovy-dsl/diktat-analysis.yml | 4 + .../diktat-analysis.yml | 4 + .../gradle-kotlin-dsl/diktat-analysis.yml | 4 + .../maven-multiproject/diktat-analysis.yml | 4 + examples/maven/diktat-analysis.yml | 4 + 10 files changed, 359 insertions(+), 90 deletions(-) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index 10586be2ed..feb3f39a24 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -323,7 +323,7 @@ # Ignore numbers from test ignoreTest: "true" # Ignore numbers - ignoreNumbers: "-1, 1, 0, 2" + ignoreNumbers: "-1, 1, 0, 2, 0U, 1U, 2U, -1L, 0L, 1L, 2L, 0UL, 1UL, 2UL" # Is ignore override hashCode function ignoreHashCodeFunction: "true" # Is ignore property @@ -342,6 +342,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true diff --git a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter3/MagicNumberRule.kt b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter3/MagicNumberRule.kt index 7ae8d77cbe..78838dfa21 100644 --- a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter3/MagicNumberRule.kt +++ b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter3/MagicNumberRule.kt @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.KtNodeTypes.OPERATION_REFERENCE import org.jetbrains.kotlin.KtNodeTypes.PROPERTY import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER import org.jetbrains.kotlin.com.intellij.lang.ASTNode +import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER import org.jetbrains.kotlin.lexer.KtTokens.MINUS import org.jetbrains.kotlin.lexer.KtTokens.RANGE import org.jetbrains.kotlin.psi.KtFunction @@ -52,23 +53,30 @@ class MagicNumberRule(configRules: List) : DiktatRule( @Suppress("ComplexMethod") private fun checkNumber(node: ASTNode, configuration: MagicNumberConfiguration) { val nodeText = node.treePrev?.let { if (it.elementType == OPERATION_REFERENCE && it.hasChildOfType(MINUS)) "-${node.text}" else node.text } ?: node.text + val isIgnoreNumber = configuration.ignoreNumbers.contains(nodeText) + val isHashFunction = node.parent { it.elementType == FUN && it.isHashFun() } != null - val isConstant = node.parent { it.elementType == PROPERTY && it.isConstant() } != null - val isPropertyDeclaration = !isConstant && node.parent { it.elementType == PROPERTY && !it.isNodeFromCompanionObject() } != null - val isLocalVariable = node.parent { it.elementType == PROPERTY && it.isVarProperty() && (it.psi as KtProperty).isLocal } != null + val isLocalVariable = node.parent { it.elementType == PROPERTY && (it.isVarProperty() || it.isValProperty()) && (it.psi as KtProperty).isLocal } != null val isValueParameter = node.parent { it.elementType == VALUE_PARAMETER } != null + val isConstant = node.parent { it.elementType == PROPERTY && it.isConstant() } != null val isCompanionObjectProperty = node.parent { it.elementType == PROPERTY && it.isNodeFromCompanionObject() } != null val isEnums = node.parent { it.elementType == ENUM_ENTRY } != null - val isRanges = node.treeParent.run { - this.elementType == BINARY_EXPRESSION && - this.findChildByType(OPERATION_REFERENCE)?.hasChildOfType(RANGE) ?: false + val isRanges = node.treeParent.let { + it.elementType == BINARY_EXPRESSION && it.findChildByType(OPERATION_REFERENCE)?.hasChildOfType(RANGE) ?: false } val isExtensionFunctions = node.parent { it.elementType == FUN && (it.psi as KtFunction).isExtensionDeclaration() } != null && node.parents().none { it.elementType == PROPERTY } - val result = listOf(isHashFunction, isPropertyDeclaration, isLocalVariable, isValueParameter, isConstant, - isCompanionObjectProperty, isEnums, isRanges, isExtensionFunctions).zip(mapConfiguration.map { configuration.getParameter(it.key) }) - if (result.any { it.first && it.first != it.second } && !isIgnoreNumber) { + val isPairsCreatedUsingTo = node.treeParent.let { + it.elementType == BINARY_EXPRESSION && it.findChildByType(OPERATION_REFERENCE)?.findChildByType(IDENTIFIER)?.text == "to" + } + val isPropertyDeclaration = !isLocalVariable && !isConstant && !isCompanionObjectProperty && !isRanges && !isPairsCreatedUsingTo && + node.parent { it.elementType == PROPERTY } != null + + val result = listOf(isHashFunction, isPropertyDeclaration, isLocalVariable, isValueParameter, isConstant, isCompanionObjectProperty, isEnums, isRanges, + isExtensionFunctions, isPairsCreatedUsingTo).zip(mapConfiguration.map { configuration.getParameter(it.key) }) + + if (result.any { it.first && !it.second } && !isIgnoreNumber) { MAGIC_NUMBER.warn(configRules, emitWarn, nodeText, node.startOffset, node) } } @@ -125,6 +133,7 @@ class MagicNumberRule(configRules: List) : DiktatRule( "ignoreCompanionObjectPropertyDeclaration" to true, "ignoreEnums" to false, "ignoreRanges" to false, - "ignoreExtensionFunctions" to false) + "ignoreExtensionFunctions" to false, + "ignorePairsCreatedUsingTo" to false) } } diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index ad70140bcd..4db7d3932c 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -320,7 +320,7 @@ # Ignore numbers from test ignoreTest: "true" # Ignore numbers - ignoreNumbers: "-1, 1, 0, 2" + ignoreNumbers: "-1, 1, 0, 2, 0U, 1U, 2U, -1L, 0L, 1L, 2L, 0UL, 1UL, 2UL" # Is ignore override hashCode function ignoreHashCodeFunction: "true" # Is ignore property @@ -339,6 +339,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index e5a8b6e79e..59015a6785 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -327,6 +327,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object @@ -337,6 +339,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt index eb0c1562fd..f4e5744559 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt @@ -1,44 +1,203 @@ package com.saveourtool.diktat.ruleset.chapter3 +import com.saveourtool.diktat.api.DiktatError import com.saveourtool.diktat.common.config.rules.DIKTAT_RULE_SET_ID import com.saveourtool.diktat.common.config.rules.RulesConfig import com.saveourtool.diktat.ruleset.constants.Warnings.MAGIC_NUMBER import com.saveourtool.diktat.ruleset.rules.chapter3.MagicNumberRule import com.saveourtool.diktat.util.LintTestBase - -import com.saveourtool.diktat.api.DiktatError import generated.WarningNames import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test -import org.junit.jupiter.api.io.TempDir -import java.nio.file.Path class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) { private val ruleId = "$DIKTAT_RULE_SET_ID:${MagicNumberRule.NAME_ID}" - private val rulesConfigMagicNumber: List = listOf( + private val rulesConfigIgnoreNone: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreNumbers: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreNumbers" to "50,-240, 128L, -3.5f, 4, 11UL", + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreHashCodeFunction: List = listOf( RulesConfig( MAGIC_NUMBER.name, true, mapOf( "ignoreHashCodeFunction" to "true", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnorePropertyDeclaration: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", "ignorePropertyDeclaration" to "true", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreLocalVariableDeclaration: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", "ignoreLocalVariableDeclaration" to "true", "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreValueParameter: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "true", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreConstantDeclaration: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", "ignoreConstantDeclaration" to "true", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreCompanionObjectPropertyDeclaration: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", "ignoreCompanionObjectPropertyDeclaration" to "true", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreEnums: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", "ignoreEnums" to "true", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreRanges: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", "ignoreRanges" to "true", - "ignoreExtensionFunctions" to "true")) + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "false")) + ) + private val rulesConfigIgnoreExtensionFunctions: List = listOf( + RulesConfig( + MAGIC_NUMBER.name, true, + mapOf( + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "true", + "ignorePairsCreatedUsingTo" to "false")) ) - private val rulesConfigIgnoreNumbersMagicNumber: List = listOf( + private val rulesConfigIgnorePairsCreatedUsingTo: List = listOf( RulesConfig( MAGIC_NUMBER.name, true, mapOf( - "ignoreNumbers" to "50,-240, 128L, -3.5f")) + "ignoreHashCodeFunction" to "false", + "ignorePropertyDeclaration" to "false", + "ignoreLocalVariableDeclaration" to "false", + "ignoreValueParameter" to "false", + "ignoreConstantDeclaration" to "false", + "ignoreCompanionObjectPropertyDeclaration" to "false", + "ignoreEnums" to "false", + "ignoreRanges" to "false", + "ignoreExtensionFunctions" to "false", + "ignorePairsCreatedUsingTo" to "true")) ) @Test @Tag(WarningNames.MAGIC_NUMBER) - fun `simple check`() { + @Suppress("LongMethod") + fun `check all`() { lintMethod( """ |fun f1oo() { @@ -56,17 +215,71 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) { | return 32 |} | - |class Cl{ + |val abc = 32 + |var abc2 = 32 + | + |fun foo() { + | val a = 3 + | var a2 = 3 + |} + | + |class TomlDecoder( + | val elementsCount: Int = 100, + | var elementsCount2: Int = 100 + |) + | + |fun TomlDecoder(elementsCount: Int = 100) {} + | + |const val topLevel = 31 + | + |class A { | companion object { - | private const val AA = 4 + | val b = 3 + | var b2 = 4 | } |} + | + |enum class A(b:Int) { + | A(3), + | B(4), + | C(5), + |} + | + |val tagLengthRange = 3..15 + |var tagLengthRange2 = 3..15 + | + |fun Int.foo() = 3 + | + |val fg = abc to 3 + |var fg2 = abc to 4 """.trimMargin(), DiktatError(2, 18, ruleId, "${MAGIC_NUMBER.warnText()} 4", false), DiktatError(3, 12, ruleId, "${MAGIC_NUMBER.warnText()} 128L", false), DiktatError(4, 12, ruleId, "${MAGIC_NUMBER.warnText()} 3.4f", false), DiktatError(5, 12, ruleId, "${MAGIC_NUMBER.warnText()} 4", false), - DiktatError(5, 14, ruleId, "${MAGIC_NUMBER.warnText()} 3", false) + DiktatError(5, 14, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(13, 11, ruleId, "${MAGIC_NUMBER.warnText()} 32", false), + DiktatError(16, 11, ruleId, "${MAGIC_NUMBER.warnText()} 32", false), + DiktatError(17, 12, ruleId, "${MAGIC_NUMBER.warnText()} 32", false), + DiktatError(20, 12, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(21, 13, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(25, 30, ruleId, "${MAGIC_NUMBER.warnText()} 100", false), + DiktatError(26, 31, ruleId, "${MAGIC_NUMBER.warnText()} 100", false), + DiktatError(29, 38, ruleId, "${MAGIC_NUMBER.warnText()} 100", false), + DiktatError(31, 22, ruleId, "${MAGIC_NUMBER.warnText()} 31", false), + DiktatError(35, 16, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(36, 17, ruleId, "${MAGIC_NUMBER.warnText()} 4", false), + DiktatError(41, 6, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(42, 6, ruleId, "${MAGIC_NUMBER.warnText()} 4", false), + DiktatError(43, 6, ruleId, "${MAGIC_NUMBER.warnText()} 5", false), + DiktatError(46, 22, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(46, 25, ruleId, "${MAGIC_NUMBER.warnText()} 15", false), + DiktatError(47, 23, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(47, 26, ruleId, "${MAGIC_NUMBER.warnText()} 15", false), + DiktatError(49, 17, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(51, 17, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + DiktatError(52, 18, ruleId, "${MAGIC_NUMBER.warnText()} 4", false), + rulesConfigList = rulesConfigIgnoreNone ) } @@ -77,8 +290,11 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) { """ |fun f1oo() { | val m = -1 + | var m2 = -1 | val a: Byte = 4 + | var a2: Byte = 4 | val b = 0xff + | var b2 = 0xff |} | |enum class A(b:Int) { @@ -86,130 +302,146 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) { | B(50), | C(3), |} + """.trimMargin(), + DiktatError(2, 13, ruleId, "${MAGIC_NUMBER.warnText()} -1", false), + DiktatError(3, 14, ruleId, "${MAGIC_NUMBER.warnText()} -1", false), + DiktatError(6, 12, ruleId, "${MAGIC_NUMBER.warnText()} 0xff", false), + DiktatError(7, 13, ruleId, "${MAGIC_NUMBER.warnText()} 0xff", false), + DiktatError(13, 6, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), + rulesConfigList = rulesConfigIgnoreNumbers + ) + } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check ignore hash code function`() { + lintMethod( + """ |@Override |fun hashCode(): Int { | return 32 |} """.trimMargin(), - DiktatError(2, 13, ruleId, "${MAGIC_NUMBER.warnText()} -1", false), - DiktatError(3, 18, ruleId, "${MAGIC_NUMBER.warnText()} 4", false), - DiktatError(4, 12, ruleId, "${MAGIC_NUMBER.warnText()} 0xff", false), - DiktatError(10, 6, ruleId, "${MAGIC_NUMBER.warnText()} 3", false), - rulesConfigList = rulesConfigIgnoreNumbersMagicNumber + rulesConfigList = rulesConfigIgnoreHashCodeFunction ) } @Test @Tag(WarningNames.MAGIC_NUMBER) - fun `check ignore top level constants`() { + fun `check ignore property declaration`() { lintMethod( """ - |const val topLevel = 31 - | - |val shouldTrigger = 32 - | - |fun some() { - | - |} + |val abc = 32 + |var abc2 = 32 """.trimMargin(), - DiktatError(3, 21, ruleId, "${MAGIC_NUMBER.warnText()} 32", false), + rulesConfigList = rulesConfigIgnorePropertyDeclaration ) } @Test @Tag(WarningNames.MAGIC_NUMBER) - fun `check all param in config true`() { + fun `check ignore local variable declaration`() { lintMethod( """ |fun foo() { - | var a = 3 + | val a = 3 + | var a2 = 3 |} + """.trimMargin(), + rulesConfigList = rulesConfigIgnoreLocalVariableDeclaration + ) + } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check ignore value parameter`() { + lintMethod( + """ + |class TomlDecoder( + | val elementsCount: Int = 100, + | var elementsCount2: Int = 100 + |) | - |const val aa = 21.5f - | + |fun TomlDecoder(elementsCount: Int = 100) {} + """.trimMargin(), + rulesConfigList = rulesConfigIgnoreValueParameter + ) + } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check ignore constant declaration`() { + lintMethod( + """ + |const val topLevel = 31 + """.trimMargin(), + rulesConfigList = rulesConfigIgnoreConstantDeclaration + ) + } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check ignore companion object property declaration`() { + lintMethod( + """ |class A { | companion object { - | val b = 2 + | val b = 3 + | var b2 = 4 | } |} - | - |enum class A(b:Int) { - | A(1), - | B(2), - | C(3), - |} - | - |fun goo() { - | val q = 100.1000 - |} - | - |fun Int.foo() = 2 - """.trimMargin(), rulesConfigList = rulesConfigMagicNumber + """.trimMargin(), + rulesConfigList = rulesConfigIgnoreCompanionObjectPropertyDeclaration ) } @Test @Tag(WarningNames.MAGIC_NUMBER) - fun `check value parameter`() { + fun `check ignore enums`() { lintMethod( """ - class TomlDecoder( - var elementsCount: Int = 100 - ) + |enum class A(b:Int) { + | A(3), + | B(4), + | C(5), + |} """.trimMargin(), + rulesConfigList = rulesConfigIgnoreEnums ) } @Test @Tag(WarningNames.MAGIC_NUMBER) - fun `check value parameter with config`() { + fun `check ignore ranges`() { lintMethod( """ - class TomlDecoder( - var elementsCount: Int = 100 - ) + |val tagLengthRange = 3..15 + |var tagLengthRange2 = 3..15 """.trimMargin(), - DiktatError(2, 46, ruleId, "${MAGIC_NUMBER.warnText()} 100", false), - rulesConfigList = rulesConfigMagicNumber + rulesConfigList = rulesConfigIgnoreRanges ) } @Test @Tag(WarningNames.MAGIC_NUMBER) - fun `check value parameter in function with config`() { + fun `check ignore extension functions`() { lintMethod( """ - fun TomlDecoder(elementsCount: Int = 100) {} + |fun Int.foo() = 3 """.trimMargin(), - DiktatError(1, 54, ruleId, "${MAGIC_NUMBER.warnText()} 100", false), - rulesConfigList = rulesConfigMagicNumber + rulesConfigList = rulesConfigIgnoreExtensionFunctions ) } @Test @Tag(WarningNames.MAGIC_NUMBER) - fun `check ignore numbers in test`(@TempDir tempDir: Path) { - lintMethodWithFile( + fun `check ignore pairs created using 'to'`() { + lintMethod( """ - |fun f1oo() { - | val m = -1 - | val a: Byte = 4 - | val b = 0xff - |} - | - |enum class A(b:Int) { - | A(-240), - | B(50), - | C(3), - |} - |@Override - |fun hashCode(): Int { - | return 32 - |} + |val fg = abc to 3 + |var fg2 = abc to 4 """.trimMargin(), - tempDir = tempDir, - fileName = "src/test/kotlin/com/saveourtool/diktat/test/hehe/MagicNumberTest.kt", - rulesConfigList = rulesConfigIgnoreNumbersMagicNumber, + rulesConfigList = rulesConfigIgnorePairsCreatedUsingTo ) } } diff --git a/examples/gradle-groovy-dsl/diktat-analysis.yml b/examples/gradle-groovy-dsl/diktat-analysis.yml index e5a8b6e79e..59015a6785 100644 --- a/examples/gradle-groovy-dsl/diktat-analysis.yml +++ b/examples/gradle-groovy-dsl/diktat-analysis.yml @@ -327,6 +327,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object @@ -337,6 +339,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true diff --git a/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml b/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml index e5a8b6e79e..59015a6785 100644 --- a/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml +++ b/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml @@ -327,6 +327,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object @@ -337,6 +339,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true diff --git a/examples/gradle-kotlin-dsl/diktat-analysis.yml b/examples/gradle-kotlin-dsl/diktat-analysis.yml index e5a8b6e79e..59015a6785 100644 --- a/examples/gradle-kotlin-dsl/diktat-analysis.yml +++ b/examples/gradle-kotlin-dsl/diktat-analysis.yml @@ -327,6 +327,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object @@ -337,6 +339,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true diff --git a/examples/maven-multiproject/diktat-analysis.yml b/examples/maven-multiproject/diktat-analysis.yml index e5a8b6e79e..59015a6785 100644 --- a/examples/maven-multiproject/diktat-analysis.yml +++ b/examples/maven-multiproject/diktat-analysis.yml @@ -327,6 +327,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object @@ -337,6 +339,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true diff --git a/examples/maven/diktat-analysis.yml b/examples/maven/diktat-analysis.yml index e5a8b6e79e..59015a6785 100644 --- a/examples/maven/diktat-analysis.yml +++ b/examples/maven/diktat-analysis.yml @@ -327,6 +327,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object @@ -337,6 +339,8 @@ ignoreRanges: "false" # Is ignore number in extension function ignoreExtensionFunctions: "false" + # Is ignore number in pairs created using to + ignorePairsCreatedUsingTo: "false" # Checks that order of enum values or constant property inside companion is correct - name: WRONG_DECLARATIONS_ORDER enabled: true