exhaustive-switch / exhaustive-dispatch: name only the members that can reach the dispatch #154
Loading…
Reference in a new issue
No description provided.
Delete branch "narrowed-dispatch"
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?
Both rules listed members that TypeScript had already narrowed away. An adopter hit two forms:
switchafterif (tool === "bridge") return ...;asked forcase "bridge", which is TS2678;?:chain inside an eight-labelcaseof aswitchon a command's type was reported for the other 24 members.What changed
crates/safe_ts_core/src/narrowing.rs, built onflow.rs. In the file pass, each function holding a dispatch whose value is tested elsewhere in it gets its graph reduced to change events, marks and tests (the tests keep the values as written). The whole-program pass resolves those values (Job.MOVE→1) and solves one fact per member plus one for "a member added later", withMeet::Every.return,throw,continue,break, std'sunreachable/panic),||, aconstholding a test, enclosingif/&&/?:branches, a precedingswitchwhose cases return or whosedefaultisunreachable, and thecaseof an enclosingswitchwith fall-through. Inside an enclosingcase, "and so would a new member" is dropped, and a dispatch that handles every label isn't reported.++,delete, or a re-declaration in a loop); a write of that member through another name; a loan (mut(...)); a method called on a prefix or on a receiver that isn't a place;await/yield; any call, for a value something else can reach (this, module-level, an outer binding) or one a closure here changes or hands on; a getter read (newTypeLookup::runs_accessor). Calls given the object without a loan don't count, per the loan rules (the rule only runs in checked files).dispatch.rsfilters "doesn't handle" by what can reach the site and says how many members can (some of the 3 values that can reach it (of the 5 of its type)).docs/rules/exhaustive-switch.mdandexhaustive-dispatch.mdget "Members the code before it took". CHANGELOG has an entry under Unreleased → Fixed.Tests
tests/fixtures/narrowed_dispatch/. Its 24// lists:/// laterannotations are checked bymessages.rs. It covers string unions,Enum/is(),codesOfcodes (typed andnumber),x.kind, and each invalidation.meaning.rsprogram applies the Add default: unreachable(…) fix on a narrowed switch; tsc passes before and after.neverin TS.cargo nextest run --workspace: 413/413 pass.npm run test:docspasses. Clippy wasn't run: it isn't installed for the pinned nightly.rewrites/misses.txt:miss exhaustive-dispatch ternary-to-ifgoes from 2 to 6. It's the same existing gap, hit at the new fixture's sites.Measurements
check --json --idiomatic): 22 exhaustive-switch / 59 exhaustive-dispatch before and after. No site disappears, because each still has a real missing member. 11 messages shrink, with 89 members removed in total. Each removed member was checked by hand as unreachable at its site:case: 671 (24 removed), 687 (27, also through aconstalias), 700 (29);switchwhose cases return: world-ops 401 and 522;switchwithdefault: unreachable: civic 487 (only the "new member" clause is dropped).Not done
unreachable-code(decided) still reads the declared type.default: unreachable(...), and on a narrowed switch it compiles.🤖 Generated with Claude Code
std's mut imported under another name, a loan inside the arguments (touchAll({ h: mut(h) })), std.mut, a const copy of mut, .call/.apply, and a method called through .call or a const copy of its object now undo the narrowing of a path, as mut(h) and h.turn() written plainly do. Found by the full rewrites ratchet: the renamed import and the nested loan kept the narrowing on the tree as checked, so a member the code may have set since was left out of the list. Co-Authored-By: Claude Opus 5.5 <[email protected]>imports.imported keeps one local name per imported one, picked in no fixed order, so with `import { mut, mut as lend }` the loan through `lend` was seen in some runs only. Co-Authored-By: Claude Opus 5.5 <[email protected]>