← All lints

split_range_check

L0551Idiomswarn

Range check written as two comparisons

x >= 0 and x < n: chain it as 0 <= x < n

Flagged
let (x, n): (Int, 1..=100) = read()
if x >= 0 and x < n:
    print("in")
Write instead
let (x, n): (Int, 1..=100) = read()
if 0 <= x < n:
    print("in")

Why

A range check written as two comparisons joined by and names its variable twice and splits the range. CP Lang chains comparisons as Python does, each operand evaluated once: 0 <= x < n reads as the range it checks. For a grid cell checked in both coordinates, give the coordinates their domains instead: p.adj4 from std.grid yields only the neighbours inside the grid. There is no automatic fix yet.

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 (x, n): (Int, 1..=100) = read()
if x >= 0 and n > 5:
    print("in")

Turn it off

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

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