sparse-array-write
A write that may land past an array's end, and so leaves holes: slots[n] = 1 into an array filled by position, values[values.length + 1] = 0, delete values[i]. On by default, as an error.
Why: writing index 5 of an empty array makes its length 6 and leaves 0 to 4 as holes. A hole reads as undefined while the array's type says number[]: for (const v of slots) hands out undefined as a number, slots.map(...) skips it, and slots[i]! under i < slots.length reads it as a number. That last one is a guarantee SAFE-TS makes: no-unchecked-cast accepts a[i]! where the index is proved below the length, on the assumption that every position below the length holds a value. Rust has no holes: vec[5] = x on a shorter vector panics, and a table with gaps is a HashMap. Guide: Loops.
✕ Rejectedsparse-array-write
ts
export function namesByCode(codes: readonly number[]): readonly string[] {
const names: string[] = [];
for (const code of codes) names[code] = `#${code}`;
return names;
}✅ Accepted: a table with gaps is a Map, and a lookup an Option
✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";
export function namesByCode(codes: readonly number[]): ReadonlyMap<number, string> {
const names = new Map<number, string>();
for (const code of codes) names.set(code, `#${code}`);
return names;
}
export function nameOf(names: ReadonlyMap<number, string>, code: number): Option<string> {
return names.get(code);
}✅ Also accepted: appended, written inside, or full from the start
✓ Accepted0 diagnostics
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
export function doubled(source: readonly number[]): number[] {
const out: number[] = [];
for (let i = 0; i < source.length; i++) out[i] = (source[i] ?? 0) * 2;
return out;
}
export function setAt(values: Mut<number[]>, at: number, value: number): void {
if (at >= 0 && at < values.length) values[at] = value;
}
export function counts(kinds: readonly number[], size: number): number[] {
const out = Array.from({ length: size }, () => 0);
for (const kind of kinds) if (kind >= 0 && kind < out.length) out[kind] = (out[kind] ?? 0) + 1;
return out;
}
export function keepPositive(values: Mut<number[]>): void {
let kept = 0;
for (let i = 0; i < values.length; i++) {
const v = values[i]!;
if (v > 0) values[kept++] = v;
}
values.length = kept;
}What is reported: an index write (a[i] = v, a[i] += v, a[i]++, a[i] ??= v) or a length write (a.length = n) that the checker can't show is at most a.length, into an array made empty in the file (const a: T[] = [], a field initialized with []) that nothing pushes to (an array filled by position: a table); a write at a.length + k (k >= 1) into any array; and delete a[i] on an array. An array is one by its declared type (T[], Array<T>, a tuple, Mut<T[]>) or how it is made ([], new Array(n), Array.from, split, Object.keys, map and filter of an array).
What shows a write isn't past the end: an index proved at most a.length by the checks no-unchecked-cast reads (a loop over a, if (i < a.length), a[a.length] = v, a search result of a, Math.min(a.length, n) for a length), the loop that fills an array made empty just before it (out[i] = … as a statement of for (let i = 0; …; i++), of for (const [i, x] of items.entries()), or of a callback items.forEach((x, i) => …) (also map, filter, ...), whose index counts up from 0 one per element; with nothing else changing out until the fill is over, and no continue (in a callback, return) before the write), and compaction (a[kept++] = x in a loop over a, kept starting at 0 and growing only there, by one per element at most; a.length = kept after it).
Not reported: a typed array (Uint8Array, Float64Array, ...: a write outside it is ignored, nothing is added), an array whose elements may be undefined by its type ((string | undefined)[], Option<T>[]: a hole is one of the values it declares), and an index write into an array that is pushed to or made with its elements ([0, 0, 0, 0]): there a write the checker can't follow is almost always an update inside it (a heap's sift, a swap, a count per kind).
Those writes are still taken into account: an array that has a write the checker can't show is inside it, anywhere in the file, gets no a[i]! proved, whatever its kind (unless it is a typed array). no-unchecked-cast then says which write:
✕ Rejectedsparse-array-writeno-unchecked-cast
ts
export function total(n: number): number {
const slots: number[] = [];
slots[n] = 1;
let sum = 0;
for (let k = 0; k < slots.length; k++) sum += slots[k]!;
return sum;
}What the rule can't see:
- A write in another file: an array made here and written past its end by a function of another file, or one passed in that a caller filled with holes. The rule reports the writes it reaches in each file, so a project that passes it has no such write anywhere, unless the write is into an array it doesn't report (pushed to, made with elements, of unknown kind) in a file that doesn't read it by
a[i]!. - An array under another name:
const view = this.cellsis followed; an array stored in an object or aletand written through that is not. - The kind of an array it can't see: a field of a type declared elsewhere (
state.cells[i] = v) isn't reported, and gets noa[i]!proved in the file after such a write, even if it is a typed array (getChannelDataandsubarrayare known to give one). - Writes inside, by reasons it can't follow: an index from a heap's arithmetic, a counter advanced outside a loop, a fixed-size table written at a domain value. Not reported (above), but an
a[i]!of such an array in the same file isn't proved: read it with?? fallback.
Related: no-unchecked-cast; not-found-index (a[a.indexOf(x)] = v writes "-1"); guide: Loops.