[9.5] .golangci.yml: enable the modernize linter#51959
Open
orestisfl wants to merge 4 commits into
Open
Conversation
Enable the modernize linter from golangci-lint's suite. It flags
outdated Go syntax and rewrites it to modern equivalents: min/max
builtins, range-over-int loops, slices/maps helpers, any, t.Context()
in tests, fmt.Appendf, strings.CutPrefix, and more. Keeping it enabled
stops outdated patterns from re-entering the codebase, which matters
more now that much code is produced by AI agents trained on older Go,
and it often yields simpler, more efficient code.
The omitzero analyzer is disabled: it rewrites ,omitempty struct tags to
,omitzero, which changes JSON marshaling semantics and is not a
mechanical, behavior-preserving fix.
The loop also runs `go fix -inline`, which applies //go:fix inline
directives. modernize marks its own pointer helpers with the directive,
and dependencies/stdlib carry it on deprecated APIs, so this pulls in
mechanical migrations such as io/ioutil -> io and reflect.Ptr ->
reflect.Pointer. `go fix` inlines the call sites but never deletes the
now-dead helper declarations; a follow-up commit removes those.
Every Go change here is a linter/`go fix` auto-fix, restricted to the
files owned by @elastic/elastic-agent-data-plane; the rest of the
repository is modernized in a stacked follow-up PR so each team reviews
only its own code.
The .golangci.yml change is made by hand. The Go changes are then fully
reproducible by running this with bash from the repository root:
set -euo pipefail
OWNER="@elastic/elastic-agent-data-plane"
export CGO_ENABLED=1
MAX_PASSES=10
modernize() {
golangci-lint run --enable-only modernize \
--max-issues-per-linter 0 --max-same-issues 0 --timeout=90m --fix ./...
}
for pass in $(seq "$MAX_PASSES"); do
echo ">>> pass $pass: fmt"
before="$(git diff | git hash-object --stdin)"
golangci-lint fmt ./...
echo ">>> pass $pass: modernize --fix"
if modernize; then :; fi
echo ">>> pass $pass: go fix -inline"
if go fix -inline ./...; then :; fi
echo ">>> pass $pass done: $(git diff --name-only | wc -l) files changed"
[ "$before" = "$(git diff | git hash-object --stdin)" ] && { echo ">>> fixpoint at pass $pass"; break; }
done
golangci-lint fmt ./...
changed="$(mktemp)"
keep="$(mktemp)"
trap 'rm -f "$changed" "$keep"' EXIT
git diff --name-only -- '*.go' | sort -u >"$changed"
git diff --name-only -- '*.go' | xargs -r codeowners -o "$OWNER" \
| awk '{print $1}' | sort -u >"$keep"
comm -23 "$changed" "$keep" | tr '\n' '\0' | xargs -0 -r git checkout --
echo "PR1 data-plane .go files: $(git diff --name-only -- '*.go' | wc -l)"
modernize marks trivial pointer helpers (func f(v T) *T { return &v })
with //go:fix inline and rewrites the body to new(v). `go fix -inline`
then replaces every call site with new(...) but does not delete the
helper declaration, leaving it unused.
Remove those now-dead helpers so the unused linter stays clean. This is
the manual step `go fix` cannot do; all removed functions have zero
remaining callers.
Contributor
🤖 GitHub commentsJust comment with:
|
Member
|
@orestisfl, this PR does not change Was it intentional? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Manual backport of #51840 using the shell script: