Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VarCouldBeVal] Override vars will not be flagged if bindingContext is not set #4477

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
}
}
}
})