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

CastToNullableType: allow casting null keyword #4907

Merged
merged 1 commit into from Jun 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -42,6 +42,7 @@ class CastToNullableType(config: Config = Config.empty) : Rule(config) {

val operationReference = expression.operationReference
if (operationReference.getReferencedNameElementType() != KtTokens.AS_KEYWORD) return
if (expression.left.text == KtTokens.NULL_KEYWORD.value) return
val nullableTypeElement = expression.right?.typeElement as? KtNullableType ?: return

val message = "Use the safe cast ('as? ${nullableTypeElement.innerType?.text}')" +
Expand Down
Expand Up @@ -41,4 +41,15 @@ class CastToNullableTypeSpec {
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}

@Test
fun `cast null to nullable type is allowed`() {
val code = """
fun foo(a: Any?) {
val x = null as String?
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
}