decided-test
A test of a place (worker.task !== undefined, if (job.done), lamp.mode === Mode.ON) while the place holds a literal the function itself just gave it, with nothing between that could change it: the test is always false (what it guards never runs) or always true. On by default, as an error. The finding says which line set the value.
Why: the code clears a state, then asks it what to do. stop(mut(worker)) sets worker.task to undefined; the next statement asks whether it is set, to give back the slot the task held. It never is, so the slot is never given back. The test reads as if it decided something, and it doesn't: the value it meant to see is gone. unreachable-code reports a branch a closed type decides; this rule reports one a value decides.
✅ Accepted: what the test is about is read before it is cleared
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
interface Worker {
task: Option<string>;
}
function stop(worker: Mut<Worker>): void {
worker.task = undefined;
}
export function finish(worker: Mut<Worker>, taken: Mut<Set<string>>): void {
const task = worker.task;
stop(mut(worker));
if (task !== undefined) taken.delete(task);
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
interface Worker {
task: Option<string>;
}
function stop(worker: Mut<Worker>): void {
worker.task = undefined;
}
export function finish(worker: Mut<Worker>, taken: Mut<Set<string>>): void {
const task = worker.task;
stop(worker);
if (task !== undefined) taken.delete(task);
}❌ Rejected: stop always leaves worker.task undefined, so the slot is never given back
✕ Rejecteddecided-test
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
interface Worker {
task: Option<string>;
}
function stop(worker: Mut<Worker>): void {
worker.task = undefined;
}
export function finish(worker: Mut<Worker>, taken: Mut<Set<string>>): void {
stop(mut(worker));
if (worker.task !== undefined) taken.delete(worker.task);
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
interface Worker {
task: Option<string>;
}
function stop(worker: Mut<Worker>): void {
worker.task = undefined;
}
export function finish(worker: Mut<Worker>, taken: Mut<Set<string>>): void {
stop(worker);
if (worker.task !== undefined) taken.delete(worker.task);
}❌ Rejected: a defensive test right after the write is always true
✕ Rejecteddecided-test
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
interface Job {
done: boolean;
}
export function markDone(job: Mut<Job>, log: (text: string) => void): void {
job.done = true;
if (job.done) log("done");
}Drop it, or test what was meant.
What counts
- A place: a root (a parameter,
this, a binding) and the property names written after it, as the flow layer has them (a["b"]isa.b). An element (a[i]) or a call's result isn't one. - A literal:
undefined,null,true,false, a number, a string, or a constant whose value the program shows (const NONE = -1, a member of aconstobject or anenum, also imported:Mode.OFF). - Given it by an assignment in the function, or by a call whose callee always leaves that literal there: an assignment of the callee's on every path that completes, with nothing else in it writing the place (also a parameter it sets from:
setMode(lamp, Mode.OFF)whensetModedoeslamp.mode = mode). Which functions do is a summary the whole-program pass works out once per run; it describes the callee's own changes, and a call the callee makes may change anything it is given. - Nothing between can change it: no write of the place or of what holds it; no call given the place's object (or what holds it) whose callee may change that path; for
this, module state or a local a callback captures, no call at all; noawaitoryield(other code runs meanwhile, and may hold the same object as a parameter); and no join where the paths disagree (if (a) x.v = 1; else x.v = 2;decides nothing). - Every form of call, and what it may change of what it is given:
- a function of the program with a body: what its summary says (the loan rules: in a checked file, only a parameter it declares
Mut<...>, and what it calls with it); - a function with no body here (
declare function, an abstract method, a package's types): anything; - a method, through
this, a receiver of a declared type orsuper.m(): its summary only where no override can run instead (the class is not exported, not extended in its file, not abstract); otherwise anything; a static method is the one its class names; - a getter or a setter (
x.value,x.value = v) the receiver resolution ties the member to: a call of it on the object; a member that is a field runs nothing; - a destructuring (
const { value } = x): a call given the object (each property it reads may run a getter, which nothing resolves); - a tagged template (
tag`${x}`),new C(x),f?.(x),o[k](x),f.call(t, x),f.apply(t, xs),f.bind(t): calls given what they are given (a tag or a constructor of the program by its summary, the rest anything); - a built-in: nothing where the file shows it is one that only answers on what it is called on (
ids.includes(id)withids: string[],Math.max(a, b)); a method by the name of one that changes its receiver (push,add) on a value the pass can't name, anything it is given; - a callback written in the function (
items.forEach((i) => ...)) holds what it uses: anything of it may change.
- a function of the program with a body: what its summary says (the loan rules: in a checked file, only a parameter it declares
- A test: a branch on the place's truthiness, or a comparison with a literal (
===,!==, and==/!=withnullorundefined), in anif, a loop,?:,&&or||.
What the rule can't see
- A test in another function: a callee that reads the place to decide what to release, called after the caller already cleared it (the caller switches a lamp off, then calls the function that ends its mode, which reads the mode to know what to release). Reads of the same state that are meant to see it cleared are far more common there (measured on an adopter's code: 1 in 11 such calls was a bug), and the code doesn't tell them apart, so the rule doesn't guess.
- Values other than literals, and what a value is computed from: this isn't constant propagation.
- A literal given below the callee:
stopcallingclear, which sets the place. A call the callee makes may change anything it is given, so nothing is known (this keeps the summaries cheap, and no case the fixtures, the corpus or an adopter's code show needs deeper). - State across an
await: anything read after one may have been changed by another task; that isstale-across-await's question, not this rule's. - A getter through an interface: a member read whose receiver's type is an interface the receiver resolution can't tie to a class is taken as a field (a getter that changes its object is
mutating-getter's error). - Aliases: a local bound to a part of a parameter (
const dial = board.dial) is shared, and a call kills what is known of it; a second reference to the same object the analysis can't see (a parameter that is also in a listthisholds) is taken not to be changed by a call it isn't given, as the loan rules assume.