Borrow checks for parameters: stale-read, unneeded-mut (idiomatic) #54
Loading…
Reference in a new issue
No description provided.
Delete branch "evobug/mut-rules"
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?
What
Two of Rust's borrow checks, for parameters (stacked on #48).
stale-read(main set,warnin the oxlint preset): a local holding an object read from a parameter (const world = game.state.world, orcurrentWorld(game)when that returns such a path) is used after a call that assigns that place or one above it (game.state.world = ...,game.state = ...). The assignment can be in the callee or further down the call graph (sendCommand(game)→onlineCommand→recompute(game.state)→replaceWorld), across files. Changes made inside the object (world.units.push) aren't reported.unneeded-mut(idiomatic,warn): aMut<T>parameter the function never changes, like Rust'sunused_mut. Quick fix Take it borrowed:game: Mut<Game>→game: Game, and it drops the import when nothing else uses it. Callers don't change.How
borrow.rs. Per file it records, for each top-level function, the parameter paths it assigns, passes on and returns. For every parameter it also records whether the function only reads it. Uses are followed up the syntax tree (the semantic pass now builds parent links).unneeded-mut: the value is returned, stored, thrown, or passed to a package, a callback or an unknown method. A callee'sMut<...>counts as a change. So does a borrowed parameter the callee passes on to aMutone:no-param-mutationdoesn't catch that, and TwitchRTS does it a lot. Signatures fixed from outside are skipped (callbacks, typed functions, methods of classes thatimplements/extends, typed object literals).stale-read: unknown callees, computed keys, and assignments of primitives orundefinedreplace nothing. A local that is only used after the call is a deliberate snapshot, so it's not reported. Neither areprevious…-style names, comparisons, putting the old value back, other branches, and paths that return first.TwitchRTS client (
apps/client/src, 73 files)stale-read: 2 findings, both real.trade()(market.ts:43) and the group order in settlement.ts:241 readworldbeforesendCommand(), then use it after. Online,sendCommand→onlineCommand→updateDisplay→recompute→replaceWorldcan put a recomputed world ingame.state.worldfirst. A third candidate (islandsinshowUpdate) is a deliberate snapshot ("locked parts as they were before the change"). The snapshot rule above drops it.unneeded-mut: 0 findings on the 253Mut<>s, as expected: they were added whereno-param-mutationasked for them. To get a precision sample, I declared everygame: GameasMut<Game>in a scratch copy: 31 findings, and all 31 are functions that only readgame.gameon.Verification
cargo nextest run --workspace(186 passed), clippy (nothing new),npm run build,npm run test:std(242 passed),npm run typecheck,npm run typecheck:emit,node scripts/docs-typecheck.mjs,npm run test:docs. Tests added: the fixturefixtures/borrow(positives and each skip case, cross-file), 3 quick-fix tests, and tested docs examples. One example has aMut<Game>caller of the fixed function, so tsc checks that callers still type-check.Docs changed
New:
docs/rules/stale-read.md,docs/rules/unneeded-mut.md.Updated:
README.md(rule table),docs/README.md(both tables),docs/.vitepress/config.mts(sidebar: State, Idiomatic),docs/guide/state.md(Borrow only what you change, rule list),docs/guide/adoption.md(idiomatic table and preset note),docs/rules/no-param-mutation.md(link tounneeded-mut),CHANGELOG.md(Unreleased).🤖 Generated with Claude Code