-
Notifications
You must be signed in to change notification settings - Fork 225
Switch to golangci-lint v2, cleanup config, fix new linter warnings #2389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
||
| env: | ||
| LINT_VERSION: v1.64.8 | ||
| LINT_VERSION: v2.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we pin x.y.z here. Otherwise the linter might can get a ptach update that randomly breaks on a PR, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thinking was by omitting the patch version we have less noise PRs to upgrade, and from my experience with other repos I don't remember that patch bump results in any new warnings.
Of course YMMV, so let me know which way you like it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally I hate breakages on random PRs, in case of community contributors they might get confused and you then get to tell them to rebase, etc...
That said I agree if you have experience that it is rather stable I am fine to give that a go, my (bad) experiences about linter updates are from the fact that we had enable-all which of course causes new warnings very often but you already fixed that problem here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that enable-all is a PITA, and this is actually my second attempt at killing it (🤞🏻 it will stay this time).
Instead of enabling all linters, and when disabling some of them because we don't like them, switch to list of explicitly enabled linters. The upside of this is we can easily upgrade golangci-lint (i.e. any new linters won't break our CI). The downside is, we need to explicitly enable extra linters. To me, this is better from the maintainability perspective. NOTE this commit does not change the configuration in any way, in other words, the list of linters being run is the same as before. The next commit will address this. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Let's take a look at each enabled linter, and find out if it's needed; remove those that make no sense. * gci: formats imports, probably superseded by gofumpt; * gofmt: is a subset of gofumpt; * goheader: does nothing with empty configuration; * goimports: functionality should be covered by gofumpt; * gomodguard: does nothing with empty configuration; * grouper: formats imports, probably superseded by gofumpt; * importas: does nothing with empty configuration; * loggercheck: this repo does not use any loggers it checks (kitlog,klog,logr,zap); * promlinter: this repo does not use Prometheus; * protogetter: this repo does not use protobuf; * rowserrcheck: this repo does not use sql; * sloginit: this repo does not use slog; * spancheck: this repo does not use opentelemetry/opencensus; * sqlclosecheck: this repo does not use sql; * tagalign: this repo does not use multiple struct tags; * testableexamples: this repo does not have any examples; * zerologlint: this repo does not use zerolog. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fix the following warning, as reported by staticcheck from golangci-lint
v2.0.1:
> pkg/secrets/secrets_test.go:67:5: QF1009: probably want to use time.Time.Equal instead (staticcheck)
> if s.CreatedAt == s.UpdatedAt {
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fix the following staticcheck warning:
> pkg/cgroups/utils_linux.go:264:18: QF1012: Use fmt.Fprintf(...) instead of WriteString(fmt.Sprintf(...)) (staticcheck)
> if _, err := f.WriteString(fmt.Sprintf("%d\n", pid)); err != nil {
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fix the following staticcheck warning:
> libnetwork/slirp4netns/slirp4netns.go:685:15: QF1012: Use fmt.Fprintf(...) instead of Write([]byte(fmt.Sprintf(...))) (staticcheck)
> if _, err := conn.Write([]byte(fmt.Sprintf("%s\n", data))); err != nil {
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following staticcheck warning:
> pkg/timezone/timezone.go:23:2: QF1002: could use tagged switch on timezone (staticcheck)
> switch {
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
... and add a deprecated alias so backward compatibility is still preserved (and users can gradually switch to the new name). Done because this is now also reported by staticcheck (in addition to revive) linter. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following staticcheck warning: > libnetwork/netavark/config.go:297:2: QF1007: could merge conditional assignment into variable declaration (staticcheck) > isMacVlan := true > ^ Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fix the following staticcheck warning: > pkg/timetype/timestamp.go:35:21: QF1001: could apply De Morgan's law (staticcheck) > parseInLocation := !(strings.ContainsAny(value, "zZ+") || strings.Count(value, "-") == 3) > ^ While at it, improve the comment. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following staticcheck warnings:
> pkg/configmaps/configmaps.go:135:5: QF1001: could apply De Morgan's law (staticcheck)
> if !(len(data) > 0 && len(data) < maxConfigMapSize) {
> ^
> pkg/secrets/secrets.go:169:5: QF1001: could apply De Morgan's law (staticcheck)
> if !(len(data) > 0 && len(data) < maxSecretSize) {
> ^
as well as the subsequent gocritic warnings:
> pkg/configmaps/configmaps.go:135:5: sloppyLen: len(data) <= 0 can be len(data) == 0 (gocritic)
> if len(data) <= 0 || len(data) >= maxConfigMapSize {
> ^
> pkg/secrets/secrets.go:169:5: sloppyLen: len(data) <= 0 can be len(data) == 0 (gocritic)
> if len(data) <= 0 || len(data) >= maxSecretSize {
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following staticcheck warnings:
> libimage/image.go:463:5: QF1001: could apply De Morgan's law (staticcheck)
> if !(referencedBy == "" || numNames == 1) {
> ^
> libimage/normalize.go:33:5: QF1001: could apply De Morgan's law (staticcheck)
> if !(strings.ContainsAny(registry, ".:") || registry == "localhost") {
> ^
> libimage/search.go:220:6: QF1001: could apply De Morgan's law (staticcheck)
> if !(filterMatchesAutomatedFilter(&options.Filter, results[i]) && filterMatchesOfficialFilter(&options.Filter, results[i]) && filterMatchesStarFilter(&options.Filter, results[i])) {
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following staticcheck warning:
> pkg/cgroups/utils_linux.go:224:25: QF1001: could apply De Morgan's law (staticcheck)
> if parts[2] == "/" && !(unifiedMode && parts[1] == "") {
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following linter warnings:
> pkg/auth/auth.go:176:11: ST1005: error strings should not be capitalized (staticcheck)
> return errors.New("Can't specify both --password-stdin and --password")
> ^
> pkg/auth/auth.go:179:11: ST1005: error strings should not be capitalized (staticcheck)
> return errors.New("Must provide --username with --password-stdin")
> ^
> pkg/subscriptions/subscriptions.go:325:17: ST1005: error strings should not be capitalized (staticcheck)
> return false, fmt.Errorf("Container /etc resolution error: %w", err)
> ^
> pkg/subscriptions/subscriptions.go:325:17: ST1005: error strings should not be capitalized (staticcheck)
> return false, fmt.Errorf("Container /etc resolution error: %w", err)
> ^
> pkg/subscriptions/subscriptions.go:334:17: ST1005: error strings should not be capitalized (staticcheck)
> return false, fmt.Errorf("Container /etc/system-fips resolution error: %w", err)
> ^
> pkg/subscriptions/subscriptions.go:451:10: ST1005: error strings should not be capitalized (staticcheck)
> return fmt.Errorf("Could not expand %q in container: %w", srcPolicyConfig, err)
> ^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following linter warning: > pkg/retry/retry.go:50:4: SA4011: ineffective break statement. Did you mean to break out of the outer loop? (staticcheck) > break > ^ Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
The new configuration files were initially generated by `golangci-lint migrate`, when tweaked to minimize and simplify. golangci-lint v2 switches to a new version of staticcheck which shows much more warnings. Some of them were fixed by a few previous commits, and the rest of them are disabled. Also, golangci-extra was modified to include ALL staticcheck linters. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Luap99
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: kolyshkin, Luap99 The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/lgtm |
This can be logically split into 3 parts:
.golangci.yml: switch to list of enabled linters
Instead of enabling all linters, and when disabling some of them because
we don't like them, switch to list of explicitly enabled linters. The
upside of this is we can easily upgrade golangci-lint (i.e. any new
linters won't break our CI). The downside is, we need to explicitly
enable extra linters.
To me, this is better from the maintainability perspective.
NOTE this commit does not change the configuration in any way, in other
words, the list of linters being run is the same as before. The next
commit will address this.
PS this is my second attempt of doing this. The first one was in
commit 3bb7e63 ("Bump golangci-lint to 1.45.2", PR golangci-lint spring cleaning and bump #997),
which was reverted in b6cd136 ("golangci.yml: enable all,
disable failing linters"). I still think that enable-all is not sane.
.golangci.yml: remove some linters
This carefully remove linters that makes no sense but were previuously
enabled because of enable-all logic.
Questions / discussion
Whether "apply De Morgan's law" check should be enabled?
Personally, I am not convinced it always makes the code more readable. If others agree, I will remove the corresponding commits and instead just disable staticcheck's QF1001;
Can
pkg: do not capitalize error stringsresult in any regressions or test failures?Probably. If that's the case, I think we should introduce more typed errors.
The alternative, as in the previous case, is to disable staticcheck's ST1005.
Should the .z version of golangci-lint be specified?
Pros of having .z: more stability (no random CI breaks caused by unintended upgrades).
Cons: more upgrade "noisy" PRs.