unused-answer
A call whose only point is its answer, written as a statement of its own: the answer is thrown away, and the call did nothing else. name.trim(); leaves name as it was; list.concat(more); makes a list nobody keeps; new TextEncoder().encodeInto(text, out); writes what fits and says how much in an answer nobody reads. On by default, as an error.
Why: Rust marks such functions #[must_use] and warns at every call that drops the answer. In TypeScript nothing does: name.trim(); reads as if it trimmed name, compiles, and passes every linter. An agent that writes it ships the untrimmed value, a cut string, a sort that never happened; the tests see the wrong value only if they look at that one. The answer is the call's whole effect, so a call that drops it is never what was meant.
❌ Rejected: trim gives back a trimmed copy, and the copy is dropped
✕ Rejectedunused-answer
ts
export function clean(name: string): string {
name.trim();
return name;
}text
`name.trim()` changes nothing, and this statement throws its answer away: `name` stays as it was
= help: Use the answer: keep it (`const x = name.trim()`) or assign it where it is meant to go;
or remove the statement✅ Accepted: the answer is what the function returns
✓ Accepted0 diagnostics
ts
export function clean(name: string): string {
return name.trim();
}A member that does something and answers how much of it happened is the other kind: the code that drops the answer can't know whether all of it happened.
❌ Rejected: what doesn't fit in out is left out, and only written says so
✕ Rejectedunused-answer
ts
const encoder = new TextEncoder();
export function pack(text: string): Uint8Array {
const out = new Uint8Array(text.length);
encoder.encodeInto(text, out);
return out;
}✅ Accepted: the bytes it wrote are the bytes sent
✓ Accepted0 diagnostics
ts
const encoder = new TextEncoder();
export function pack(text: string): Uint8Array {
const out = new Uint8Array(text.length * 3);
const { written } = encoder.encodeInto(text, out);
return out.subarray(0, written);
}The program's own functions count too, when the whole program shows they change nothing.
❌ Rejected: width changes nothing outside itself
✕ Rejectedunused-answer
ts
function width(xs: readonly number[]): number {
return xs.reduce((a, b) => Math.max(a, b), 0) - xs.reduce((a, b) => Math.min(a, b), 0);
}
export function report(xs: readonly number[]): string {
width(xs);
return `${xs.length} values`;
}✓ Accepted0 diagnostics
ts
function width(xs: readonly number[]): number {
return xs.reduce((a, b) => Math.max(a, b), 0) - xs.reduce((a, b) => Math.min(a, b), 0);
}
export function report(xs: readonly number[]): string {
return `${xs.length} values, ${width(xs)} wide`;
}What counts
- The language's built-ins with no effect but their answer:
answers.onlyinecmascript-api.json, measured on Node, not written by hand (built-ins): every member the map lists is called on sample receivers with sample arguments, and listed when it answered something and changed neither its receiver nor an argument. The strings' and lists' copies (trim,slice,concat,replace,toSorted,with, ...), lookups (get,has,includes,indexOf),Math,Object.keys,String(x), anew Error(...)that isn't thrown or kept. The receiver is found asunhandled-failurefinds it: by how it is written here (a parameter, a loop variable, an element by index, a destructured parameter, a member of a parameter, a field, a#field, a parameter property, a callback's parameter, aMap's entries,?.), or through a declared type, in the whole program; and made in place: a literal or a template (`${a}-${b}`.trim();), an array literal or a spread copy ([...items].slice(1);), a built-in's own answer (text.trim().toUpperCase();), a built-in constructed there (new Date(0).getTime();). - Members whose answer tells what they did:
answers.tellsin the maps. The language'sAtomics.compareExchange(it wrote only if it found the value expected) and the web'sTextEncoder#encodeInto({ read, written }); the Node pack'sreadSyncandreadvSync(how many bytes: the rest of the buffer holds what was there); the browser pack'sconfirmandprompt(what the user answered). - The program's own functions: one that answers (its declared return type isn't
void, or it returns a value), whose body changes nothing outside itself (no write to a binding or object it didn't make, nothrow, noawait, no call into a package or the platform, no built-in with an effect), and every function it calls is such a function too, across files. A function that can fail (a built-in in it can throw) is the failure rules'. Each function is judged once per run.
Not reported:
void f();: dropped on purpose, as for a promise or aResult. No adopter we measured had avoidin front of a call this rule reports (none of 191voidstatements), so it isn't counted as an escape hatch.- A call that can fail where it is (
JSON.parse(text);as a test of the text,n.toFixed(d)withdfrom data): that isunhandled-failure's, and a call made for its failure isn't made for its answer. Where the call can't fail (n.toFixed(2)), it is reported here. - A statement whose receiver or arguments do something themselves:
read().trim();,String(list.pop());,label(parse(text));do whatread,popandparsedo. - A member that runs code it is given, where that code may do something (
list.map(save),list.map((x) => { seen.add(x); return x; })). - A dropped
Result(must-use-result) or promise (floating-promise,floating-rejection). - In the program's functions: one that hands back a parameter as it came (
assertNever(x),(arg: Shape) => arg, called for the type checker), an assertion signature (asserts x is T), a call under// @ts-expect-error, and a call in a test (what a test asserts istest-without-assertion's).
Which gate owns which form
A dropped answer written as an expression that isn't a call is oxlint's no-unused-expressions, which the SAFE-TS preset makes an error: c ? a.trim() : b.trim();, c && a.trim();, text.length;, name;. This rule takes the statement that is a call (name.trim();), where oxlint has nothing to say: a call may do something, and only the data says whether this one does. A call that can fail where it is (JSON.parse(text);, new Date(text).toISOString();) is unhandled-failure's; a dropped Result must-use-result's; a dropped promise floating-promise's and floating-rejection's.
What the rule can't see
- The answer of a member the maps don't measure: the browser's and Node's members with no effect but their answer (
URLSearchParams#get,Headers#get,el.getAttribute(...)) aren't listed; their maps say how members fail, not what they change. - Answers dropped on purpose, measured and left out:
Map#deleteandSet#delete(whether it was there; 196 calls ofdeleteas a statement in one adopter),Reflect.setand its kin (falsewhere the plain write throws: in the packages measured, 58 calls as a statement, all made not to throw),fs.writeSync's count (24 of 35 calls drop it), a stream'swrite(backpressure),navigator.sendBeacon(its own documentation drops it),RegExp#testandexec(they move a global pattern'slastIndex, and a scanner calls them for that). Set#add's answer: it gives back the set, not whether the value was new, so code that counts additions has nothing to read (seen.add(name); added += 1counts a name already there). Compareseen.sizebefore and after, or testseen.has(name)first.- A receiver whose type is only the whole program's to tell and that the model doesn't follow: a union, an inferred type, an optional member of a type with a member the JSON model doesn't take (
h?.name.trim()with aReadonlyMapbesidename). - A dropped answer that isn't a statement: an arrow whose body is the call (
names.forEach((n) => n.trim())), a callback's answer the caller drops. (A?:or&&statement is oxlint's, above.) - A receiver that is a call of the program's code (
label(user).trim();): whetherlabeldoes something is not asked there, so the statement isn't taken for one that does nothing. - A function of the program passed by name (
list.map(width)), a getter's effects, and a function whose effects are through a closure it makes (it is taken to act).