Skip to content

Commit 8f5e5c4

Browse files
committed
fix(occurrence): remove dead nil guard in countPattern; add empty-token test
countPattern is only called when r.Pattern != nil (all callers guard it), so the nil check was dead code per the project's defensive-code rule (branches must be driveable red/green). Remove it and add a test for the empty-token path in countToken to reach 100% statement coverage.
1 parent 9748dc5 commit 8f5e5c4

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

internal/rules/occurrence/rule.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,8 @@ func (r *Rule) countToken(text string, ti int) int {
238238
return strings.Count(text, tok)
239239
}
240240

241-
// countPattern counts regexp matches in text.
241+
// countPattern counts regexp matches in text. Caller must ensure r.Pattern != nil.
242242
func (r *Rule) countPattern(text string) int {
243-
if r.Pattern == nil {
244-
return 0
245-
}
246243
return len(r.Pattern.FindAllStringIndex(text, -1))
247244
}
248245

internal/rules/occurrence/rule_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ func TestApplySettings_MinWrongType(t *testing.T) {
388388
assert.Contains(t, err.Error(), "min")
389389
}
390390

391+
func TestCheck_Paragraph_EmptyToken_NoDiagnostic(t *testing.T) {
392+
r := &Rule{}
393+
mustApply(t, r, map[string]any{"tokens": []any{""}, "max": 1, "count": "each"})
394+
// empty token always returns count 0 → within any max
395+
assert.Empty(t, r.Check(mustFile(t, "# T\n\nsome text.\n")))
396+
}
397+
391398
// --- paragraph scope: pattern with count=each ---
392399

393400
func TestCheck_Paragraph_PatternEach_ExceedsMax(t *testing.T) {

0 commit comments

Comments
 (0)