A copy with new lists lent to a callee that only changes the lists: no no-param-mutation #156

Merged
sionzee merged 2 commits from shallow-copy-items into main 2026-11-24 11:39:25 +01:00
Owner

An adopter's validateCommand(world) clones the world ({ ...world, remains: world.remains.slice(), ... }, about 40 fields) and lends the copy to execute(mut(copy), ...). no-param-mutation reported it as "what trunkMaimed, weedPicked, puddles and 8 more hold is still the caller's" whenever the callee named those fields, even when it only spliced, pushed or wrote an index. A new list is the copy's own, and only its items are shared with the caller.

What changes

  • borrow.rs gets a new whole-program summary, built next to param_fields. FnBorrow::param_items records, per Mut parameter and per field named on it, what reaches the items of the list (or map, or set) that field holds. It is followed with the existing track and steps past changes to the list itself: splice, push, pop, sort, an index written, length = 0, map set/delete, w.units = w.units.filter(...). It also records the calls the parameter is handed to whole, and a spread copy made in the callee (const before = { ...w }) counts as the parameter.
  • items_changed solves the graph of those calls with Tarjan's SCCs, across files and around cycles. The field sets only grow along a call, so a cycle shares one answer, and the walk is linear. A non-Mut parameter in a checked file changes nothing (the rule reports the function that does, as left_behind already assumes).
  • mutation::lent_copy keeps a held field only if the callee may change one of its items. The message then names only those fields.
  • Soundness gaps closed along the way:
    • xs.forEach(heal) (and map, sort, find, ...) now follows a named function like a call. Array.from(xs, f) counts as a change. Before, both handed elements to code nothing followed, which also caused an unneeded-mut false report (confirmed on main).
    • new Map(o.byId) / new Set(o.tags) in a copy are now new collections of the same objects (part_in and reach). Before, changing a value they hold was never reported, inline or through a clone helper (confirmed on main).

project/left_behind.rs's SlotSum was considered and not used: it doesn't tell a list from its items, and at DEPTH = 0 any call counts as changing everything.

The adopter site

It is still reported, with the same 11 fields. Reading the code, execute changes only the lists of the 7 object-list fields among them: it replaces records (world.remains[k] = next, new lists in relayoutWorld). The checker can't show that, for two reasons:

  1. enterWorkplace calls a function value read from a table (door(world, u, ...)), which nothing follows. On its own, this keeps the answer "any field".
  2. Some item reads aren't followed by track yet: a local closure called with p.cell, { ...e, grave }, and an item pushed into another list. These count as changes.

A follow-up could clear (2) using written types, since a read of a number field can't be written through. (1) needs callback types.

Numbers (fast profile)

main this branch
soundness corpus, classes escaping 16 of 110 16 of 110 (--check passes)
no-param-mutation on the adopter's sim + protocol 10 10 (same findings, same messages)
wall time, same tree, interleaved runs 2.87 s (whole-program pass 1.81 s) 2.79 s (1.78 s)

The timing was taken under a load average of about 40 from other sessions. No difference shows above the noise.

Tests

  • New held-out fixture shared_copies/items.ts:
    • List-only callees stay quiet: through a clone helper, inline, through destructuring and aliases, through a pass, and around a cycle.
    • 18 forms are reported, each naming only its field: a loop write, an alias, find(...)!, forEach, an item handed to a changing function, a named callback, a list handed to unseen code, splice's answer, a map value, a set item, an inner list of T[][], a spread copy in the callee, through a pass, through a cycle, inline Map, inline Set, an inline list, and uncopied lists that are changed.
  • cargo nextest run --workspace: 412 passed.
  • npm run test:docs: passes.
  • Docs: docs/rules/no-param-mutation.md (Copies: what counts as an item change, what it can't clear, a new ✅ example) and the CHANGELOG under Unreleased → Fixed.

🤖 Generated with Claude Code

An adopter's `validateCommand(world)` clones the world (`{ ...world, remains: world.remains.slice(), ... }`, about 40 fields) and lends the copy to `execute(mut(copy), ...)`. `no-param-mutation` reported it as "what `trunkMaimed`, `weedPicked`, `puddles` and 8 more hold is still the caller's" whenever the callee named those fields, even when it only spliced, pushed or wrote an index. A new list is the copy's own, and only its items are shared with the caller. ## What changes - **`borrow.rs`** gets a new whole-program summary, built next to `param_fields`. `FnBorrow::param_items` records, per `Mut` parameter and per field named on it, what reaches the items of the list (or map, or set) that field holds. It is followed with the existing `track` and steps past changes to the list itself: `splice`, `push`, `pop`, `sort`, an index written, `length = 0`, map `set`/`delete`, `w.units = w.units.filter(...)`. It also records the calls the parameter is handed to whole, and a spread copy made in the callee (`const before = { ...w }`) counts as the parameter. - **`items_changed`** solves the graph of those calls with Tarjan's SCCs, across files and around cycles. The field sets only grow along a call, so a cycle shares one answer, and the walk is linear. A non-`Mut` parameter in a checked file changes nothing (the rule reports the function that does, as `left_behind` already assumes). - **`mutation::lent_copy`** keeps a held field only if the callee may change one of its items. The message then names only those fields. - **Soundness gaps closed along the way:** - `xs.forEach(heal)` (and `map`, `sort`, `find`, ...) now follows a named function like a call. `Array.from(xs, f)` counts as a change. Before, both handed elements to code nothing followed, which also caused an `unneeded-mut` false report (confirmed on main). - `new Map(o.byId)` / `new Set(o.tags)` in a copy are now new collections of the same objects (`part_in` and `reach`). Before, changing a value they hold was never reported, inline or through a clone helper (confirmed on main). `project/left_behind.rs`'s `SlotSum` was considered and not used: it doesn't tell a list from its items, and at `DEPTH = 0` any call counts as changing everything. ## The adopter site It is still reported, with the same 11 fields. Reading the code, `execute` changes only the lists of the 7 object-list fields among them: it replaces records (`world.remains[k] = next`, new lists in `relayoutWorld`). The checker can't show that, for two reasons: 1. `enterWorkplace` calls a function value read from a table (`door(world, u, ...)`), which nothing follows. On its own, this keeps the answer "any field". 2. Some item reads aren't followed by `track` yet: a local closure called with `p.cell`, `{ ...e, grave }`, and an item pushed into another list. These count as changes. A follow-up could clear (2) using written types, since a read of a `number` field can't be written through. (1) needs callback types. ## Numbers (fast profile) | | main | this branch | |---|---|---| | soundness corpus, classes escaping | 16 of 110 | 16 of 110 (`--check` passes) | | `no-param-mutation` on the adopter's sim + protocol | 10 | 10 (same findings, same messages) | | wall time, same tree, interleaved runs | 2.87 s (whole-program pass 1.81 s) | 2.79 s (1.78 s) | The timing was taken under a load average of about 40 from other sessions. No difference shows above the noise. ## Tests - New held-out fixture `shared_copies/items.ts`: - List-only callees stay quiet: through a clone helper, inline, through destructuring and aliases, through a pass, and around a cycle. - 18 forms are reported, each naming only its field: a loop write, an alias, `find(...)!`, `forEach`, an item handed to a changing function, a named callback, a list handed to unseen code, `splice`'s answer, a map value, a set item, an inner list of `T[][]`, a spread copy in the callee, through a pass, through a cycle, inline Map, inline Set, an inline list, and uncopied lists that are changed. - `cargo nextest run --workspace`: 412 passed. - `npm run test:docs`: passes. - Docs: `docs/rules/no-param-mutation.md` (Copies: what counts as an item change, what it can't clear, a new ✅ example) and the CHANGELOG under Unreleased → Fixed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A copy with new lists lent to a callee that only changes the lists: no no-param-mutation
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m22s
Tests / test (pull_request) Failing after 14m17s
1b77a5fab8
`const copy = { ...w, units: w.units.slice() }; trim(mut(copy))` was reported whenever `trim`
named `units`, though a new list is the copy's own and only its items are the caller's. The
lend now keeps such a field only where the callee may change an item, as the whole program
says.

- borrow.rs: `FnBorrow::param_items` (and a method's), per `Mut` parameter: per field named on
  it, what reaches the items of the list (map, set) it holds, followed with `track` past what
  only changes the list (splice, push, pop, sort, an index written, length = 0, map set/delete,
  `w.units = w.units.filter(...)`); the calls it hands the parameter to whole; a spread copy of
  it made in the callee counts as the parameter. `items_changed` solves the calls the parameter
  is handed to by their strongly connected components (Tarjan), across files; a non-`Mut`
  parameter in a checked file changes nothing.
- mutation.rs: `lent_copy` drops a held field the callee can't change an item of.
- An iterating method given a named function (`units.forEach(heal)`) follows it like a call,
  and `Array.from(xs, f)` counts as a change: both handed the elements to code nothing followed
  (also an `unneeded-mut` false report).
- `new Map(o.byId)` / `new Set(o.tags)` in a copy are new collections of the same objects
  (borrow.rs `part_in`, analyze.rs `reach`): changing a value they hold went unreported.

Held-out fixture `shared_copies/items.ts`: list-only callees silent; an item written in a loop,
through an alias, `find(...)!`, `forEach`, handed to a changing function or a named callback,
a list handed to unseen code, `splice`'s answer, a map value, a set item, an inner list, a
spread copy in the callee, a callee reached through a pass and through a cycle all reported,
each naming only that field.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge main
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
91162f4110
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch shallow-copy-items 2026-11-24 11:39:25 +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!156
No description provided.