Matching Declaration Name
ID |
kotlin.matching_declaration_name |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Naming Convention |
Language |
Kotlin |
Tags |
naming, readability |
Description
Reports a .kt file that contains exactly one top-level class, interface, or
object whose name does not match the file’s base name (the file name without
the .kt extension).
Rationale
The Kotlin coding conventions state that when a file contains a single
non-private top-level class or interface, the file name should match the
declaration name. Aligning the file and the declaration name makes
navigation, packaging, and code review predictable: a reader who sees
UserService.kt expects exactly one top-level UserService.
// File: UserService.kt
class UserService { ... } // OK — names match
// File: UserService.kt
class Helper { ... } // FLAW — declaration does not match file name
Remediation
Either rename the file to match the top-level declaration, or rename the declaration to match the file name. If the file is intentionally a collection of utilities with no single dominant type, add a second top-level declaration (the rule only flags files with exactly one top-level declaration).