Skip to content

Apply go fix modernizations - #18

Merged
thunderkatz merged 7 commits into
masterfrom
go-fix-modernize
Nov 6, 2026
Merged

thunderkatz merged 7 commits into
masterfrom
go-fix-modernize

Conversation

@thunderkatz

@thunderkatz thunderkatz commented Nov 5, 2026 •

Copy link
Copy Markdown

Summary

Output of go fix ./... on Go 1.27.3 (plus a stray blank line removed from WriteHeader):

  • ioutil.ReadAll -> io.ReadAll
  • for i := 0; i < n; i++ -> for i := range n
  • manual clamp -> min()
  • reverse index loops -> slices.Backward
  • string += in a loop -> strings.Builder (mail/header.go)

Manual changes:

  • the remaining errors.As calls -> errors.AsType (by hand; go fix skips the new(T)/&T{} target forms)
  • bytes.IndexByte + slicing -> bytes.Cut (AddRaw, trimAroundNewlines, parseHeaderFieldLine), HasSuffix+TrimSuffix -> bytes.CutSuffix, sort.SliceStable -> slices.SortStableFunc (all skipped by go fix)
  • append(s[:i], s[i+1:]...) -> slices.Delete (also zeroes the vacated slot), b.N loops -> b.Loop(), last ioutil.Discard -> io.Discard
  • charset/charset.go: %v -> %w, so errors.Is/As see the underlying error. This is the one behavior-visible change.

multipart.go's NextPart: %v is intentionally left as %v: wrapping would make a truncated body satisfy errors.Is(err, io.EOF).

Test plan

  • go test ./...
  • go fix -diff ./... is empty afterwards

🤖 Generated with Claude Code

thunderkatz and others added 6 commits November 5, 2026 10:15
Run `go fix ./...` for Go 1.27: ioutil.ReadAll -> io.ReadAll,
integer range loops, min(), slices.Backward, and strings.Builder for
string concatenation in a loop.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
go fix skips these because they pass new(T) or &T{} as the target.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
go fix skips these: IndexByte followed by slicing around the index,
HasSuffix+TrimSuffix on bytes, and sort.SliceStable on a non-basic slice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- slices.Delete replaces append(s[:i], s[i+1:]...) and zeroes the vacated
  slot so the removed field can be collected.
- b.Loop replaces the b.N loops in the header benchmarks.
- io.Discard replaces the last ioutil use.
- charset errors wrap the underlying error with %w so errors.Is/As work.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@thunderkatz
thunderkatz marked this pull request as ready for review November 5, 2026 15:33
@thunderkatz
thunderkatz requested a balanced review from Copilot November 5, 2026 15:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The invalid-charset tests do not verify error unwrapping, leaving the intentional %w behavior unprotected against regression to %v.

1 open issue
What changed in this PR

Modernizes the message, mail, and textproto packages with current Go idioms and standard-library APIs. Charset lookup errors now expose their underlying cause; multipart EOF handling remains unchanged.

Changes:

  • Replace legacy I/O helpers, loops, sorting, and slice deletion patterns.
  • Use typed error matching and strings.Builder for comment parsing.
  • Wrap charset lookup errors and modernize benchmark loops.
File Description
textproto/​multipart.go Simplify read limits, replace discard helper, and document EOF handling.
textproto/​multipart_test.go Modernize I/O helpers and loop syntax.
textproto/​header.go Modernize sorting, byte splitting, reverse iteration, and deletion.
textproto/​bench_test.go Switch benchmarks to b.Loop().
mail/​reader_test.go Replace ioutil.ReadAll with io.ReadAll.
mail/​header.go Build parsed comments with strings.Builder.
entity_test.go Modernize I/O helpers and typed error assertions.
encoding.go Update error matching, loops, and suffix splitting.
encoding_test.go Replace the legacy read helper.
charset/​charset.go Preserve underlying charset lookup errors with %w.
charset/​charset_test.go Replace the legacy read helper.
charset.go Use typed matching for unknown-charset errors.

Review effort: Balanced

Comment thread charset/charset.go Resolved
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@thunderkatz
thunderkatz requested a review from a team November 5, 2026 15:48

@dlynch-sublime dlynch-sublime left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@thunderkatz
thunderkatz merged commit 3a4cd1f into master Nov 6, 2026
3 checks passed
@thunderkatz
thunderkatz deleted the go-fix-modernize branch November 6, 2026 15:29
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.

3 participants