← All lints

unproven_list_index

L0414Pedanticoff by default

Unproven List index in nested loop

a List index inside nested loops that is checked on every access

Flagged
let a: List<Int> = [1, 2, 3]
var s = 0
for i in 0..3:
    for j in 0..3:
        s += a[j]
print(s)
Write instead
domain D = 0..3
let a: [D]Int = [1, 2, 3]
var s = 0
for _ in D:
    for j in D:
        s += a[j]
print(s)

Why

Indexing a List<T> with an arbitrary Int inside nested loops emits a runtime bounds check on every iteration. Declare a domain and [D]T array so bounds are verified once. Allowed by default (pedantic): the check is cheap and the warning was the most frequent on C++-shaped code; turn it on with unproven_list_index = "warn" under [lints] in cp.toml.

The practice it serves

Stricter checks, off by default. Family: D. Domains, positions and cuts.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
let a: List<Int> = [1, 2, 3]
var s = 0
for j in 0..a.len:
    s += a[j]
print(s)
var d = 0
for k in 0..2:
    for j in 1..a.len:
        d += k * a[j]
print(d)

Turn it off

For one block, wrap it in with lint(unproven_list_index: allow):. For a project, add unproven_list_index = "allow" under [lints] in cp.toml.

Generated from spec/lints/unproven_list_index, the same record the linter is built from.