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 4, 2022
1 parent e052dbe commit 85a6e3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Expand Up @@ -10,6 +10,8 @@ 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.isLateinit
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 +198,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 @@ -283,6 +283,20 @@ class VarCouldBeValSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("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()
}

context("anonymous objects that escape") {
it("does not report when an object initializes a variable directly") {
val code = """
Expand Down

0 comments on commit 85a6e3d

Please sign in to comment.