Skip to content

Commit 439c8ba

Browse files
authored
Merge pull request #2016 from alexandear/enable-gocritic
Enable gocritic linter and fix issues
2 parents ad4d0ee + 7b163fb commit 439c8ba

File tree

28 files changed

+81
-127
lines changed

28 files changed

+81
-127
lines changed

.golangci.yml

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
---
1919
run:
2020
concurrency: 6
21-
deadline: 5m
2221
linters:
2322
disable-all: true
2423
enable:
@@ -47,7 +46,7 @@ linters:
4746
# - gochecknoinits
4847
# - gocognit
4948
# - goconst
50-
# - gocritic
49+
- gocritic
5150
# - gocyclo
5251
# - godot
5352
# - godox
@@ -83,76 +82,29 @@ linters:
8382
# - wsl
8483
linters-settings:
8584
gocritic:
86-
enabled-checks:
87-
# Diagnostic
88-
- appendAssign
89-
- argOrder
90-
- badCond
91-
- caseOrder
92-
- codegenComment
93-
- commentedOutCode
94-
- deprecatedComment
95-
- dupArg
96-
- dupBranchBody
97-
- dupCase
98-
- dupSubExpr
99-
- exitAfterDefer
100-
- flagDeref
101-
- flagName
102-
- nilValReturn
103-
- offBy1
104-
- sloppyReassign
105-
- weakCond
106-
- octalLiteral
107-
108-
# Performance
85+
# See "Tags" section in https://github.com/go-critic/go-critic#usage
86+
enabled-tags:
87+
- diagnostic
88+
- performance
89+
- style
90+
- opinionated
91+
- experimental
92+
disabled-checks:
10993
- appendCombine
110-
- equalFold
111-
- hugeParam
112-
- indexAlloc
113-
- rangeExprCopy
94+
- sloppyReassign
95+
- unlabelStmt
11496
- rangeValCopy
115-
116-
# Style
117-
- assignOp
118-
- boolExprSimplify
119-
- captLocal
120-
- commentFormatting
121-
- commentedOutImport
122-
- defaultCaseOrder
123-
- docStub
124-
- elseif
125-
- emptyFallthrough
126-
- emptyStringTest
127-
- hexLiteral
97+
- hugeParam
98+
- importShadow
12899
- ifElseChain
129-
- methodExprCall
130-
- regexpMust
131-
- singleCaseSwitch
132-
- sloppyLen
133-
- stringXbytes
134-
- switchTrue
135-
- typeAssertChain
136-
- typeSwitchVar
137-
- underef
138-
- unlabelStmt
139-
- unlambda
140-
- unslice
141-
- valSwap
142-
- wrapperFunc
143-
- yodaStyleExpr
144-
145-
# Opinionated
100+
- sprintfQuotedString
146101
- builtinShadow
147-
- importShadow
148-
- initClause
149-
- nestingReduce
150-
- paramTypeCombine
151-
- ptrToRefParam
152-
- typeUnparen
153-
- unnamedResult
154-
- unnecessaryBlock
102+
- filepathJoin
155103
issues:
104+
# Maximum issues count per one linter.
105+
max-issues-per-linter: 0
106+
# Maximum count of issues with the same text.
107+
max-same-issues: 0
156108
exclude-rules:
157109
# Allow using Uid, Gid in pkg/osutil.
158110
- path: "pkg/osutil/"

cmd/limactl/gendoc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ weight: 3
117117
}
118118

