Empty Finally Block
ID |
kotlin.empty_finally_block |
Severity |
low |
Remediation Complexity |
auto_fix |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Kotlin |
Tags |
CWE:1071, empty, reliability |
Description
Reports finally blocks that contain no statements and therefore have no effect.
A finally block is intended to run cleanup code that must execute regardless of whether an
exception was thrown. An empty finally provides no such guarantee and adds only syntactic
noise. Such blocks typically result from an incomplete implementation or from clean-up code
that was removed without removing the surrounding finally.
Rationale
// Bad — finally block has no effect
try {
riskyOperation()
} finally { } // FLAW
// OK — finally performs cleanup
try {
riskyOperation()
} finally {
cleanup()
}
// OK — finally closes a resource
try {
stream.write(data)
} finally {
stream.close()
}