Key tested, then indexed again
if k in m: then m[k]: if let v = m.get(k):
let n: Int = read()
let a = read<[n]Int>()
var first = Map<Int, Int>()
var best = -1
for i in 0..n:
let x = a[i]
if x in first:
best max= i - first[x]
else:
first[x] = i
print(best)
fn twice(m: Map<Int, Int>, k: Int) -> Int:
if k not in m: return 0
m[k] + m[k]
print(twice(first, 0))let n: Int = read()
let a = read<[n]Int>()
var first = Map<Int, Int>()
var best = -1
for i in 0..n:
let x = a[i]
if let v = first.get(x):
best max= i - v
else:
first[x] = i
print(best)
fn twice(m: Map<Int, Int>, k: Int) -> Int:
let v = m.get(k) else: return 0
v + v
print(twice(first, 0))Why
C++ asks a map twice: if (m.count(k)) use(m[k]);, because operator[] inserts and find returns an iterator. In CP Lang m.get(k) returns ?V, the value or None, so the test and the read are one question. if k in m: followed by m[k] asks it twice: two lookups, and an m[k] that traps when an edit moves it out from under its test. if let v = m.get(k): asks once and binds the value the branch reads; an else: or elif stays as it was.
The lint fires on a Map or SortedMap declared in the file, when the branch reads m[k] with the very key it tested. It stays quiet when the branch writes to m or k, calls a method on m or on m[k] (the value may change in place), calls a function of the file whose body names m (it may change m), or binds m or k anew: there the bound value could differ from what each m[k] reads. When the branch starts with let x = m[k], the fix binds x directly and deletes that line.
The guard form is the same question asked twice: if k not in m: return (or break, continue) and then m[k] below it becomes let v = m.get(k) else: return, with the same quiet cases for the statements after the guard.
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: Int = read()
let a = read<[n]Int>()
var last = Map<Int, Int>()
var best = -1
for i in 0..n:
let x = a[i]
if x in last:
best max= i - last[x]
last[x] = i
else:
last[x] = i
print(best)Turn it off
For one block, wrap it in with lint(map_contains_then_index: allow):. For a project, add map_contains_then_index = "allow" under [lints] in cp.toml.
Generated from spec/lints/map_contains_then_index, the same record the linter is built from.