Repository: cqfn/diKTat (kotlin)

Issue:
Top-level constants should not be considered magic number
## Describe the bug
```kotlin
package com.example

private const val FOO = 42

class Example
```

## Environment information
* diktat version: 0.5.0

Pull request:
### What's done:
  * Fixed bug


This pull request closes #841

Patch:
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 828016e2c2..c53573e5d9 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
@@ -8,6 +8,7 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule
 import org.cqfn.diktat.ruleset.utils.*
 
 import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION
+import com.pinterest.ktlint.core.ast.ElementType.CONST_KEYWORD
 import com.pinterest.ktlint.core.ast.ElementType.ENUM_ENTRY
 import com.pinterest.ktlint.core.ast.ElementType.FLOAT_CONSTANT
 import com.pinterest.ktlint.core.ast.ElementType.FUN
@@ -47,9 +48,9 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
         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 isPropertyDeclaration = node.parent({ it.elementType == PROPERTY && !it.isNodeFromCompanionObject() }) != null
-        val isLocalVariable = node.parent({ it.isVarProperty() && (it.psi as KtProperty).isLocal }) != 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 isCompanionObjectProperty = node.parent({ it.elementType == PROPERTY && it.isNodeFromCompanionObject() }) != null
         val isEnums = node.parent({ it.elementType == ENUM_ENTRY }) != null
         val isRanges = node.treeParent.run {


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 731a4a4250..eddea55859 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
@@ -94,6 +94,23 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) {
         )
     }
 
+    @Test
+    @Tag(WarningNames.MAGIC_NUMBER)
+    fun `check ignore top level constants`() {
+        lintMethod(
+            """
+            |const val topLevel = 31
+            |
+            |val shouldTrigger = 32
+            |
+            |fun some() {
+            |
+            |}
+            """.trimMargin(),
+            LintError(3, 21, ruleId, "${MAGIC_NUMBER.warnText()} 32", false),
+        )
+    }
+
     @Test
     @Tag(WarningNames.MAGIC_NUMBER)
     fun `check all param in config true`() {