Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE in GENERIC_VARIABLE_WRONG_DECLARATION #1098

Merged
merged 4 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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