Neighbouring pairs by index
if i + 1 < n and a[i] .. a[i + 1] (or v != V.last!) in a loop over positions: for (x, y) in a.adjacent
let n: 2..=100 = read()
domain V = 1..=n
let a: [V]Int = read()
var cnt = 0
for v in V:
if v != V.last!:
if a[v] < a[v.next!]:
cnt += 1
print(cnt)let n: 2..=100 = read()
domain V = 1..=n
let a: [V]Int = read()
print(count(x < y for (x, y) in a.adjacent))Why
A loop over positions that guards i + 1 < n (or v != V.last!) and then compares a[i] with a[i + 1] is walking the neighbouring pairs of a, with a last-position guard and a + 1 index that are each easy to get wrong by one. for (x, y) in a.adjacent (std.seq) is that walk: every neighbouring pair, left to right, none for a sequence of length 0 or 1. Where the position itself is needed, v.next is the position after v, a ?V that is None at the end. A guard that does not lead to a read of the next position is not reported. There is no automatic fix, because the loop often uses the position as well as the pair, and that has to be read.
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: 2..=100 = read()
let a: [0..n]Int = read()
var cnt = 0
for i in 0..n:
if i + 1 < n:
cnt += 1
if a[i] > 0:
cnt += 1
print(cnt)
// A loop that writes the array it reads at `i + 1` is a DP over neighbours.
var dp: [0..n]Int = read()
for i in 0..n:
if i + 1 < n:
dp[i] min= dp[i + 1] + a[i]
print(dp)Turn it off
For one block, wrap it in with lint(adjacent_by_index: allow):. For a project, add adjacent_by_index = "allow" under [lints] in cp.toml.
Generated from spec/lints/adjacent_by_index, the same record the linter is built from.