A loop runs from 0 up to and including the size, one past the last index
What it finds
for (i in 0..items.size) includes items.size, which is not an index, so items[i] throws on the last round. items.indices, 0 until items.size or 0..<items.size stop at the last index. Reported for loops whose range ends at .size, .length or .count().
From the rule's source, range_past_last_index.rs.
fun print(items: List<String>, name: String) { for (i in 0..items.size) println(items[i])tx0 reports: 0..items.size includes items.size for (i in 0..name.length) println(name[i])tx0 reports: includes name.length for (i in 0..items.size - 1) println(items[i]) for (i in 0 until items.size) println(items[i]) for (i in items.indices) println(items[i])} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:loop-past-last-index" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:loop-past-last-index] reason="..."tx0 explain tx0/kotlin:loop-past-last-index shows the same in the terminal. Found a false positive? Report it.