Skip to content

Commit

Permalink
[VarCouldBeVal] fix overrides false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
meriouma committed Apr 5, 2022
1 parent 0be7775 commit b65dbd5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Expand Up @@ -10,6 +10,7 @@ import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import io.gitlab.arturbosch.detekt.rules.isOverride
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
Expand Down Expand Up @@ -196,7 +197,7 @@ class VarCouldBeVal(config: Config = Config.empty) : Rule(config) {

private fun KtProperty.isDeclarationCandidate(): Boolean {
return when {
!isVar -> false
!isVar || isOverride() -> false
isLocal || isPrivate() -> true
else -> {
// Check for whether property belongs to an anonymous object
Expand Down
Expand Up @@ -289,6 +289,21 @@ class VarCouldBeValSpec(val env: KotlinCoreEnvironment) {
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `should not report when a property overrides a var`() {
val code = """
interface I {
var optionEnabled: Boolean
}
class Test {
val test = object : I {
override var optionEnabled: Boolean = false
}
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `should not report assigned properties that have accessors that are accessed`() {
val code = """
Expand Down

0 comments on commit b65dbd5

Please sign in to comment.