A copy with new lists lent to a callee that only changes the lists: no no-param-mutation #156
Loading…
Reference in a new issue
No description provided.
Delete branch "shallow-copy-items"
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?
An adopter's
validateCommand(world)clones the world ({ ...world, remains: world.remains.slice(), ... }, about 40 fields) and lends the copy toexecute(mut(copy), ...).no-param-mutationreported it as "whattrunkMaimed,weedPicked,puddlesand 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.rsgets a new whole-program summary, built next toparam_fields.FnBorrow::param_itemsrecords, perMutparameter 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 existingtrackand steps past changes to the list itself:splice,push,pop,sort, an index written,length = 0, mapset/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_changedsolves 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-Mutparameter in a checked file changes nothing (the rule reports the function that does, asleft_behindalready assumes).mutation::lent_copykeeps a held field only if the callee may change one of its items. The message then names only those fields.xs.forEach(heal)(andmap,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 anunneeded-mutfalse report (confirmed on main).new Map(o.byId)/new Set(o.tags)in a copy are now new collections of the same objects (part_inandreach). Before, changing a value they hold was never reported, inline or through a clone helper (confirmed on main).project/left_behind.rs'sSlotSumwas considered and not used: it doesn't tell a list from its items, and atDEPTH = 0any call counts as changing everything.The adopter site
It is still reported, with the same 11 fields. Reading the code,
executechanges only the lists of the 7 object-list fields among them: it replaces records (world.remains[k] = next, new lists inrelayoutWorld). The checker can't show that, for two reasons:enterWorkplacecalls a function value read from a table (door(world, u, ...)), which nothing follows. On its own, this keeps the answer "any field".trackyet: a local closure called withp.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
numberfield can't be written through. (1) needs callback types.Numbers (fast profile)
--checkpasses)no-param-mutationon the adopter's sim + protocolThe timing was taken under a load average of about 40 from other sessions. No difference shows above the noise.
Tests
shared_copies/items.ts: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 ofT[][], 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/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
`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]>