Skip to content

chore(lint): enable perfsprint and usetesting, and apply their rewrites - #6982

Merged
otavio merged 1 commit into
masterfrom
chore/lint-idiom
Aug 28, 2026
Merged

chore(lint): enable perfsprint and usetesting, and apply their rewrites#6982
otavio merged 1 commit into
masterfrom
chore/lint-idiom

Conversation

@otavio

@otavio otavio commented Aug 28, 2026

Copy link
Copy Markdown
Member

What

Enables perfsprint and usetesting, and applies the ~200 rewrites they report.

Why

Third step of the lint expansion, after #6980 and #6981. Both linters carry autofixes, so almost every hunk here was written by the tool rather than by hand — review the diff, not the reasoning behind each line.

  • perfsprintfmt.Sprintf used where concatenation or strconv does the job, fmt.Errorf with no format verbs where errors.New belongs, and fmt.Sprintf("%x", b) where hex.EncodeToString is both clearer and faster.
  • usetestingt.Setenv, t.Context() and t.TempDir() over the hand-rolled equivalents.

Changes

One file needed hands. pkg/envs/envs_test.go set the environment through a before closure and undid it through a matching after, neither of which takes a *testing.T, so the fix could not be mechanical. Those cases now take one and call t.Setenv, which restores the previous value when the subtest ends. That is exactly what the after closures did by hand, so they are gone — 4 test tables, ~40 lines of teardown deleted.

Everything else is the linter's output, plus one tidy-up: perfsprint left five "/api/devices/" + "3a47..." pairs of adjacent literals, which I merged.

Not included

dupword was in this group and is deliberately left out. Its autofix corrupts test fixtures, because in fixture data the repetition is the data. Applied to this tree it:

  • rewrote a /proc/self/mountinfo sample in pkg/dockerutils/utils_test.go, stripping the filesystem type where the format legitimately repeats it (- overlay overlay rw,... became - overlay rw,...)
  • turned VALUES (..., now(), now(), ...) into now() in three migrations_test.go inserts, changing the statement's column count, and mangled a string concatenation into ,"+ownerID+' in the process

Both would have compiled. Neither is a change anyone would approve on purpose.

Testing

Under golang:1.26.7-alpine3.24:

  • golangci-lint run ./... reports 0 issues for all six modules
  • go build ./... clean everywhere, plus agent under -tags docker and -tags native
  • go test ./... passes for root, server, and agent under -tags docker
  • go mod tidy is a no-op

https://claude.ai/code/session_01D4BSojj4fmh3ZQSZWkGbD8

@otavio
otavio requested review from a team as code owners August 28, 2026 20:09
@github-actions

github-actions Bot commented Aug 28, 2026

Copy link
Copy Markdown

Claude finished @otavio's task in 1m 28s —— View job


Code Review Complete

Reviewed 24 files across code quality, security, testing, language patterns, and architecture — no new issues found. The code looks good as-is.

This is a mechanical perfsprint + usetesting autofix pass, and the rewrites hold up:

  • Import consistency — spot-checked the files where an autofix can strand or drop a fmt/errors import (server/ssh/web/session.go, server/ssh/session/session.go, server/ssh/web/errors.go, server/ssh/session/errors.go, pkg/revdial/revdial.go, agent/server/session.go). All consistent — fmt is retained only where still used, errors is imported everywhere it's now referenced.
  • Semantic equivalencehex.EncodeToString(buf)fmt.Sprintf("%x", buf) for []byte, strconv.Itoa(version)fmt.Sprintf("%d", version) for int, and the errors.New conversions preserve errors.Is identity semantics for the package-level Err* vars (no %w was present in any of them).
  • envs_test.go t.Setenv conversion — safe: none of these tests call t.Parallel(), and per-subtest restore is actually stricter than the hand-rolled after closures it replaces (some of which unset the wrong keys).
  • tests/ssh_test.goos.CreateTemp(t.TempDir(), ...) is correct; the leftover defer os.Remove(...) is now redundant but harmless.

Good call excluding dupword — its autofix genuinely corrupts fixture data, and the reasoning in the description is sound.


To request another review round, comment /review.
· chore/lint-idiom

Both carry autofixes, so almost every hunk here was written by the linter: Sprintf calls
that only concatenate become concatenation, Errorf with no verbs becomes errors.New, and
%x over a byte slice becomes hex.EncodeToString.

pkg/envs was the one that needed hands. Its table-driven cases set the environment through
a before closure and undid it through a matching after, neither of which takes a *testing.T,
so the fix could not be mechanical. The cases now take one and call t.Setenv, which restores
the previous value when the subtest ends — the teardown the after closures were doing by
hand, so they are gone.

dupword was in this group and is left out. Its autofix rewrites duplicated tokens inside
test fixtures, where the repetition is the data: it stripped the filesystem type from a
mountinfo sample and turned "now(), now()" in a SQL insert into a single call, changing the
statement's column count.
@otavio
otavio merged commit 7c3003a into master Aug 28, 2026
43 checks passed
@otavio
otavio deleted the chore/lint-idiom branch August 28, 2026 20:21
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