119119
// replaceAll replaces all occurrences of new with old, for all files in dir
120-
func replaceAll(dir string, old, new string) error {
120+
func replaceAll(dir, old, new string) error {
121121
logrus.Infof("Replacing %q with %q", old, new)
122122
return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
123123
if err != nil {
@@ -133,7 +133,7 @@ func replaceAll(dir string, old, new string) error {
133133
if err != nil {
134134
return err
135135
}
136-
out := bytes.Replace(in, []byte(old), []byte(new), -1)
136+
out := bytes.ReplaceAll(in, []byte(old), []byte(new))
137137
err = os.WriteFile(path, out, 0o644)
138138
if err != nil {
139139
return err

pkg/cidata/cidata.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,11 @@ func writeCIDataDir(rootPath string, layout []iso9660util.Entry) error {
437437
if err != nil {
438438
return err
439439
}
440-
defer f.Close()
441440
if _, err := io.Copy(f, e.Reader); err != nil {
441+
_ = f.Close()
442442
return err
443443
}
444+
_ = f.Close()
444445
}
445446

446447
return nil

pkg/downloader/downloader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
291291
// cacheDirectoryPath returns the cache subdirectory path.
292292
// - "url" file contains the url
293293
// - "data" file contains the data
294-
func cacheDirectoryPath(cacheDir string, remote string) string {
294+
func cacheDirectoryPath(cacheDir, remote string) string {
295295
return filepath.Join(cacheDir, "download", "by-url-sha256", fmt.Sprintf("%x", sha256.Sum256([]byte(remote))))
296296
}
297297

@@ -380,7 +380,7 @@ func Decompressor(ext string) ([]string, bool) {
380380
return []string{program, "-d"}, true
381381
}
382382

383-
func decompressLocal(dst, src, ext string, description string) error {
383+
func decompressLocal(dst, src, ext, description string) error {
384384
command, found := Decompressor(ext)
385385
if !found {
386386
return fmt.Errorf("decompressLocal: unknown extension %s", ext)
@@ -472,7 +472,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
472472
return nil
473473
}
474474

475-
func downloadHTTP(localPath, url string, description string, expectedDigest digest.Digest) error {
475+
func downloadHTTP(localPath, url, description string, expectedDigest digest.Digest) error {
476476
if localPath == "" {
477477
return fmt.Errorf("downloadHTTP: got empty localPath")
478478
}

pkg/executil/command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type options struct {
1616
type Opt func(*options) error
1717

1818
// WithContext runs the command with CommandContext.
19+
//
20+
//nolint:gocritic // consider `ctx' to be of non-pointer type
1921
func WithContext(ctx *context.Context) Opt {
2022
return func(o *options) error {
2123
o.ctx = ctx

pkg/guestagent/guestagent_linux.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ func (a *agent) setWorthCheckingIPTablesRoutine(auditClient *libaudit.AuditClien
121121
logrus.Error(err)
122122
continue
123123
}
124-
switch msg.Type {
125-
case auparse.AUDIT_NETFILTER_CFG:
124+
if msg.Type == auparse.AUDIT_NETFILTER_CFG {
126125
a.worthCheckingIPTablesMu.Lock()
127126
logrus.Debug("setWorthCheckingIPTablesRoutine(): setting to true")
128127
a.worthCheckingIPTables = true

pkg/guestagent/procnettcp/procnettcp_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ func ParseFiles() ([]Entry, error) {
2020
}
2121
return res, err
2222
}
23-
defer r.Close()
2423
parsed, err := Parse(r, kind)
2524
if err != nil {
25+
_ = r.Close()
2626
return res, err
2727
}
28+
_ = r.Close()
2829
res = append(res, parsed...)
2930
}
3031
return res, nil

pkg/hostagent/hostagent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
665665
return nil
666666
}
667667

668-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string, verb string, reverse bool) error {
668+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string, reverse bool) error {
669669
args := sshConfig.Args()
670670
args = append(args,
671671
"-T",
@@ -732,7 +732,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
732732
}
733733
} else {
734734
logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local)
735-
if removeErr := os.RemoveAll(local); err != nil {
735+
if removeErr := os.RemoveAll(local); removeErr != nil {
736736
logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local)
737737
}
738738
}

pkg/hostagent/mount.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func (a *HostAgent) setupMount(m limayaml.Mount) (*mount, error) {
4747
// NOTE: allow_other requires "user_allow_other" in /etc/fuse.conf
4848
sshfsOptions := "allow_other"
4949
if !*m.SSHFS.Cache {
50-
sshfsOptions = sshfsOptions + ",cache=no"
50+
sshfsOptions += ",cache=no"
5151
}
5252
if *m.SSHFS.FollowSymlinks {
53-
sshfsOptions = sshfsOptions + ",follow_symlinks"
53+
sshfsOptions += ",follow_symlinks"
5454
}
5555
logrus.Infof("Mounting %q on %q", location, mountPoint)
5656

pkg/hostagent/port.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func hostAddress(rule limayaml.PortForward, guest api.IPPort) string {
4242
return host.String()
4343
}
4444

45-
func (pf *portForwarder) forwardingAddresses(guest api.IPPort, localUnixIP net.IP) (string, string) {
45+
func (pf *portForwarder) forwardingAddresses(guest api.IPPort, localUnixIP net.IP) (hostAddr, guestAddr string) {
4646
if pf.vmType == limayaml.WSL2 {
4747
guest.IP = localUnixIP
4848
host := api.IPPort{

0 commit comments

Comments
 (0)