From d249fbbba9d34b89cd21885c065542f3604d7752 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Mon, 8 Nov 2021 19:46:59 +0700 Subject: [PATCH] Fix NPE in GENERIC_VARIABLE_WRONG_DECLARATION (#1098) ### What's done: * Fix bug - allow cases with wild card types * Add tests --- .../VariableGenericTypeDeclarationRule.kt | 7 ++- ...iableGenericTypeDeclarationRuleWarnTest.kt | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/VariableGenericTypeDeclarationRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/VariableGenericTypeDeclarationRule.kt index 76e508e3f8..b90dde5ff6 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/VariableGenericTypeDeclarationRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/VariableGenericTypeDeclarationRule.kt @@ -33,7 +33,7 @@ class VariableGenericTypeDeclarationRule(configRules: List) : Dikta } } - @Suppress("UnsafeCallOnNullableType") + @Suppress("UnsafeCallOnNullableType", "AVOID_NULL_CHECKS") private fun handleProperty(node: ASTNode) { val callExpr = node.findChildByType(CALL_EXPRESSION) ?: node @@ -54,6 +54,11 @@ class VariableGenericTypeDeclarationRule(configRules: List) : Dikta ?.typeArgumentsAsTypes } + // Allow cases with wild card types; `*` interprets as `null` in list of types + if (leftSide?.any { it == null } == true) { + return + } + if (rightSide != null && leftSide != null && rightSide.size == leftSide.size && rightSide.zip(leftSide).all { (first, second) -> first.text == second.text }) { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/VariableGenericTypeDeclarationRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/VariableGenericTypeDeclarationRuleWarnTest.kt index ed1757d561..5cc76f3380 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/VariableGenericTypeDeclarationRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/VariableGenericTypeDeclarationRuleWarnTest.kt @@ -66,6 +66,34 @@ class VariableGenericTypeDeclarationRuleWarnTest : LintTestBase(::VariableGeneri ) } + @Test + @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) + fun `property in function as parameter with wildcard type good`() { + lintMethod( + """ + |class SomeClass { + | private fun someFunc(myVariable: List<*> = emptyList()) { + | + | } + |} + """.trimMargin() + ) + } + + @Test + @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) + fun `property in function as parameter with wildcard type good 2`() { + lintMethod( + """ + |class SomeClass { + | private fun someFunc(myVariable: Map<*, String> = emptyMap()) { + | + | } + |} + """.trimMargin() + ) + } + @Test @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) fun `property in function as parameter bad`() { @@ -96,6 +124,20 @@ class VariableGenericTypeDeclarationRuleWarnTest : LintTestBase(::VariableGeneri ) } + @Test + @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) + fun `property in function with wildcard type good`() { + lintMethod( + """ + |class SomeClass { + | private fun someFunc() { + | val myVariable: List<*> = emptyList() + | } + |} + """.trimMargin() + ) + } + @Test @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) fun `property in function bad`() { @@ -124,6 +166,18 @@ class VariableGenericTypeDeclarationRuleWarnTest : LintTestBase(::VariableGeneri ) } + @Test + @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) + fun `property in class with wildcard type good`() { + lintMethod( + """ + |class SomeClass(val myVariable: List<*> = emptyList()) { + | + |} + """.trimMargin() + ) + } + @Test @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) fun `property in class bad`() {