Repository: analysis-dev/diktat (kotlin)

Issue:
IDENTIFIER_NAME rule causes NPE
```
[ERROR] 2021-11-09 16:29:07 Internal error has occurred in rule [identifier-naming]. Please make an issue on this bug at https://github.com/cqfn/diKTat/.
                       As a workaround you can disable these inspections in yml config: <[BACKTICKS_PROHIBITED, VARIABLE_NAME_INCORRECT, VARIABLE_NAME_INCORRECT_FORMAT, CONSTANT_UPPERC
ASE, VARIABLE_HAS_PREFIX, CONFUSING_IDENTIFIER_NAMING, GENERIC_NAME, CLASS_NAME_INCORRECT, ENUM_VALUE, EXCEPTION_SUFFIX, FUNCTION_BOOLEAN_PREFIX, FUNCTION_NAME_INCORRECT_CASE, IDENTIFI
ER_LENGTH, OBJECT_NAME_INCORRECT, TYPEALIAS_NAME_INCORRECT_CASE]>.
                       Root cause of the problem is in [D:\reflekt\reflekt-plugin\src\main\kotlin\io\reflekt\plugin\utils\Util.kt] file.
java.lang.NullPointerException
        at org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming$checkVariableName$1$2.invoke(IdentifierNaming.kt:219)
        at org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming$checkVariableName$1$2.invoke(IdentifierNaming.kt:205)
        at org.cqfn.diktat.ruleset.constants.Warnings.warnAndFix(Warnings.kt:260)
        at org.cqfn.diktat.ruleset.constants.Warnings.warnAndFix$default(Warnings.kt:202)
        at org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming.checkVariableName(IdentifierNaming.kt:205)
        at org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming.logic(IdentifierNaming.kt:128)
        at org.cqfn.diktat.ruleset.rules.DiktatRule.visit(DiktatRule.kt:50)
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:381)

```

Small reproducer: 

```
package io.reflekt.plugin.utils

object Util {
    private val GET_USES: String
}

```

Pull request description:
## Which rule and warnings did you add?

This pull request closes <!-- insert related issue number -->
<!-- Briefly describe rule and warnings -->

## Actions checklist
* [ ] Implemented Rule, added Warnings
* [ ] Added tests on checks
* [ ] Added tests on fixers
* [ ] Updated diktat-analysis.yml
* [ ] Updated available-rules.md

## Fixme

<!-- Is there anything left out of scope of this PR? -->

