From 658d748857edad3c2875c7a7f885143ab41da619 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Mon, 8 Nov 2021 15:03:36 +0300 Subject: [PATCH 1/2] Bugfix && add tests ### What's done: * Bugfix && add tests --- .../VariableGenericTypeDeclarationRule.kt | 5 ++ ...iableGenericTypeDeclarationRuleWarnTest.kt | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+) 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..7343d2c2b9 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 @@ -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..c252d8337b 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,35 @@ 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 +125,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 +167,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`() { From aa7e60a3be80f8a03871e36fe7d1d29bfaec491a Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Mon, 8 Nov 2021 15:31:43 +0300 Subject: [PATCH 2/2] Codestyle ### What's done: * Code style --- .../rules/chapter4/VariableGenericTypeDeclarationRule.kt | 2 +- .../chapter4/VariableGenericTypeDeclarationRuleWarnTest.kt | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) 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 7343d2c2b9..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 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 c252d8337b..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 @@ -94,7 +94,6 @@ class VariableGenericTypeDeclarationRuleWarnTest : LintTestBase(::VariableGeneri ) } - @Test @Tag(GENERIC_VARIABLE_WRONG_DECLARATION) fun `property in function as parameter bad`() {