Borrow checks for parameters: stale-read, unneeded-mut (idiomatic) #54

Merged
sionzee merged 3 commits from evobug/mut-rules into main 2026-11-16 17:44:05 +01:00
Owner

What

Two of Rust's borrow checks, for parameters (stacked on #48).

  • stale-read (main set, warn in the oxlint preset): a local holding an object read from a parameter (const world = game.state.world, or currentWorld(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): a Mut<T> parameter the function never changes, like Rust's unused_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

  • New 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).
  • Precision first, so "can't tell" never reports:
    • unneeded-mut: the value is returned, stored, thrown, or passed to a package, a callback or an unknown method. A callee's Mut<...> counts as a change. So does a borrowed parameter the callee passes on to a Mut one: no-param-mutation doesn't catch that, and TwitchRTS does it a lot. Signatures fixed from outside are skipped (callbacks, typed functions, methods of classes that implements/extends, typed object literals).
    • stale-read: unknown callees, computed keys, and assignments of primitives or undefined replace nothing. A local that is only used after the call is a deliberate snapshot, so it's not reported. Neither are previous…-style names, comparisons, putting the old value back, other branches, and paths that return first.
  • The whole-program pass now remembers module resolutions for the length of one pass.

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 read world before sendCommand(), then use it after. Online, sendCommand → onlineCommand → updateDisplay → recompute → replaceWorld can put a recomputed world in game.state.world first. A third candidate (islands in showUpdate) is a deliberate snapshot ("locked parts as they were before the change"). The snapshot rule above drops it.
  • unneeded-mut: 0 findings on the 253 Mut<>s, as expected: they were added where no-param-mutation asked for them. To get a precision sample, I declared every game: Game as Mut<Game> in a scratch copy: 31 findings, and all 31 are functions that only read game.
  • Cost: +12% instructions single-threaded on this client. That's close to the worst case, since nearly every function passes game on.

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 fixture fixtures/borrow (positives and each skip case, cross-file), 3 quick-fix tests, and tested docs examples. One example has a Mut<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 to unneeded-mut), CHANGELOG.md (Unreleased).

🤖 Generated with Claude Code

## What Two of Rust's borrow checks, for parameters (stacked on #48). - **`stale-read`** (main set, `warn` in the oxlint preset): a local holding an object read from a parameter (`const world = game.state.world`, or `currentWorld(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`): a `Mut<T>` parameter the function never changes, like Rust's `unused_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 - New `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). - Precision first, so "can't tell" never reports: - `unneeded-mut`: the value is returned, stored, thrown, or passed to a package, a callback or an unknown method. A callee's `Mut<...>` counts as a change. So does a borrowed parameter the callee passes on to a `Mut` one: `no-param-mutation` doesn't catch that, and TwitchRTS does it a lot. Signatures fixed from outside are skipped (callbacks, typed functions, methods of classes that `implements`/`extends`, typed object literals). - `stale-read`: unknown callees, computed keys, and assignments of primitives or `undefined` replace nothing. A local that is only used after the call is a deliberate snapshot, so it's not reported. Neither are `previous…`-style names, comparisons, putting the old value back, other branches, and paths that return first. - The whole-program pass now remembers module resolutions for the length of one pass. ## 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 read `world` before `sendCommand()`, then use it after. Online, `sendCommand` → `onlineCommand` → `updateDisplay` → `recompute` → `replaceWorld` can put a recomputed world in `game.state.world` first. A third candidate (`islands` in `showUpdate`) is a deliberate snapshot ("locked parts as they were before the change"). The snapshot rule above drops it. - `unneeded-mut`: 0 findings on the 253 `Mut<>`s, as expected: they were added where `no-param-mutation` asked for them. To get a precision sample, I declared every `game: Game` as `Mut<Game>` in a scratch copy: 31 findings, and all 31 are functions that only read `game`. - Cost: +12% instructions single-threaded on this client. That's close to the worst case, since nearly every function passes `game` on. ## 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 fixture `fixtures/borrow` (positives and each skip case, cross-file), 3 quick-fix tests, and tested docs examples. One example has a `Mut<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 to `unneeded-mut`), `CHANGELOG.md` (Unreleased). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Borrow checks for parameters: stale-read, unneeded-mut (idiomatic)
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m5s
8325d801e0
stale-read: a local holding an object read from a parameter
(`const world = game.state.world`, or a call that returns such a path) is
used after a call that assigns that place or one above it, directly or
further down the call graph, across files. Changes made inside the object
are fine. Unknown callees replace nothing; a local kept unused across the
call (a deliberate snapshot), comparisons and restores aren't reported.

unneeded-mut (idiomatic): a `Mut<T>` parameter the function never changes.
Uses are followed through aliases, loops, callbacks and into project
functions (a borrowed parameter that is passed on to a `Mut` one still
changes it); returned, stored, thrown or handed to a package or a callback
means "can't tell": not reported. Skips functions whose signature is fixed
from outside. Quick fix unwraps the type and drops an unused import.

The semantic pass now builds parent links (used by borrow.rs), and the
whole-program pass remembers module resolutions for its duration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee changed target branch from evobug/idiomatic-option to main 2026-11-16 17:31:58 +01:00
Merge remote-tracking branch 'origin/main' into up-54
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m6s
f9771bd380
sionzee deleted branch evobug/mut-rules 2026-11-16 17:44:06 +01:00
sionzee referenced this pull request from a commit 2026-11-16 17:44:07 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!54
No description provided.