Kotlin

Kotlin

Made by DeepSource

Unreachable code can be safely removed KT-W1065

Anti-pattern
Major
Autofix

Unreachable code should be removed, as it serves no purpose.

Such code is typically a result of incorrect or unintended control flow in code. It is thus important to correctly structure your code and ensure that all code-paths are reachable and can be executed.

The compiler can often detect and warn about unreachable code, helping you identify and fix such issues during development. Removing unreachable code improves code clarity, maintains program correctness, and prevents potential runtime errors.

Bad Practice

for (i in 1..2) {
    doStuff(i)
    break
    println() // unreachable
}

Recommended

Remove unreachable code.

for (i in 1..2) {
    doStuff(i)
}