A shared control-flow layer; values used after they were consumed; a Result kept in a local #120
Loading…
Reference in a new issue
No description provided.
Delete branch "flow-consumed"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Based on
escape-hatches-2(#119), and contains it: the diff shown is this piece alone. The open pieces above #118 form one chain, each branch containing the ones below it: #119, #120, #121, #123, then #116 and #122. Merge them in that order.Combined with the pieces below.
analyze_file_withtakes both this piece'sconsumestable and #119's marker permission; nothing else overlapped.Several rules ask "on some path, did A happen before B?", and each had its own walker with its own idea of branches, loops and early exits: eight of them, listed below. The next bug classes on the roadmap all need that question answered properly. These passed
tscstrict,safe-ts check --deny-warningsand oxlint, and are wrong at run time:What this adds
A shared control-flow layer (
flow.rs). One graph per function, built only when an analysis asks:if/?:/&&/||/??(branching straight to their targets inside a test), optional chains, every loop with its back edge,switchfall-through, labelledbreak/continue,return,throw, andtry/catch/finallywith thefinallycopied for each way out.awaitandyieldend their block, so the analyses that come next can ask what happens across them. On top, one forward data-flow driver for "on some path" and "on every path". oxc's own graph was measured and not used: it is built eagerly for every function (+21 to 26% on semantic analysis), marks statements only, and does not markawait.number-parse-nanmoved onto it, with identical findings on an adopter's project. Three cases the old source-order walker got wrong are now right: a check in the other branch no longer covers a use, a use on the next round of a loop is after the parse, andlet n = Number(s); n = 0;is no longer reported.New rule
use-after-consume(error). A value that an operation uses up, used again on a path where the operation ran: a transferredArrayBuffer(also through a view's.buffer, and a second name for the same object), a generator or one-shot iterator run twice, aResponse/Requestbody read twice, a stream with two readers, and use afterclose()on sockets, ports, workers, file handles andreadline. Which operations consume is data, aconsumessection in the ECMAScript map and in each pack's manifest, so a third-party pack can declare its own; every entry is demonstrated on Node, and the worker ones in Chromium, Firefox and WebKit.must-use-resultsees aResultkept in a local and never looked at on some path to the function's end: also behind?:/??/await, and each element ofconst [a, b] = await Promise.all([...]). Logging and string conversion are not looking; returning it, storing it, handing it on or capturing it is.Evidence
breakout of two loops,do...while, a loop's update clause,continueinsidetry/finally,defaultin the middle of aswitch, an exception path intocatch). All are answered correctly. The repository now has its own table of 39 shapes that tests the layer without any rule.res.okafter the body, a fresh response per retry, an array iterator's rest afterbreak, listeners removed afterclose(), andtry/finallyaround a file handle are all silent.Resultcheck reported six places there; four were an earlyreturn(a cancellation) before the code that checks theResult, which is deliberate. The rule now counts only a function end that comes after every look.one-shot-generator-reusedanduse-after-consume-arraybufferare closed;response-body-read-twiceandresult-kept-only-loggedwere added and closed. 10 of 33 classes escape.The walkers that have not moved
search_index.rsnot-found-indexforupdate clauseborrow.rsreachesstale-readswitchcounts as "not reached"holders.rsbounds.rsdivide-by-length,sparse-array-write,a[i]!proofsshape.rsnarrowingif (...) exitin the same statement listlints.rsstable-shapevue.rsvue-setup-after-awaitawait" in source orderThey move one at a time, as decided. The three analyses planned next (state read before an
awaitand written after it, a resource released on every exit, anew Promisewith a path that never settles) are written up indocs/guide/flow.mdwith what the layer still lacks for each.What stays unseen
let.Resultabandoned by an earlyreturnbefore the code that checks it, and one behindconst { result } = pair().number-parse-nan:if (text !== "" && Number.isNaN(n)) return;leaves the empty-text path unchecked, so it is reported althoughNumber("")is0.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
Open (review): `const r = flag ? save(1) : save(2)` and `const [a, b] = await Promise.all([...])` weren't seen as kept Results; the kept-Result analysis built a graph for one function in five on the adopter's largest entry (2,561 of ~10,500) and use-after-consume for 539, mostly for calls that can't matter; and the layer was held only by ten unit tests and the rules' fixtures. Closes: - Kept: each call of a `?:` / `??` / `||` / `&&` / parenthesised / awaited initialiser, and each element of an array destructuring of `Promise.all` / `allSettled` over a list written in place, in the binding at its place (fixtures). `const { result } = pair()` isn't (only `pair`'s return type says what `result` is). - The cheap test first. Kept Results: the local pass only lists the kept calls (no graph), after two syntactic tests (a const looked at unconditionally by the very next statement; a call whose callee the file shows returns a plain type, or a method of a value declared with one); the whole-program pass keeps those whose function returns a Result and only then parses the file again and builds graphs for them (`unsafe(...)` / `parseJson` stay local). use-after-consume: a function gets a graph only if an operation there would use a binding up (`Array.from(list)` of an array doesn't); the generator walk runs only in a file with a `function*`. On the adopter's `apps/client/src/start.ts`: 3,119 graphs before (2,561 kept, 539 consumed, 19 number-parse-nan), 40 after (21, 0, 19). - flow::tests::path_cases: 39 shapes with their answers on some / every path, independent of any rule (labelled break and continue, do...while, a loop's update clause, for await, continue in try/finally, finally after a return, nested finally, switch with default in the middle and falling into it, && / || in a statement and a test, ?:, ??, ||=, optional calls, yield and await between, reset, an exception into catch, nested functions, unreachable code). - Docs: the libraries guide says a pack's `consumes` is read from its safe-ts.json (and the manifest example shows it); use-after-consume's page covers views, second names and optional calls; must-use-result's covers branches and Promise.all; README gaps, CHANGELOG, tool gaps. Co-Authored-By: Claude Opus 5.5 <[email protected]>promise-as-value,floating-promise#121this, parts handed out, kept copies, wider aliases, loans alive at once #122.oxlintrc.json, the file that sets themdeclareis a cast #119