Unused Private Property

ID

kotlin.unused_private_property

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

dead_code, readability

Description

Reports a private property declared inside a class whose name is never read or written from anywhere within the enclosing class body. Such a property stores state that no code ever observes.

Rationale

A private property is, by definition, only reachable from inside its declaring class. When no code reads it, the storage and initialiser cost is wasted, and readers can be misled about the class’s actual state. Removing the property clarifies the class’s true invariants.

// Bad — `unused` is never read
class User(val name: String) {
    private val unused = "x"
    fun greet() = "hello $name"
}

// Good — every private property has at least one read
class User(val name: String) {
    private val suffix = "!"
    fun greet() = "hello $name$suffix"
}

Remediation

Delete the property. If the property is intended to be part of a public or internal API, change its visibility. Properties declared inside a companion object are not inspected by this rule.