← All lints

hand_rolled_sieve

L0518Idiomswarn

Hand-rolled prime or SPF sieve

manual Eratosthenes or linear sieve loop: use std.math.Sieve

Flagged
let n = 20
var is_prime = List.filled(n + 1, true)
for i in 2..=n:
    if is_prime[i]:
        var j = i * i
        while j <= n:
            is_prime[j] = false
            j += i
print(is_prime[7])
Write instead
let sv = Sieve.new(20)
print(yes(sv.is_prime(7)))

Why

Hand-rolling a prime or smallest-prime-factor sieve (j = i * i; while j <= n: is_prime[j] = false; j += i or linear spf[i * p] = p) duplicates Sieve and sieve_multiplicative in std.math.

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: S.math.

Not flagged

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

Near miss
let n = 20
var odd = List.filled(n + 1, false)
for i in 0..=n:
    odd[i] = i % 2 == 1
print(yes(odd[7]))

Turn it off

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

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