No Unit Return
ID |
kotlin.no_unit_return |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Kotlin |
Tags |
best-practice, code-style |
Description
Reports function declarations that explicitly annotate the return type as
: Unit. Since Unit is Kotlin’s default return type when no annotation is
present, the explicit annotation is redundant noise.
Rationale
Kotlin functions with no meaningful return value implicitly return Unit. Adding
: Unit adds visual clutter without providing any additional information to the
reader or the compiler.
// Bad
fun save(): Unit { repository.save(entity) }
fun onDestroy(): Unit { cleanup() }
// Good
fun save() { repository.save(entity) }
fun onDestroy() { cleanup() }
Remediation
Remove the : Unit return type annotation. Most IDEs offer a one-click
inspection fix. If the annotation is present for explicit API documentation
purposes, consider adding a KDoc comment to communicate intent instead.