← All lints

cross_by_hand

L0556Idiomswarn

Cross product of parallel coordinate lists

xs[i] * ys[j] - xs[j] * ys[i]: points as Pt, p.cross(q), area2

Flagged
let n: 3..=1000 = read()
let xs: [0..n]Int = read()
let ys: [0..n]Int = read()
var twice = 0
for i in 0..n:
    let j = (i + 1) % n
    twice += xs[i] * ys[j] - xs[j] * ys[i]
print(abs(twice))
Write instead
use std.geometry.{area2}
let n: 3..=1000 = read()
let xs: [0..n]Int = read()
let ys: [0..n]Int = read()
let poly = [Pt(xs[i], ys[i]) for i in 0..n]
print(abs(area2(poly)))

Why

Points kept as two parallel lists make every geometric formula spell both coordinates by hand, and xs[i] * ys[j] - xs[j] * ys[i] swaps one index on a bad night. Pt from std.geometry is a point: p.cross(q) is this product, area2(poly) sums it around a polygon (twice the signed area), and locate, hull and the other geometry read the same type. There is no automatic fix, since the points have to be built first.

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.

Near miss
let n: 3..=1000 = read()
let xs: [0..n]Int = read()
let ys: [0..n]Int = read()
for i in 1..n:
    print(xs[i] * ys[i] - xs[i - 1] * ys[i - 1])

Turn it off

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

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