Invalid Range
ID |
kotlin.invalid_range |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Kotlin |
Tags |
correctness, range |
Description
Reports range expressions whose literal bounds produce an empty range:
-
a..bwherea > b— the..operator creates an ascending range; if start exceeds end, no iterations occur. -
a..<bwherea >= b— the exclusive upper bound makes the range empty when start equals or exceeds end.
Rationale
for (i in 10..5) { } // FLAW — empty, loop body never runs
for (i in 5..<5) { } // FLAW — empty (exclusive end equals start)
for (i in 10..<5) { } // FLAW — empty
for (i in 1..10) { } // OK — ascending
for (i in 10 downTo 1) { } // OK — explicit descending
Remediation
-
To iterate in descending order, use
10 downTo 1. -
To iterate up to (excluding)
n, use0..<n. -
Review the bounds to ensure the range covers the intended values.