Repository: analysis-dev/diktat (kotlin)

Issue:
Magic number rule failure
**Diktat version: 0.5.2**

```
[ERROR] 2021-05-08 23:21:20 Internal error has occurred in magic-number. Please make an issue on this bug at https://github.com/cqfn/diKTat/.
java.lang.ClassCastException: class org.jetbrains.kotlin.psi.KtParameter cannot be cast to class org.jetbrains.kotlin.psi.KtProperty (org.jetbrains.kotlin.psi.KtParameter and org.jetbr
ains.kotlin.psi.KtProperty are in unnamed module of loader 'app')
        at org.cqfn.diktat.ruleset.rules.chapter3.MagicNumberRule$checkNumber$isLocalVariable$1.invoke(MagicNumberRule.kt:52)
        at org.cqfn.diktat.ruleset.rules.chapter3.MagicNumberRule$checkNumber$isLocalVariable$1.invoke(MagicNumberRule.kt:29)
        at com.pinterest.ktlint.core.ast.PackageKt.parent(package.kt:159)
        at com.pinterest.ktlint.core.ast.PackageKt.parent$default(package.kt:156)
        at org.cqfn.diktat.ruleset.rules.chapter3.MagicNumberRule.checkNumber(MagicNumberRule.kt:52)
        at org.cqfn.diktat.ruleset.rules.chapter3.MagicNumberRule.logic(MagicNumberRule.kt:41)
        at org.cqfn.diktat.ruleset.rules.DiktatRule.visit(DiktatRule.kt:46)
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:271)
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:23)
        at com.pinterest.ktlint.core.KtLint$visitor$2$2.invoke(KtLint.kt:212)
        at com.pinterest.ktlint.core.KtLint$visitor$2$2.invoke(KtLint.kt:23)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:224)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:225)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:225)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:225)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:225)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:225)
        at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:211)
        at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:23)
        at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:264)
        at com.pinterest.ktlint.internal.FileUtilsKt.formatFile(FileUtils.kt:79)
        at com.pinterest.ktlint.KtlintCommandLine.process(Main.kt:331)
        at com.pinterest.ktlint.KtlintCommandLine.access$process(Main.kt:116)
        at com.pinterest.ktlint.KtlintCommandLine$lintFiles$2$1.call(Main.kt:257)
        at com.pinterest.ktlint.KtlintCommandLine$lintFiles$2$1.call(Main.kt:116)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at java.base/java.lang.Thread.run(Thread.java:832)
[ERROR] 2021-05-08 23:21:20 As a workaround you can disable these inspections in yml config: [MAGIC_NUMBER]
```

Pull request:
###  What's done:
 - [x] Added new flag to handle value parameter
 - [x] Added this flag to config files
 - [x] Added tests
 - [ ] Closes #869

Patch:
diff --git a/diktat-analysis.yml b/diktat-analysis.yml
index fc8c6764ee..14bfcb612c 100644
--- a/diktat-analysis.yml
+++ b/diktat-analysis.yml
@@ -312,6 +312,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
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt
index cf6174d2a0..643362eb52 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt
@@ -16,6 +16,7 @@ import com.pinterest.ktlint.core.ast.ElementType.MINUS
 import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE
 import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
 import com.pinterest.ktlint.core.ast.ElementType.RANGE
+import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER
 import com.pinterest.ktlint.core.ast.parent
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
 import org.jetbrains.kotlin.psi.KtFunction
@@ -49,7 +50,8 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
         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.isVarProperty() && (it.psi as KtProperty).isLocal }) != null
+        val isLocalVariable = node.parent({ it.elementType == PROPERTY && it.isVarProperty() && (it.psi as KtProperty).isLocal }) != null
+        val isValueParameter = node.parent({ it.elementType == VALUE_PARAMETER }) != 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 {
@@ -58,7 +60,7 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
         }
         val isExtensionFunctions = node.parent({ it.elementType == FUN && (it.psi as KtFunction).isExtensionDeclaration() }) != null &&
                 node.parents().find { it.elementType == PROPERTY } == null
-        val result = listOf(isHashFunction, isPropertyDeclaration, isLocalVariable, isConstant,
+        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) {
             MAGIC_NUMBER.warn(configRules, emitWarn, isFixMode, nodeText, node.startOffset, node)
@@ -98,6 +100,7 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
             "ignoreHashCodeFunction" to true,
             "ignorePropertyDeclaration" to false,
             "ignoreLocalVariableDeclaration" to false,
+            "ignoreValueParameter" to true,
             "ignoreConstantDeclaration" to true,
             "ignoreCompanionObjectPropertyDeclaration" to true,
             "ignoreEnums" to false,
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 e68bb1c44c..6b6359f8b5 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
@@ -29,6 +29,7 @@ import com.pinterest.ktlint.core.ast.ElementType.LBRACE
 import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
 import com.pinterest.ktlint.core.ast.ElementType.OVERRIDE_KEYWORD
 import com.pinterest.ktlint.core.ast.ElementType.PRIVATE_KEYWORD
+import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
 import com.pinterest.ktlint.core.ast.ElementType.PROTECTED_KEYWORD
 import com.pinterest.ktlint.core.ast.ElementType.PUBLIC_KEYWORD
 import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml
index e49788b609..9354b65640 100644
--- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml
+++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml
@@ -312,6 +312,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
diff --git a/examples/maven/diktat-analysis.yml b/examples/maven/diktat-analysis.yml
index 3f953a53ed..f11ea46c37 100644
--- a/examples/maven/diktat-analysis.yml
+++ b/examples/maven/diktat-analysis.yml
@@ -308,6 +308,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


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt
index eddea55859..baffac256c 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt
@@ -20,6 +20,7 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) {
                 "ignoreHashCodeFunction" to "true",
                 "ignorePropertyDeclaration" to "true",
                 "ignoreLocalVariableDeclaration" to "true",
+                "ignoreValueParameter" to "false",
                 "ignoreConstantDeclaration" to "true",
                 "ignoreCompanionObjectPropertyDeclaration" to "true",
                 "ignoreEnums" to "true",
@@ -142,4 +143,42 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) {
                 """.trimMargin(), rulesConfigList = rulesConfigMagicNumber
         )
     }
+
+    @Test
+    @Tag(WarningNames.MAGIC_NUMBER)
+    fun `check value parameter`() {
+        lintMethod(
+            """
+                class TomlDecoder(
+                    var elementsCount: Int = 100
+                )
+            """.trimMargin(),
+        )
+    }
+
+    @Test
+    @Tag(WarningNames.MAGIC_NUMBER)
+    fun `check value parameter with config`() {
+        lintMethod(
+            """
+                class TomlDecoder(
+                    var elementsCount: Int = 100
+                )
+            """.trimMargin(),
+            LintError(2, 46, ruleId, "${MAGIC_NUMBER.warnText()} 100", false),
+            rulesConfigList = rulesConfigMagicNumber
+        )
+    }
+
+    @Test
+    @Tag(WarningNames.MAGIC_NUMBER)
+    fun `check value parameter in function with config`() {
+        lintMethod(
+            """
+                fun TomlDecoder(elementsCount: Int = 100) {}
+            """.trimMargin(),
+            LintError(1, 54, ruleId, "${MAGIC_NUMBER.warnText()} 100", false),
+            rulesConfigList = rulesConfigMagicNumber
+        )
+    }
 }