Skip to content

Commit

Permalink
Fix NPE in GENERIC_VARIABLE_WRONG_DECLARATION (#1098)
Browse files Browse the repository at this point in the history
### What's done:
* Fix bug - allow cases with wild card types
* Add tests
  • Loading branch information
kgevorkyan authored Nov 8, 2021
1 parent 45f91c9 commit d249fbb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class VariableGenericTypeDeclarationRule(configRules: List<RulesConfig>) : Dikta
}
}

@Suppress("UnsafeCallOnNullableType")
@Suppress("UnsafeCallOnNullableType", "AVOID_NULL_CHECKS")
private fun handleProperty(node: ASTNode) {
val callExpr = node.findChildByType(CALL_EXPRESSION)
?: node
Expand All @@ -54,6 +54,11 @@ class VariableGenericTypeDeclarationRule(configRules: List<RulesConfig>) : 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 }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>()) {
|
| }
|}
""".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<Int, String>()) {
|
| }
|}
""".trimMargin()
)
}

@Test
@Tag(GENERIC_VARIABLE_WRONG_DECLARATION)
fun `property in function as parameter bad`() {
Expand Down Expand Up @@ -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<Int>()
| }
|}
""".trimMargin()
)
}

@Test
@Tag(GENERIC_VARIABLE_WRONG_DECLARATION)
fun `property in function bad`() {
Expand Down Expand Up @@ -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<Int>()) {
|
|}
""".trimMargin()
)
}

@Test
@Tag(GENERIC_VARIABLE_WRONG_DECLARATION)
fun `property in class bad`() {
Expand Down

0 comments on commit d249fbb

Please sign in to comment.