How the checker follows paths
More than a dozen SAFE-TS rules reason about execution order along control-flow paths—whether a parsed number is guarded with Number.isNaN (number-parse-nan), a buffer is touched after transfer() (use-after-consume), a local Result is checked (must-use-result), shared state goes stale across an await (stale-across-await), a one-shot listener attaches too late (late-listener), a task writes without checking its signal (superseded-task-write), a new Promise never settles (promise-never-settles), a resource leaks (unreleased-resource), an update skips its undo (unbalanced-update), a test is already decided (decided-test), a test runs no assertion (test-without-assertion), code is unreachable (unreachable-code), or two mut(x) loans overlap (aliased-mut). Every one of them shares a single control-flow model so branches, loops, and finally blocks behave identically across rules.
In short
- Branch isolation & loop back-edges: sibling
if/elsebranches are independent, while the next loop iteration follows the previous iteration's end. - Exits & cleanup:
return,throw,break, andcontinue(including labels) terminate paths immediately;finallyruns on every exit out oftry. - Conservative
try/catch: any statement insidetrymay jump tocatch. - Short-circuiting:
&&,||,??,?:, and?.model conditional evaluation accurately. - Separate callbacks: nested callbacks are analyzed as independent functions, neither strictly before nor after surrounding statements.
What is followed
Each function body is a graph of the code that runs in order, built only for a function a rule asks about:
if/else,?:,switch(acasefalls through to the next until abreak),&&,||,??and their assignments (a ??= brunsbonly whenais nullish), optional chaining (res?.text()reads nothing whenresis nullish). In a test,if (a || b || Number.isNaN(n)) return;leaves one path going on, the one where every part ran.- Every loop, with its way back to the top: a use at the start of the body comes after what the end of the body did on the round before.
break,continueand their labelled forms go where they say. returnandthrowend the path. Afinallyis run on each way out of itstry: falling out of the block or thecatch, areturn, abreakorcontinuethat crosses it, an exception. Each way goes on to where it was going, so areturninside atrydoesn't continue after thetrybecause afinallyran.- In a
try, any point may throw, so thecatch(or thefinallyrun for an exception) is reached with whatever happened up to there. A rule that says what throws (anawait, or every call) says it inside atrytoo: an exception from atryinside another runs the innerfinallywhole before the outer one. awaitandyieldare marked: the function stops there and other code runs before it goes on. What was read before is stale after it (stale-across-await).- An exception outside a
tryleaves the function. Athrowalways says so; for a rule that asks about every way out (unreleased-resource), anawaitoutside atrymay reject too, a call the rule names may throw (Calls::throwing: the calls whose callee the whole-program pass is then asked about; or every call, for a rule that wants that), and std'sq(...)returns the error early, the way the transform writes it (a call the rule names as std'sqby its import,Calls::q, whatever the file calls it): afinallyruns on each of these ways out as well. - A function handed to a call runs some other time, and the call says when: once, later (
setTimeout,queueMicrotask,.then,once,addEventListener(..., { once: true })), any number of times, later (setInterval,addEventListener,on), or the checker doesn't know.promise-never-settlesasks a callback that runs once the same question as the executor around it.
Not followed: code a getter or a callback runs in the middle of an expression, and the order of a class's static initializers.
Where a rule looks
A rule says what each piece of code does to what it tracks ("this parses n", "this checks n", "this transfers buf") and whether it wants to know if a thing holds on some path (number-parse-nan, use-after-consume: a bug on one path is a bug) or on every path (what use-after-consume uses to say "on one path to here"). Facts about a binding end where it gets a new value.
Places
What a rule tracks beyond a function's own locals is a place: a root (this, or a binding) and the property names written after it: this.total, this.#cache, cache.entries, visits. a["b"] and a.b are one place, a.b.c is part of a.b; an element (a[i]) or a call's result (f().x) isn't a place. stale-across-await asks, for each place a function writes, whether it (or a part of it) was read before the last await.
The analyses on it
Each rule says what it tracks, how paths combine, and which of the layer's options it turns on (Options in crates/safe_ts_core/src/flow.rs; none is on by default).
| Rule | What a fact is | Paths | Options and hooks |
|---|---|---|---|
number-parse-nan | a parsed number not checked yet | some path | none |
use-after-consume | an operation ran on this value | some path (the finding), every path ("on one path to here" or not) | none |
must-use-result | a kept Result not looked at yet | some path | an exception edge clears its facts |
stale-across-await | per written place: read since the last await; stale; tested by a guard clause; a guard held | some path | Transfer::suspend makes what was read stale (unless a guard is held); the branch hook marks a guard clause's test; places |
late-listener | per receiver: had, then time passed, not tested since; per receiver and call waited on with it: late only through those (watched); a local receiver bound | some path | Transfer::suspend makes what the function had late (or watched, when the await was a call given it); a callback callback_runs says runs later starts late for what it captured; the whole-program pass keeps a finding when the packs say the event happens once on the receiver's class, and drops a watched one when the callee listens for the event itself |
superseded-task-write | suspended at an await not handed the signal, and not tested since; the await being left was handed it | some path | Transfer::suspend sets the first fact unless the await was handed the signal (the second, set at the await's point); the whole-program pass reads what a function a call lends state to does with its Mut<T> parameters |
promise-never-settles | resolve or reject called or handed on | every path, to the executor's end | callback_runs for what a callback handed to a call does; an exception goes to thrown, which settles |
unreleased-resource | per resource: held; per resource and await: held when that await threw | some path, to the end and to thrown | awaits_throw, q_returns (Calls::q), Calls::throwing (a second solve, only for what the first left open: every call between that may throw, not std's, not awaited); Transfer::throw_from drops an await that can't reject and records where an exception started; the whole-program pass keeps a finding there only when one of those awaited calls can fail or calls can throw |
unbalanced-update | per pair (a change and, later in its statement list, the change that undoes it): done and not undone yet; per pair and way out (a return, a q(...), a throw, an await, a call that may throw; a continue or break between the two in a loop's body, where the round ends): left by it with the change done | some path, to the end and to thrown; for a round, the facts at the continue or break | awaits_throw, q_returns (Calls::q), jump_points, Calls::throwing (each call that may throw, Ev::Raise; the pass drops the exception edge of one whose callee can't); Transfer::throw_from drops an await that can't reject; the graph is reduced (Graph::reduce) to owned events in the file's analysis and solved by the whole-program pass (solve_reduced) once each call is replaced by what its callee's summary says it always does to what it is given; the pass keeps an await as a way out only when its call can fail |
decided-test | per place and literal: the place holds it | every path | a reduced graph, solved by the whole-program pass: a call's callee summary gives a literal or says what the callee may change; an await ends what is known of anything but the function's own locals, a call of what is shared |
unreachable-code | none: which points are reached, once as written and once with the calls that never return ending their path | — | Graph::reachable_starts |
test-without-assertion | an assertion ran (in the test, or in a helper whose every path asserts) | every path to the end of the test | none; helpers are followed by the whole-program pass |
aliased-mut | per mut(x) handed to a call: its promise is still pending (where it went: a local, a field, a list, dropped, handed on); for a way out by an exception, per loan and point an exception may start from: left there with the loan pending | some path, to the end; to thrown | an await of the promise (directly, through Promise.all over it or over the list it was put into, through a for...of that awaits each) ends it; a new value of the lent place's root drops it; callback_runs says which callbacks run again; whether the callee holds the loan is the whole-program pass's: a second analysis per callee, with one fact (suspended since the start), finds a write of the loan after an await. For a loan no normal way out reports, a graph of its own with awaits_throw and Calls::throwing (the function's calls, not std's, not a loan's holder); Transfer::throw_from drops an await that can't reject and, at an await Promise.all([...]), keeps the members' loans pending (another member rejected first); the pass keeps a finding there when a call it starts from can fail |
stale-across-awaitasks, at each write of a shared place, whether that place (or a part of it) was read before the last suspension and not read again. A write is reported when the read was a guard clause's test (check, then act) or when the written value or position is made from it; it builds a graph only for anasyncfunction whose own code reads a place, then awaits, then writes it (in the source, or anywhere in a loop).late-listenerasks, at each listener (on,once,addListener,prependListener,prependOnceListener,addEventListener, std'slisten), whether the receiver is late: the function had it, waited, and didn't test it since (a read of one of its properties in a condition, the receiver passed to a function in a condition,throwIfAborted()). A test right after the listener, before the nextawait, counts too. It builds a graph only for anasyncfunction whose own code adds a listener after a suspension in the source (or in a loop with one), and for a callback that runs later and adds one to something it captured.superseded-task-writeasks, at each change of state outliving the run in auseTask/latestcallback written in place, whether the run waited without the signal and didn't test it since. It builds a graph only for such a callback whose own code changes that state, or lends it to a call, after a suspension in the source.promise-never-settlessolves the executor on every path; a callback handed to a call that runs it once is solved the same way first (and a function of the executor bound to a name, where the name goes). It builds a graph pernew Promisewhose executor is written in place, and one per callback it asks.aliased-mutasks, at eachmut(...)and each write of a place, whether a loan of it (or of a part of it, or of what holds it) is alive: the loan of the round before in a loop, one kept on a branch that didn't await it, one kept in a list, a field, or dropped. At the end of a function it asks which loans are still alive: of the function's own parameter (the loan outlives the call), or of a place that outlives the run in a method or a callback that runs again (the next run overlaps). Each finding is kept by the whole-program pass only when the callee holds the loan across anawait(it writes the loan after one on some path, a write of its own answer too; a loan it lends on, by reachability over the program's loans).aliased-muton a way out by an exception: for each loan of the function's own parameter (or of a place outliving a function that runs again) that no normal way out reports, the function is solved again on a graph where anawaitmay reject and each of its own calls may throw, and the loans alive atthrown(or at a normal way out after acatch, for a loan of aPromise.allthat rejected) go to the whole-program pass with the calls the exception may start from. Athrow, ayieldand anawaitof something that isn't a call are ways out as they are.unbalanced-updateanddecided-testare solved in the whole-program pass, because a call's effect is its callee's: the file's analysis reduces the graph of each function it has something to ask about to the events these rules read (a write, a step up or down, a collection'sadd/delete/set/push/pop, a call and what it is given, anawait, a way out, a test of a place), owned and without the tree (flow::Reduced), and keeps each function's events in source order too. The pass sums up each function once per run, from those events and its callees' summaries: what it may change in each parameter, and what it always does to one, once, on every path that completes (memoised;writes_summarisedin the work guard counts the functions worked out).unreleased-resourcesolves each function that binds a resource, with the exception edges ofawaits andq(...)'s early returns. A resource held at the end is a finding at once; one held only where an exception leaves goes to the whole-program pass with theawaits it may have come from. For what is left, the graph is solved again with an exception edge after every call that may throw (and each call that runs a callback right away, for what the callback's own calls throw:left_behind::run_now_calls): a resource held where one of those leaves (or where acatchit reaches returns) goes to the pass with those calls, which keeps it when one can throw.- Which analyses don't ask about a synchronous throw, and why:
late-listener,stale-across-awaitandsuperseded-task-writeask about time passing at anawait, not a way out;promise-never-settlestakes an exception to settle the promise (the executor'sthrowrejects it), so a call that throws only adds a path that settles;must-use-resultclears a keptResult's facts on an exception;pending-without-closeisn't on this layer.aliased-mutasks it too: a loan still held where the function leaves by an exception (below).
What no path reaches
unreachable-code walks a function's graph twice: as written, and with each call that never returns (std's panic and unreachable, process.exit(), a function declared never) ending its path where it is. The call still throws, so a catch around it is reached; the code after it in its block, and the blocks only it led to, are not. What only the first walk reaches is reported, a run of statements at a time. What neither walk reaches is after a return, throw, break or continue: oxlint's no-unreachable (an error in the SAFE-TS preset) reports it, and the checker doesn't again. A function declaration and a type are hoisted, and never count as code after the call.
What it costs
A graph is built only for a function a rule has something to ask about, found first by a cheap look at its own code: an async function that writes shared state after reading it with an await between them; a new Promise with its executor written in place; a function that binds a resource from an operation the maps name. On an adopter's entries that is 30 graphs for stale-across-await, 71 for promise-never-settles (a callback asked inside an executor is one more) and 17 for unreleased-resource; its largest entry alone builds 11, 46 and 5. unreachable-code builds one for a function whose own code calls something that never returns (panic, process.exit(), a function declared never). late-listener builds one for an async function that adds a listener after an await in its source, or a callback that runs later and adds one to something it captured: 20 on an adopter's entries, 3 for the largest. unbalanced-update and decided-test build one for a function whose own code has two changes that undo each other in one statement list with a way out between them, two call statements lent the same place with a way out between them, or a test of a place after it was given a literal or after a call statement was lent what holds it (with no await between): 146 on an adopter's entries, 97 for its largest. A look at the file's nodes first passes over a function of a checked file with neither a Mut<...> parameter nor a change followed by a way out (7,210 of the largest entry's 10,454 functions); a light look at the rest finds the candidates, and records only what touches a Mut<...> parameter for a function whose summary a call may ask; the whole of its events is read for the candidates, a method of a class nothing can extend and any function of a file the loan rules don't hold in: 140 of the 10,454. A callee's summary describes its own changes only: a call it makes may change anything it is given (233 summaries for the largest entry; following those calls one level down made 546, cost about 3.5 ms more there and changed no finding anywhere measured). superseded-task-write builds one for a task callback that changes outliving state, or lends it, after an await: none on the same entries, whose three tasks hand their signal on. aliased-mut builds a graph for a function that hands mut(x) to a call whose promise it doesn't await or return where it makes it, where a path can matter (a loop, another lend or write of the same object, an await that ends the loan; a statement that drops a call without void isn't followed), and one per async function that takes a Mut and writes it after a suspension in the source, to ask whether it does on a path. An adopter's code, before its values are lent with mut(x), has no loans: 20 graphs on its largest entry, all for those functions. Migrated (every value passed to a Mut parameter lent), its largest entry builds 507, most for a call whose result is kept in a local or handed to a test's expect(...), which only the whole-program pass can tell from a promise.