Patch:
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 1de54ef6d9..9974a7a8e2 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
@@ -98,7 +98,7 @@ enum class Warnings(
     FILE_IS_TOO_LONG(false, "3.1.1", "file has more number of lines than expected"),
     FILE_CONTAINS_ONLY_COMMENTS(false, "3.1.2", "empty files or files that contain only comments should be avoided"),
     FILE_INCORRECT_BLOCKS_ORDER(true, "3.1.2", "general structure of kotlin source file is wrong, parts are in incorrect order"),
-    FILE_NO_BLANK_LINE_BETWEEN_BLOCKS(true, "3.1.2", "general structure of kotlin source file is wrong, general code blocks sohuld be separated by empty lines"),
+    FILE_NO_BLANK_LINE_BETWEEN_BLOCKS(true, "3.1.2", "general structure of kotlin source file is wrong, general code blocks should be separated by empty lines"),
     FILE_UNORDERED_IMPORTS(true, "3.1.2", "imports should be ordered alphabetically and shouldn't be separated by newlines"),
     FILE_WILDCARD_IMPORTS(false, "3.1.2", "wildcard imports should not be used"),
     UNUSED_IMPORT(true, "3.1.2", "unused imports should be removed"),
@@ -171,7 +171,7 @@ enum class Warnings(
     EXTENSION_FUNCTION_SAME_SIGNATURE(false, "6.2.2", "extension functions should not have same signature if their receiver classes are related"),
     EMPTY_PRIMARY_CONSTRUCTOR(true, "6.1.3", "avoid empty primary constructor"),
     NO_CORRESPONDING_PROPERTY(false, "6.1.7", "backing property should have the same name, but there is no corresponding property"),
-    AVOID_USING_UTILITY_CLASS(false, "6.4.1", "avoid using utility classes/objects, use extensions functions"),
+    AVOID_USING_UTILITY_CLASS(false, "6.4.1", "avoid using utility classes/objects, use extension functions instead"),
     OBJECT_IS_PREFERRED(true, "6.4.2", "it is better to use object for stateless classes"),
     INLINE_CLASS_CAN_BE_USED(true, "6.1.12", "inline class can be used"),
     EXTENSION_FUNCTION_WITH_CLASS(false, "6.2.3", "do not use extension functions for the class defined in the same file"),
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/IdentifierNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/IdentifierNaming.kt
index 426a5f5402..d71ceb8380 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/IdentifierNaming.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/IdentifierNaming.kt
@@ -1,3 +1,5 @@
+@file:Suppress("FILE_WILDCARD_IMPORTS")
+
 package org.cqfn.diktat.ruleset.rules.chapter1
 
 import org.cqfn.diktat.common.config.rules.RuleConfiguration
@@ -19,31 +21,8 @@ import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_HAS_PREFIX
 import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT
 import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT_FORMAT
 import org.cqfn.diktat.ruleset.rules.DiktatRule
-import org.cqfn.diktat.ruleset.utils.Style
-import org.cqfn.diktat.ruleset.utils.containsOneLetterOrZero
-import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType
-import org.cqfn.diktat.ruleset.utils.findChildAfter
-import org.cqfn.diktat.ruleset.utils.findLeafWithSpecificType
-import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType
-import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
-import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
-import org.cqfn.diktat.ruleset.utils.getIdentifierName
-import org.cqfn.diktat.ruleset.utils.getTypeParameterList
-import org.cqfn.diktat.ruleset.utils.hasPrefix
-import org.cqfn.diktat.ruleset.utils.hasTestAnnotation
-import org.cqfn.diktat.ruleset.utils.isConstant
-import org.cqfn.diktat.ruleset.utils.isDigits
-import org.cqfn.diktat.ruleset.utils.isLowerCamelCase
-import org.cqfn.diktat.ruleset.utils.isOverridden
-import org.cqfn.diktat.ruleset.utils.isPascalCase
-import org.cqfn.diktat.ruleset.utils.isTextLengthInRange
-import org.cqfn.diktat.ruleset.utils.isUpperSnakeCase
-import org.cqfn.diktat.ruleset.utils.kDocTags
-import org.cqfn.diktat.ruleset.utils.removePrefix
+import org.cqfn.diktat.ruleset.utils.*
 import org.cqfn.diktat.ruleset.utils.search.findAllVariablesWithUsages
-import org.cqfn.diktat.ruleset.utils.toLowerCamelCase
-import org.cqfn.diktat.ruleset.utils.toPascalCase
-import org.cqfn.diktat.ruleset.utils.toUpperSnakeCase
 
 import com.pinterest.ktlint.core.ast.ElementType
 import com.pinterest.ktlint.core.ast.ElementType.CATCH
@@ -55,6 +34,7 @@ import com.pinterest.ktlint.core.ast.ElementType.FILE
 import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_TYPE
 import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
 import com.pinterest.ktlint.core.ast.ElementType.KDOC
+import com.pinterest.ktlint.core.ast.ElementType.OBJECT_DECLARATION
 import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
 import com.pinterest.ktlint.core.ast.ElementType.TYPE_PARAMETER
 import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
@@ -215,8 +195,8 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
                                     (this is KtParameter && getParentOfType<KtPrimaryConstructor>(true)?.valueParameters?.contains(this) == true)
                         }) {
                             // For class members also check `@property` KDoc tag.
-                            // If we are here, then `variableName` is definitely a node from a class.
-                            variableName.parent(CLASS)!!.findChildByType(KDOC)?.kDocTags()
+                            // If we are here, then `variableName` is definitely a node from a class or an object.
+                            (variableName.parent(CLASS) ?: variableName.parent(OBJECT_DECLARATION))?.findChildByType(KDOC)?.kDocTags()
                                 ?.firstOrNull {
                                     it.knownTag == KDocKnownTag.PROPERTY && it.getSubjectName() == variableName.text
                                 }


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/IdentifierNamingFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/IdentifierNamingFixTest.kt
index 41c9ff2e81..200cc3a4be 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/IdentifierNamingFixTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/IdentifierNamingFixTest.kt
@@ -16,6 +16,12 @@ class IdentifierNamingFixTest : FixTestBase(
         RulesConfig("PACKAGE_NAME_INCORRECT_PREFIX", false, emptyMap())
     )
 ) {
+    @Test
+    @Tag(WarningNames.CLASS_NAME_INCORRECT)
+    fun `regression in VARIABLE_NAME_INCORRECT_FORMAT`() {
+        fixAndCompare("identifiers/IdentifierNameRegressionExpected.kt", "identifiers/IdentifierNameRegressionTest.kt")
+    }
+
     @Test
     @Tag(WarningNames.CLASS_NAME_INCORRECT)
     fun `incorrect class name fix`() {
diff --git a/diktat-rules/src/test/resources/test/paragraph1/naming/identifiers/IdentifierNameRegressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph1/naming/identifiers/IdentifierNameRegressionExpected.kt
new file mode 100644
index 0000000000..b7c4c16f07
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/paragraph1/naming/identifiers/IdentifierNameRegressionExpected.kt
@@ -0,0 +1,5 @@
+package io.reflekt.plugin.utils
+
+object Util {
+    private val getUses: String
+}
diff --git a/diktat-rules/src/test/resources/test/paragraph1/naming/identifiers/IdentifierNameRegressionTest.kt b/diktat-rules/src/test/resources/test/paragraph1/naming/identifiers/IdentifierNameRegressionTest.kt
new file mode 100644
index 0000000000..59eec069d5
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/paragraph1/naming/identifiers/IdentifierNameRegressionTest.kt
@@ -0,0 +1,5 @@
+package io.reflekt.plugin.utils
+
+object Util {
+    private val GET_USES: String
+}