Skip to content

Golangci v2 #6109

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

Merged
merged 22 commits into from
Apr 8, 2025
Merged

Golangci v2 #6109

merged 22 commits into from
Apr 8, 2025

Conversation

kolyshkin
Copy link
Contributor

@kolyshkin kolyshkin commented Apr 3, 2025

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

High-level overview

  • Install golangci-lint from binary; use renovate to update
  • Fix linter warnings found by golangci-lint v2.0.2 (the biggest part)
  • Switch to golangci-lint v2 (new config)
  • Fix "unused" linter warning, add linter
  • Fix "nolintlint" linter warnings, add linter
  • Add golangci-lint run with --tests=false (helps find issues like cmd/buildah: rm unused code #6101)

How to verify it

Existing tests should be adequate.

Which issue(s) this PR fixes:

None

Special notes for your reviewer:

Does this PR introduce a user-facing change?

none

@openshift-ci openshift-ci bot added do-not-merge/work-in-progress kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Apr 3, 2025
@kolyshkin kolyshkin force-pushed the golangci-v2 branch 2 times, most recently from c4629e6 to b6c8472 Compare April 7, 2025 18:20
kolyshkin added 22 commits April 7, 2025 13:01
This way is recommended by golangci-lint developers, plus we'll save
some build time.

In addition, add GOLANGCI_LINT_VERSION to the top-level Makefile,
so it can be updated by renovate.

Signed-off-by: Kir Kolyshkin <[email protected]>
This fixes the following warnings:

> run_linux.go:1065:3: redefines-builtin-id: redefinition of the built-in function max (revive)
> 		max := define.RLimitDefaultValue
> 		^
> run_linux.go:1069:5: redefines-builtin-id: redefinition of the built-in function max (revive)
> 				max = rlimit.Max
> 				^
> run_linux.go:1077:3: redefines-builtin-id: redefinition of the built-in function max (revive)
> 		max := define.RLimitDefaultValue
> 		^

Signed-off-by: Kir Kolyshkin <[email protected]>
> imagebuildah/stage_executor.go:714:13: superfluous-else: if block ends with a break statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
> 					} else {
> 						// Treat the source's name as the name of an image.
> 						mountPoint, err := s.getImageRootfs(s.ctx, from)
> 						if err != nil {
> 							return nil, fmt.Errorf("%s from=%s: no stage or image found with that name", flag, from)
> 						}
> 						stageMountPoints[from] = internal.StageMountDetails{
> 							IsImage:    true,
> 							DidExecute: true,
> 							MountPoint: mountPoint,
> 						}
> 						break
> 					}
>

