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] fix overrides false positives #4664

Merged
merged 1 commit into from Apr 6, 2022
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 @@ -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