diff --git a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullable.kt b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullable.kt index 42190e44be98..ebc8bb2d5554 100644 --- a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullable.kt +++ b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullable.kt @@ -12,6 +12,7 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution import io.gitlab.arturbosch.detekt.rules.isNonNullCheck import io.gitlab.arturbosch.detekt.rules.isNullCheck import io.gitlab.arturbosch.detekt.rules.isOpen +import io.gitlab.arturbosch.detekt.rules.isOverride import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor @@ -124,6 +125,10 @@ class CanBeNonNullable(config: Config = Config.empty) : Rule(config) { private val nullableParams = mutableMapOf() override fun visitNamedFunction(function: KtNamedFunction) { + if (function.isOverride()) { + return + } + val candidateDescriptors = mutableSetOf() function.valueParameters.asSequence() .filter { diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt index 50a296cd30f1..1b03c8659128 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt @@ -426,6 +426,21 @@ class CanBeNonNullableSpec : Spek({ """.trimIndent() assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } + + it("does not report on overridden function parameter") { + val code = """ + interface A { + fun foo(a: Int?) + } + + class B : A { + override fun foo(a: Int?) { + val b = a!! + 2 + } + } + """.trimIndent() + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } } context("using a null-safe expression") {