Skip to content

Commit

Permalink
[VarCouldBeVal] Override vars will not be flagged if bindingContext i…
Browse files Browse the repository at this point in the history
…s not set (#4477)
  • Loading branch information
severn-everett committed Jan 28, 2022
1 parent d0732ab commit 8a39d2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Expand Up @@ -69,6 +69,7 @@ class VarCouldBeVal(config: Config = Config.empty) : Rule(config) {

override fun visitKtFile(file: KtFile) {
super.visitKtFile(file)
if (bindingContext == BindingContext.EMPTY) return
val assignmentVisitor = AssignmentVisitor(bindingContext)
file.accept(assignmentVisitor)

Expand Down
@@ -1,6 +1,7 @@
package io.gitlab.arturbosch.detekt.rules.style

import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
Expand Down Expand Up @@ -410,6 +411,21 @@ class VarCouldBeValSpec : Spek({
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("does not report when an object initializes a variable directly - without type solving") {
val code = """
interface I {
var optionEnabled: Boolean
}
fun test(): I {
val o = object: I {
override var optionEnabled: Boolean = false
}
return o
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
}
})

0 comments on commit 8a39d2a

Please sign in to comment.