lint rule: hand-rolled-fetch (key? ? h[k] : default pattern) #205

Closed
opened 2026-08-01 14:16:55 +00:00 by jared · 1 comment
Owner

Migrated from jared/os-sdlc#31 (repo retired).

Problem

@h.key?(k) ? @h[k] : default

A hand-rolled default-lookup where Hash#fetch already does this. Note: rubocop's own Style/HashFetch is NOT in play here -- this repo's .rubocop.yml sets AllCops: DisabledByDefault: true, so only explicitly-listed cops run, and Style/HashFetch is not among them.

Detection

  • Inputs: lib/ Ruby source.
  • Algorithm: custom cop matching the AST pattern h.key?(k) ? h[k] : d (ternary guarded by #key? on the same receiver/key, else-branch a constant/literal), autocorrecting to h.fetch(k, d).
  • Failure message: "reimplements Hash#fetch via key? ? h[k] : default. Use h.fetch(k, default)."

Correction

Use Hash#fetch with a default: h.fetch(k, default).

Pass/fail examples

  • Must fail: the ternary h.key?(k) ? h[k] : default pattern.
  • Must pass: h.fetch(k, default).

Provenance

Fable finding. Run 18 (ticket #18, decision-dice sandbox, now retired), dual review, 2026-07-21.

Implementation plan

Candidate custom cop for plugins/os-sdlc/lib/os_sdlc/cops/ (Sdlc/Structural or Sdlc/Dry namespace); autocorrect-capable like rubocop's own Style cops.

Migrated from jared/os-sdlc#31 (repo retired). ## Problem @h.key?(k) ? @h[k] : default A hand-rolled default-lookup where Hash#fetch already does this. Note: rubocop's own Style/HashFetch is NOT in play here -- this repo's .rubocop.yml sets `AllCops: DisabledByDefault: true`, so only explicitly-listed cops run, and Style/HashFetch is not among them. ## Detection - **Inputs:** lib/ Ruby source. - **Algorithm:** custom cop matching the AST pattern `h.key?(k) ? h[k] : d` (ternary guarded by #key? on the same receiver/key, else-branch a constant/literal), autocorrecting to `h.fetch(k, d)`. - **Failure message:** "reimplements Hash#fetch via key? ? h[k] : default. Use h.fetch(k, default)." ## Correction Use Hash#fetch with a default: `h.fetch(k, default)`. ## Pass/fail examples - **Must fail:** the ternary `h.key?(k) ? h[k] : default` pattern. - **Must pass:** `h.fetch(k, default)`. ## Provenance Fable finding. Run 18 (ticket #18, decision-dice sandbox, now retired), dual review, 2026-07-21. ## Implementation plan Candidate custom cop for plugins/os-sdlc/lib/os_sdlc/cops/ (Sdlc/Structural or Sdlc/Dry namespace); autocorrect-capable like rubocop's own Style cops.
Author
Owner

Implemented Sdlc/Dry/HandRolledFetch cop (lib/os_sdlc/cops/hand_rolled_fetch.rb, tests/cops/hand_rolled_fetch_test.rb, 7 tests incl. autocorrect). Registered in .rubocop.yml. Commit c54593f.

Implemented Sdlc/Dry/HandRolledFetch cop (lib/os_sdlc/cops/hand_rolled_fetch.rb, tests/cops/hand_rolled_fetch_test.rb, 7 tests incl. autocorrect). Registered in .rubocop.yml. Commit c54593f.
jared closed this issue 2026-08-01 14:27:18 +00:00
Sign in to join this conversation.
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
jared/cc-os#205
No description provided.