last element by len - 1
xs[xs.len - 1]: xs.last! is the last element
let n: 1..=100 = read()
var xs: List<Int> = [n]
xs.push(n * 2)
print(xs[xs.len - 1])let n: 1..=100 = read()
var xs: List<Int> = [n]
xs.push(n * 2)
print(xs.last!)Why
C++ reads the last element as v[v.size() - 1] or v.back(). CP Lang's .last is the last element as an optional, None on an empty list, and .last(k) is the one k before it; xs.last! reads it and traps on an empty list exactly as the index would. The fix applies to reads; a write xs[xs.len - 1] = v keeps the index.
The practice it serves
A hand-written pattern the language or the standard library already says in one step.
Not flagged
A near miss: close to the pattern, but not it.
let n: 1..=100 = read()
var xs: List<Int> = [n]
xs.push(n * 2)
xs[xs.len - 1] = 0
print(xs[xs.len - 2])Turn it off
For one block, wrap it in with lint(last_via_len: allow):. For a project, add last_via_len = "allow" under [lints] in cp.toml.
Generated from spec/lints/last_via_len, the same record the linter is built from.