← All lints

repeated_literal

L0506Idiomswarn

Repeated large literal

a big literal (a bound or modulus) written in several places

Flagged
let a = [3, 4]
print(a.sum() % 998244353)
print(a[0] * a[1] % 998244353)
Write instead
const P = 998244353
let a = [3, 4]
print(a.sum() % P)
print(a[0] * a[1] % P)

Why

A big literal (a bound, a modulus, an infinity) is written on several lines: facts that must agree but are kept apart. Equal bounds in the types of different read() inputs are separate facts and do not count. Name it once (const P = 998244353, a domain, a type alias) and use the name.

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: K. Arithmetic, overflow, modulus and floats.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
let a = [3, 4]
print(a.sum() % 998244353)
print(a[0] * a[1])
let (n, k) = read<(1..=200_000, 0..=1_000_000)>()
let h = read<[n]0..=1_000_000>()
enum Op:
    Set(d: -1_000_000_000..=1_000_000_000)
    Ask(h: 0..=1_000_000_000)

@test
fn the_modulus_is_an_odd_prime():
    assert 998244353 % 2 == 1
    assert 998244353 > 1_000

Turn it off

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

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