Skip to content

Commit

Permalink
CanBeNonNullable shouldn't consider abstract properties (#4686)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Apr 5, 2022
1 parent 0be7775 commit 66e32e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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

0 comments on commit 66e32e5

Please sign in to comment.