Parameter Shadows Property

ID

kotlin.parameter_shadows_property

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

naming, reliability

Description

Reports a function or method parameter whose name matches an instance property of the enclosing class. The parameter shadows the property inside the function body — bare references read the parameter, not the property, and writes target the parameter.

Rationale

class Counter {
    private val count: Int = 0

    fun bump(count: Int): Int {   // FLAW — shadows the property
        return count + 1          // reads the parameter, not the property
    }
}

Two bindings with the same name in the same lexical scope force readers to work out which one is in scope at every reference. Refactorings that rename one without renaming the other silently change behaviour. The error is easy to make and hard to spot, because the code compiles and almost-always "works" — the parameter happens to carry the value the property would have held.

Remediation

Rename either the parameter or the property so they are distinct:

class Counter {
    private val count: Int = 0

    fun bump(delta: Int): Int {       // OK — clear name
        return count + delta
    }
}

The rule excludes override functions (where the parameter list is fixed by the supertype contract) and setter-shaped functions (setName(value: String)), where the pass-through idiom is conventional.