fix(rules): expect_in_hot_path must not fire inside one-time initialisers - #659
Conversation
…sers
The rule is named 'in hot path' but had no concept of one:
pattern: ~r/\.expect\(/
a bare substring match over the whole file. It therefore fires on
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(..).expect(..));
which compiles exactly once on first access.
Measured on gitbot-fleet 2026-07-29: of 28 expect_in_hot_path findings, 11 were
this shape. The remediation those findings invite -- 'cache it with once_cell' --
describes code that already exists, so the finding could never be cleared by
following its own advice. That is worse than a miss: it spends reviewer trust.
Adds an opt-in field. Rules carrying it are matched
against a copy with lazy-initialiser bodies elided; every other rule is
unaffected. This follows the existing strip_inline_test_blocks/2 precedent
rather than inventing a second mechanism.
Verified against the real files, both directions:
deps.rs (all expects inside LazyLock) -> [] (was: flagged)
contrast.rs (per-call Regex::new in a fn) -> 6 occurrences still detected
So the rule keeps its teeth and loses its false alarms.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Note Automatic reviews are paused because your trial's included automatic processing has been used for this period. Upgrade now, or comment "Gitar review" to run a review anytime. Code Review ✅ Approved 2 resolved / 2 findingsRefines the expect_in_hot_path rule to ignore one-time initialisers using a runtime_path context. However, the regex-based parenthesis balancer fails on paren characters inside string literals, and automated test coverage for the new feature is missing. ✅ 2 resolved✅ Edge Case: Paren chars in string literals defeat lazy-initialiser stripping
✅ Quality: No tests for strip_lazy_initialisers / runtime_path context
OptionsDisplay: compact → Showing less information. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
The rule is named "in hot path" but had no concept of one —
pattern: ~r/\.expect\(/, a bare substring match over the whole file. So it fires on:which compiles exactly once on first access.
Measured on gitbot-fleet, 2026-07-29: of 28
expect_in_hot_pathfindings, 11 were this shape. The remediation those findings invite — "cache it with once_cell" — describes code that already exists, so the finding could never be cleared by following its own advice. That is worse than a miss: it spends reviewer trust, and it is the kind of noise that drives people to baseline a whole gate.Adds an opt-in
context: :runtime_pathfield. Rules carrying it match against a copy with lazy-initialiser bodies elided; every other rule is untouched. Follows the existingstrip_inline_test_blocks/2precedent rather than inventing a second mechanism.Verified against the real files, both directions:
deps.rs— everyexpectinsideLazyLock[]contrast.rs— genuine per-callRegex::newin a fnThe rule keeps its teeth and loses its false alarms.
Follow-on from #658. Two sibling imprecisions found in the same triage and not fixed here:
actions_expression_injectionfires when untrusted context is correctly bound viaenv:and never interpolated intorun:;secret_action_without_presence_gatefires when a presence gate is already present.🤖 Generated with Claude Code