not-found-index
The result of a search, -1 when nothing matches, used as a position before anything checks it: indexOf, lastIndexOf, findIndex, findLastIndex and search, passed to splice, slice, substring, at, with and the like, or written to as an index. On by default, as a warning.
Why: Rust's position returns an Option; JavaScript answers "not found" with -1, a number like any other, and every method that takes a position does something with it. A negative start counts from the end, so items.splice(items.indexOf(item), 1) removes the last element when item isn't there, text.slice(0, text.indexOf(mark)) drops the last character, and items.at(-1) is the last element. As an index, items[-1] = value adds a property named "-1" and leaves the array as it was. Nothing fails: the code goes on with the wrong element. Guide: Errors.
✕ Rejectednot-found-index
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
export function removeTag(tags: Mut<string[]>, tag: string): void {
tags.splice(tags.indexOf(tag), 1);
}
export function keyOf(line: string): string {
return line.slice(0, line.indexOf("="));
}✅ Accepted: "not there" as an Option, and as an error the caller sees
✓ Accepted0 diagnostics
ts
import { indexIn } from "@evobug_com/safe-ts-std/array";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import type { Mut } from "@evobug_com/safe-ts-std/mut";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export function removeTag(tags: Mut<string[]>, tag: string): boolean {
const at = indexIn(tags, tag);
if (at === undefined) return false;
tags.splice(at, 1);
return true;
}
export type LineError = Enum<{ NoSeparator: { line: string } }>;
export const LineError = constructors<LineError>();
export function keyOf(line: string): Result<string, LineError> {
const at = line.indexOf("=");
if (at === -1) return err(LineError.NoSeparator({ line }));
return line.slice(0, at);
}ts
import { indexIn } from "@evobug_com/safe-ts-std/array";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import type { Mut } from "@evobug_com/safe-ts-std/mut";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export function removeTag(tags: Mut<string[]>, tag: string): boolean {
const at = indexIn(tags, tag);
if (at === undefined) return false;
tags.splice(at, 1);
return true;
}
export type LineError = Enum<{ NoSeparator: { line: string } }>;
export const LineError = constructors<LineError>();
export function keyOf(line: string): Result<string, LineError> {
const at = line.indexOf("=");
if (at === -1) return err(({ tag: "NoSeparator", line } satisfies LineError as LineError));
return line.slice(0, at);
}indexIn(items, value) from @evobug_com/safe-ts-std/array is indexOf that returns undefined instead of -1 (for a tuple, typed as one of its indexes). For a string, a findIndex or a search, keep the result and compare it with -1 before the use.
✅ Also accepted: checked before the use
✓ Accepted0 diagnostics
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
export function removeDone(tasks: Mut<{ done: boolean }[]>): void {
const at = tasks.findIndex((task) => task.done);
if (at < 0) return;
tasks.splice(at, 1);
}
export function before(text: string, mark: string): string {
const at = text.indexOf(mark);
return at === -1 ? text : text.slice(0, at);
}A check is a comparison of the result (or of the variable holding it, also at + 1) with -1 (at === -1, at !== -1, -1 < at), or with any integer by <, <=, >, >= (at < 0, at >= 0). In a test, an assertion on it counts too (expect(at).toBeGreaterThan(-1), assert.ok(at === 0 || at === 1)): the test stops when it fails. Uses and checks are taken in the order they are written (unlike number-parse-nan, which follows paths; this rule hasn't moved to the flow layer yet).
What counts as a use: an argument of splice and toSpliced (the start), slice, substring, subarray (both), substr, at, with, charAt, charCodeAt, codePointAt; an index written to (items[at] = v, items[at]++); and an index read with a positive offset (items[at + 1]: the first element instead of nothing). The result plus or minus an integer counts as the result. A plain read items[at] isn't reported: for -1 it is undefined, which noUncheckedIndexedAccess already makes the code handle.
Test files aren't checked (a file named *.test.* / *.spec.*, or importing a test runner; a helper module under tests/ is). There the list and the element are the test's own: on an adopter's project, 1 of 23 findings in tests was a real bug, and a wrong index there fails the test's own assertions.
✅ Accepted: the idioms that are right because of the -1
✓ Accepted0 diagnostics
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
// -1 + 1 is 0: everything after the last slash, or all of it.
export function fileName(path: string): string {
return path.slice(path.lastIndexOf("/") + 1);
}
// Insert after the last smaller value, or at the start.
export function insertSorted(values: Mut<number[]>, value: number): void {
values.splice(values.findLastIndex((v) => v <= value) + 1, 0, value);
}
const LEVELS = ["low", "mid", "high"] as const;
type Level = (typeof LEVELS)[number];
// `level` is one of LEVELS, so it is found.
export function atLeast(level: Level): readonly Level[] {
return LEVELS.slice(LEVELS.indexOf(level));
}
export function removeEmpty(lines: Mut<string[]>): void {
for (const line of Array.from(lines)) {
if (line === "") lines.splice(lines.indexOf(line), 1);
}
}Not reported: the result plus 1 as an argument of slice, substring, substr, subarray, splice and toSpliced (from the start when nothing matches, which is what these idioms mean); and an answer that is found, by what the function itself shows about the element:
- it was put there:
items.push(item)oritems.includes(item)earlier in the same function; - it was taken out of the list:
const page = pages.find(...)(orfindLast,at,pages[i]), which TypeScript makes the code narrow fromundefinedbeforepages.indexOf(page); - the loop is over the list:
itemis the element of afor...ofoveritemsor over a copy or part of it (Array.from(items),[...items],items.slice(...),items.filter(...),items.toSorted(...), also through aconstholding one), or the first parameter of anitems.forEach(...)callback; - the list was built from it:
const pair = [first, second]; pair.indexOf(second), a literal that is an element of aconstarray literal of the file, or a value declared(typeof LIST)[number](directly or through a type alias of the file). A method of the project's own type isn't the built-in: a receiver declared with a class, interface or type alias of the project that declaresindexOf(orsearch, ...) is left alone, as inunbounded-spread.
What the rule can't see:
- An element known to be there for a reason elsewhere:
board.cards.splice(board.cards.indexOf(card), 1), wherecardcame fromboard.cardsin a caller or a helper (findCard(board, id)). If it was removed in between, the call removes another card. Check the result (if (at === -1) return) or keep the element's index where it was found. On an adopter's project these were 9 of the 18 findings in its source, none of them a bug today. All 7 real ones were searches in text (line.indexOf("=")of a line read from a file), which were 9 of the 18. - Order, not paths: a check in one branch counts for the code after the branch too, and a result passed to a function isn't a use (the function may check it).
- A search through another name: a result stored in an object or an array, a search method passed as a callback (
names.map(list.indexOf, list)), or a receiver whose type isn't written down (a project type with its ownindexOfis then taken for the built-in).
Related: number-parse-nan, the same "answer that isn't one" for parsed numbers; divide-by-length; no-unchecked-cast.