Skip to content

feat: treat Rust matches! patterns as magic-string decision points - #214

Merged
dbrattli merged 1 commit into
mainfrom
feat/magic-string-matches-macro
Nov 22, 2026
Merged

dbrattli merged 1 commit into
mainfrom
feat/magic-string-matches-macro

Conversation

@dbrattli

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #212. tree-sitter leaves macro arguments as flat token trees, so a string in a matches! pattern never counted as a magic-string decision point. Code like this was missed:

if !matches!(kind, Some("task_started" | "task_complete" | "turn_aborted")) { … }
match kind {
    Some("task_started") => …,
    …
}

Each string appears once in matches! and once in a match arm, so it's now flagged (meets the default minDuplicates of 2).

How it works

src/Languages/RustPatterns.fs reads a matches! argument tree token by token:

  • The pattern is every top-level item after the first comma and before any top-level if guard. Literals at any nesting depth inside those items count, so Some("a" | "b") counts.
  • The matches! invocation is recognized in two forms:
    • a parsed macro_invocation named matches or with a path ending in it (std::matches!)
    • inside another macro's arguments, the tokens matches ! followed by an argument group, as in assert!(matches!(kind, Some("x")))

Stays clean:

  • Guard comparisons (matches!(k, Some(v) if v == "x")), which are unparsed tokens rather than a pattern.
  • Every other macro's arguments (assert_eq!(kind, "x")).

The Rust case-pattern syntax moved out of Rust.fs into the new module, so Rust.fs stays within the file-coherence function limit. The module is added to the .fsproj and the source guide.

Test plan

  • just test: 628 passed. The Rust magic string case patterns matrix row now has:
    • flagged: flagged_macro_pattern, flagged_nested_macro_pattern
    • clean: clean_macro_guard, clean_other_macro
    • these replace the former clean_macro_pattern limitation case
  • just lint, just md-lint
  • just analyze: no new findings compared with main
  • Real Rust workspace: 16 more magic-string findings, all spot-checked as genuine matches!/match repeats

🤖 Generated with Claude Code

tree-sitter leaves macro arguments as flat token trees, so `matches!`
string patterns never counted. The new RustPatterns module reads a
`matches!` argument tree token by token: a string literal counts when
its top-level item follows the first comma and precedes any `if`
guard, at any nesting depth. Both parsed invocations (`matches!`,
`std::matches!`) and `matches` `!` token sequences nested in another
macro, such as `assert!(matches!(...))`, are recognized.

Guard comparisons and other macros' arguments stay uninterpreted. The
Rust case-pattern syntax moves into RustPatterns.fs so Rust.fs stays
within the file-coherence function limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Energy State Diff vs origin/main

File Base Head Δ Status
src/Languages/Rust.fs 0 0 0 ⚪ unchanged
src/Languages/RustPatterns.fs — 0 — 🆕 new
tests/DetectorFixtureMatrixTests.fs 4 4 0 ⚪ unchanged

3 files changed, 0 worsened, 0 improved, 1 new.

@dbrattli
dbrattli merged commit 170d6cc into main Nov 22, 2026
7 checks passed
@dbrattli
dbrattli deleted the feat/magic-string-matches-macro branch November 22, 2026 11:10
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