(The alternative is to keep "else" and remove "break", but there are
other break statements above it, so for style consistency it's better to
keep using break.

Signed-off-by: Kir Kolyshkin <[email protected]>
These:

> add.go:457:2: QF1007: could merge conditional assignment into variable declaration (staticcheck)
> 	newDestDirFound := false
> 	^
> cmd/buildah/umount.go:33:2: QF1007: could merge conditional assignment into variable declaration (staticcheck)
> 	umountAll := false
> 	^

Signed-off-by: Kir Kolyshkin <[email protected]>
This fixes the following warnings:

> chroot/run_test.go:319:39: QF1001: could apply De Morgan's law (staticcheck)
> 				if limit == unix.RLIM_INFINITY && !(rlim == nil || (rlim.Soft == unix.RLIM_INFINITY && rlim.Hard == unix.RLIM_INFINITY)) {
> 				                                  ^
> copier/copier.go:1012:92: QF1001: could apply De Morgan's law (staticcheck)
> 			if target, err := os.Readlink(filepath.Join(workingPath, components[0])); err == nil && !(len(components) == 1 && !evaluateFinalComponent) {
> 			                                                                                        ^
> run_linux.go:459:118: QF1001: could apply De Morgan's law (staticcheck)
> 	if !slices.Contains(volumes, resolvconf.DefaultResolvConf) && options.ConfigureNetwork != define.NetworkDisabled && !(len(b.CommonBuildOpts.DNSServers) == 1 && strings.ToLower(b.CommonBuildOpts.DNSServers[0]) == "none") {
> 	                                                                                                                    ^

Signed-off-by: Kir Kolyshkin <[email protected]>
These:

> copier/copier.go:1664:2: QF1003: could use tagged switch on hdr.Typeflag (staticcheck)
> 	if hdr.Typeflag == tar.TypeReg {
> 	^
> pkg/parse/parse.go:708:4: QF1003: could use tagged switch on arr[1] (staticcheck)
> 			if arr[1] == "local" {
> 			^

Signed-off-by: Kir Kolyshkin <[email protected]>
These:

> cmd/buildah/images.go:192:10: QF1004: could use strings.ReplaceAll instead (staticcheck)
> 		return strings.Replace(opts.format, `\t`, "\t", -1)
> 		       ^
> pkg/formats/formats.go:97:16: QF1004: could use strings.ReplaceAll instead (staticcheck)
> 		t.Template = strings.Replace(strings.TrimSpace(t.Template[5:]), " ", "\t", -1)
> 		             ^
> tests/testreport/testreport.go:328:13: QF1004: could use strings.ReplaceAll instead (staticcheck)
> 		sysctl := strings.Replace(path, "/", ".", -1)
> 		          ^

Signed-off-by: Kir Kolyshkin <[email protected]>
This one:

> tests/serve/serve.go:56:16: QF1012: Use fmt.Fprintf(...) instead of WriteString(fmt.Sprintf(...)) (staticcheck)
> 		if _, err := f.WriteString(fmt.Sprintf("%d", os.Getpid())); err != nil {
> 		             ^

Instead of following the (decent) recommendation, use os.WriteFile
and replace printf(%d) with strconv.Itoa.

Signed-off-by: Kir Kolyshkin <[email protected]>
These:

> tests/conformance/conformance_test.go:1361:16: QF1012: Use fmt.Fprintf(...) instead of Write([]byte(fmt.Sprintf(...))) (staticcheck)
> 		if _, err := tw.Write([]byte(fmt.Sprintf("Field\tDocker\t%s\n", notDocker))); err != nil {
> 		             ^
> tests/conformance/conformance_test.go:1393:16: QF1012: Use fmt.Fprintf(...) instead of Write([]byte(fmt.Sprintf(...))) (staticcheck)
> 		if _, err := tw.Write([]byte(fmt.Sprintf("File:attr\tDocker\t%s\n", notDocker))); err != nil {
> 		             ^

Signed-off-by: Kir Kolyshkin <[email protected]>
This fixes "ST1005: error strings should not be capitalized (staticcheck)" warnings

Signed-off-by: Kir Kolyshkin <[email protected]>
This fixes "ST1005: error strings should not be capitalized
(staticcheck)" warnings.

Signed-off-by: Kir Kolyshkin <[email protected]>
This fixes the following warning:

> cmd/buildah/manifest.go:29:2: ST1019: package "github.com/opencontainers/image-spec/specs-go/v1" is being imported more than once (staticcheck)
> 	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
> 	^
> cmd/buildah/manifest.go:30:2: ST1019(related information): other import of "github.com/opencontainers/image-spec/specs-go/v1" (staticcheck)
> 	v1 "github.com/opencontainers/image-spec/specs-go/v1"
> 	^

Fixes: aca884a ("`buildah manifest`: add artifact-related options")
Signed-off-by: Kir Kolyshkin <[email protected]>
This fixes the following warning:

> tests/conformance/conformance_test.go:37:2: ST1019: package "github.com/containers/image/v5/storage" is being imported more than once (staticcheck)
> 	is "github.com/containers/image/v5/storage"
> 	^
> tests/conformance/conformance_test.go:38:2: ST1019(related information): other import of "github.com/containers/image/v5/storage" (staticcheck)
> 	istorage "github.com/containers/image/v5/storage"
> 	^

Fixes: 3a61cc0 ("Add OverrideChanges and OverrideConfig to CommitOptions")
Signed-off-by: Kir Kolyshkin <[email protected]>
Disable warnings like this one:

> internal/mkcw/workload.go:34:2: ST1003: should not use ALL_CAPS in Go names; use CamelCase instead (staticcheck)
> 	SEV_NO_ES = types.SEV_NO_ES //revive:disable-line:var-naming
> 	^

(

Signed-off-by: Kir Kolyshkin <[email protected]>
Also, add a way to update golangci-lint locally, if an old version is
installed.

Signed-off-by: Kir Kolyshkin <[email protected]>
There is some code in tests/conformance which is only used by tests.
Move it to *_test.go files.

Found by golangci-lint run --tests=false, which shows this warning:

> tests/conformance/selinux.go:9:6: func `selinuxMountFlag` is unused (unused)
> func selinuxMountFlag() string {
>      ^

Signed-off-by: Kir Kolyshkin <[email protected]>
Found when running golangci-lint with --tests=false, which results in:

> copier/syscall_unix.go:89:2: const `testModeMask` is unused (unused)
> 	testModeMask           = int64(os.ModePerm)
> 	^
> copier/syscall_unix.go:90:2: const `testIgnoreSymlinkDates` is unused (unused)
> 	testIgnoreSymlinkDates = false
> 	^

Signed-off-by: Kir Kolyshkin <[email protected]>
When running golangci-lint run --tests=false, it complains:

> chroot/seccomp.go:15:7: const `seccompAvailable` is unused (unused)
> const seccompAvailable = true
>       ^
> chroot/seccomp.go:182:6: func `setupSeccomp` is unused (unused)
> func setupSeccomp(spec *specs.Spec, seccompProfilePath string) error {
>      ^

Fix this.

Signed-off-by: Kir Kolyshkin <[email protected]>
This function always returns nil as the first parameter, which makes
unparam linter sad.

Rather than adding //nolint:unparam, let's just move nil to actual
returns.

Signed-off-by: Kir Kolyshkin <[email protected]>
This helps to find out code which is unused except in its own self
tests. For example, see PR 6101.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin kolyshkin marked this pull request as ready for review April 7, 2025 20:03
Copy link
Member

@nalind nalind left a comment

Choose a reason for hiding this comment

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

I forget if we ever learned the name of De Morgan's Law. Thanks for breaking this up into easier-to-review bits! LGTM

Copy link
Contributor

openshift-ci bot commented Apr 7, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: kolyshkin, nalind

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved label Apr 7, 2025
Copy link
Member

@Honny1 Honny1 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.


I have a non-blocking question about the chroot: fix unused warnings commit. Why are these functions only moved to *_test.go files? Is this some sort of false positive?

@rhatdan
Copy link
Member

rhatdan commented Apr 8, 2025

/lgtm

@openshift-ci openshift-ci bot added the lgtm label Apr 8, 2025
@openshift-merge-bot openshift-merge-bot bot merged commit bb240a6 into containers:main Apr 8, 2025
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants