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

CanBeNonNullable shouldn't consider abstract properties #4686

Merged
merged 1 commit into from Apr 5, 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 @@ -9,6 +9,7 @@ import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import io.gitlab.arturbosch.detekt.rules.isAbstract
import io.gitlab.arturbosch.detekt.rules.isNonNullCheck
import io.gitlab.arturbosch.detekt.rules.isNullCheck
import io.gitlab.arturbosch.detekt.rules.isOpen
Expand Down Expand Up @@ -474,7 +475,7 @@ class CanBeNonNullable(config: Config = Config.empty) : Rule(config) {
}

private fun KtProperty.isCandidate(): Boolean {
if (isOpen()) return false
if (isOpen() || isAbstract()) return false
val isSetToNonNullable = initializer?.isNullableType() != true &&
getter?.isNullableType() != true &&
delegate?.returnsNullable() != true
Expand Down
Expand Up @@ -377,14 +377,25 @@ class CanBeNonNullableSpec(val env: KotlinCoreEnvironment) {
@Test
fun `does not report open properties`() {
val code = """
abstract class A {
open class A {
open val a: Int? = 5
open var b: Int? = 5
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `does not report abstract properties`() {
val code = """
abstract class A {
abstract val a: Int?
abstract var b: Int?
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `does not report properties whose initial assignment derives from unsafe non-Java code`() {
val code = """
Expand Down