Skip to content

feat(rules): duplicate-items reports function bodies written twice - #33

Draft
PiquelChips wants to merge 1 commit into
feat/rule-route-literalsfrom
feat/rule-duplicate-items
Draft

PiquelChips wants to merge 1 commit into
feat/rule-route-literalsfrom
feat/rule-duplicate-items

Conversation

@PiquelChips

Copy link
Copy Markdown
Member

What and why

Code isn't duplicated, even small functions: shared behaviour lives once, on its owning type (PR2). Copies with renamed variables slip past review, and nothing checked for them. duplicate-items fails on a function body outside test code that repeats another one, token for token, once identifiers are renamed in the order they appear, when the body holds at least 50 tokens.

  • Shape: the body's tokens, with keywords, punctuation and literals kept, and every other identifier renamed by first appearance ($0, $1, …). Bodies that differ only in names, including the path constant or method they call, share a shape. The shape is hashed. A copy names its first occurrence, path:line.
  • Scope:
  • The walk now passes each function with a body to Inspect::function.
  • No --fix: merging copies takes judgement. The cleanup skill judges what is left below the threshold.

Stacked on #32.

Testing

  • just check-changed feat/rule-route-literals passes.
  • New fixture test: a 50-token body and its copy with every name changed, in another module, fail at the copy. A duplicated function under 50 tokens passes, and so do copies in an integration test.

Choosing N, on detached worktrees of each repository's origin/main (since removed), counting copies (piqueld / sourceorigin / DirkEngine):

N 20 30 40 50 60 80 120
copies 62 / 49 / 20 15 / 29 / 10 5 / 9 / 6 4 / 6 / 4 3 / 3 / 0 2 / 1 / 0 0 / 0 / 0

At 40, boilerplate comes in: two-route routes() routers in sourceorigin, and From impls across crates. At 50, every hit is logic written twice with one name changed:

  • piqueld: delete_secret/delete_stored_secret (only the store method differs), the fake Docker's remove_service/remove_volume, and three piquelctl env subcommands' run. The one debatable copy is timing/timing_mut, a &/&mut accessor pair that only a macro would share.
  • sourceorigin: schedule_deletion/purge_user (.schedule vs .purge), the admin invitation handlers resend/revoke, recover_page/undo_page, sign_out_page/unsubscribe_page, emails/passkeys, and two from_request_parts.
  • DirkEngine: frame_graph's read_texture_fragment/read_texture_compute/read_transfer_src/write_transfer_dst (only the Vulkan constants differ), and two to_egui conversions (only Pos2/Vec2 differ).

Cost: the rule takes about 1 s of a debug cargo xtask standard on piqueld, where loading and parsing the files takes about 5 s.

Checklist

  • The crate's README (its documentation on docs.rs) shows the change, and a repository's xtask can still follow it. (just readme.)
  • A change to the public API is noted for the next release, with what each repository's xtask must change. (A new Rule variant; no xtask change.)
  • just check-changed passes.

Note

Written by Claude Opus 5.5 in Claude Code (via T3 Code)

A function body outside test code with at least 50 tokens fails when it
repeats another token for token once identifiers are renamed in the order
they appear. The walk passes each function with a body to Inspect, and a
body's shape is hashed. Below 50 tokens, the three repositories' copies
are boilerplate rather than logic to share.
@PiquelChips
PiquelChips force-pushed the feat/rule-duplicate-items branch from 25d3636 to bc0c299 Compare November 22, 2026 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant