Local Shadows Property

ID

kotlin.local_shadows_property

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

naming, shadowing

Description

Reports a local val or var declared inside a method whose name matches a property declared in the enclosing class (or any enclosing class). The local declaration shadows the class member.

Rationale

When two variables with the same name share a lexical scope, every read and every write must be carefully attributed to one or the other. The unqualified name resolves to the innermost binding (the local), so writes to the local silently bypass the property; reads can also differ between bare references (the local) and qualified references (this.name). This is an established source of subtle bugs, especially after refactorings that rename either binding without renaming the other.

class Counter {
    private var count = 0

    fun bump() {
        val count = 5      // FLAW — shadows the property
        println(count)     // reads the local
    }

    fun read() {
        val tally = count  // OK — different name
    }
}

Remediation

Rename the local variable so its purpose is clear and its name is distinct from any enclosing class property. If the local is intentionally the new value for the property, assign to the property directly instead of binding a new local.