Skip to content

Commit 1043d55

Browse files
committed
review: fix double-scan, allocation regression, and test gaps
Four cleanup fixes from the round-2 code review pass: - requiredstructure/isPIOpenLine: eliminate redundant double-scan of the leading-space prefix (CountLeadingSpaces then TrimLeft on same raw). One TrimLeft now derives both the trimmed slice and the indent count via len difference. - orderedlistnumbering/checkListLines,checkItem: revert strconv.Itoa + string concatenation back to fmt.Sprintf. The concat form makes 3-4 allocations per diagnostic versus Sprintf's ~1 via its pooled pp. - astutil_test/TestCollectSectionHeadings_Memoized: add require.Len guard on h2 so a broken memoization path produces a clear assertion failure instead of a panic on h2[0]. - astutil_test/TestCountLeadingSpaces: add the space-then-tab boundary case (" \tabc" → 1) to the canonical unit test; the case existed in listindent/helpers_test.go but not in astutil's own test. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gj8AvSb7r12iqa7r3vYYof
1 parent 834b560 commit 1043d55

3 files changed

Lines changed: 6 additions & 6 deletions

File tree

internal/rules/astutil/astutil_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ func TestCollectSectionHeadings_Memoized(t *testing.T) {
534534
h1 := CollectSectionHeadings(f)
535535
h2 := CollectSectionHeadings(f)
536536
require.Len(t, h1, 3)
537+
require.Len(t, h2, 3)
537538
assert.Same(t, &h1[0], &h2[0],
538539
"repeated calls must return the cached slice")
539540
}
@@ -821,6 +822,7 @@ func TestCountLeadingSpaces(t *testing.T) {
821822
assert.Equal(t, 2, CountLeadingSpaces([]byte(" abc")))
822823
assert.Equal(t, 0, CountLeadingSpaces([]byte("")))
823824
assert.Equal(t, 3, CountLeadingSpaces([]byte(" ")))
825+
assert.Equal(t, 1, CountLeadingSpaces([]byte(" \tabc")))
824826
}
825827

826828
// --- IsBlank ---

internal/rules/orderedlistnumbering/rule.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ func (r *Rule) checkListLines(f *lint.File, listStart int, itemLines []int) []li
135135
}
136136
if i == 0 && startMismatch {
137137
diags = append(diags, r.diag(f, line,
138-
"ordered list starts at "+strconv.Itoa(listStart)+
139-
"; configured start is "+strconv.Itoa(r.Start)))
138+
fmt.Sprintf("ordered list starts at %d; configured start is %d", listStart, r.Start)))
140139
}
141140
if startMismatch {
142141
continue
@@ -163,8 +162,7 @@ func (r *Rule) checkItem(f *lint.File, line, i int) (lint.Diagnostic, bool) {
163162
return lint.Diagnostic{}, false
164163
}
165164
return r.diag(f, line,
166-
"ordered list item "+strconv.Itoa(i+1)+" numbered "+strconv.Itoa(literal)+
167-
"; expected "+strconv.Itoa(expected)), true
165+
fmt.Sprintf("ordered list item %d numbered %d; expected %d", i+1, literal, expected)), true
168166
}
169167

170168
func (r *Rule) diag(f *lint.File, line int, msg string) lint.Diagnostic {

internal/rules/requiredstructure/rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,10 +1475,10 @@ func fenceRun(line []byte, ch byte) int {
14751475
// (the bytes up to the first whitespace or `?>`). An indented code
14761476
// example showing a directive is therefore not mistaken for one.
14771477
func isPIOpenLine(raw []byte) bool {
1478-
if astutil.CountLeadingSpaces(raw) > 3 {
1478+
trimmed := bytes.TrimLeft(raw, " ")
1479+
if len(raw)-len(trimmed) > 3 {
14791480
return false
14801481
}
1481-
trimmed := bytes.TrimLeft(raw, " ")
14821482
trimmed = bytes.TrimRight(trimmed, " \t\r\n")
14831483
if !bytes.HasPrefix(trimmed, piOpenPrefix) {
14841484
return false

0 commit comments

Comments
 (0)