Skip to content

Commit 14c10b1

Browse files
authored
Merge pull request #4051 from apostasie/ci-golangci-v2-post
[CI] golangci v2: review, fix, document, rules
2 parents 8337102 + 46c6af6 commit 14c10b1

File tree

13 files changed

+171
-145
lines changed

13 files changed

+171
-145
lines changed

Diff for: .golangci.yml

+147-112
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,36 @@ issues:
1010
linters:
1111
default: none
1212
enable:
13-
- depguard
13+
# 1. This is the default enabled set of golanci
14+
15+
# We should consider enabling errcheck
16+
# - errcheck
1417
- govet
1518
- ineffassign
16-
- misspell
17-
- nakedret
18-
- prealloc
19-
- revive
2019
- staticcheck
21-
- unconvert
2220
- unused
21+
22+
# 2. These are not part of the default set
23+
24+
# Important to prevent import of certain packages
25+
- depguard
26+
# Removes unnecessary conversions
27+
- unconvert
28+
# Flag common typos
29+
- misspell
30+
# A meta-linter seen as a good replacement for golint
31+
- revive
32+
# Gocritic
33+
- gocritic
34+
35+
# 3. We used to use these, but have now removed them
36+
37+
# Use of prealloc is generally premature optimization and performance profiling should be done instead
38+
# https://golangci-lint.run/usage/linters/#prealloc
39+
# - prealloc
40+
# Provided by revive in a better way
41+
# - nakedret
42+
2343
settings:
2444
staticcheck:
2545
checks:
@@ -31,93 +51,122 @@ linters:
3151
- "-ST1020"
3252
- "-ST1021"
3353
- "-ST1022"
34-
# FIXME: this below this point is disabled for now, but we should investigate
35-
- "-QF1008" # Omit embedded fields from selector expression https://staticcheck.dev/docs/checks#QF1008
36-
- "-QF1003" # Convert if/else-if chain to tagged switch https://staticcheck.dev/docs/checks#QF1003
37-
- "-QF1009" # Use time.Time.Equal instead of == operator https://staticcheck.dev/docs/checks#QF1009
38-
- "-QF1001" # Apply De Morgan’s law https://staticcheck.dev/docs/checks#QF1001
39-
- "-QF1012" # Use fmt.Fprintf(x, ...) instead of x.Write(fmt.Sprintf(...)) https://staticcheck.dev/docs/checks#QF1012
40-
- "-ST1005" # Expand call to math.Pow https://staticcheck.dev/docs/checks#QF1005
41-
- "-QF1004" # Use strings.ReplaceAll instead of strings.Replace with n == -1 https://staticcheck.dev/docs/checks#QF1004
54+
55+
##### TODO: fix and enable these
56+
# 4 occurrences.
57+
# Use fmt.Fprintf(x, ...) instead of x.Write(fmt.Sprintf(...)) https://staticcheck.dev/docs/checks#QF1012
58+
- "-QF1012"
59+
# 6 occurrences.
60+
# Apply De Morgan’s law https://staticcheck.dev/docs/checks#QF1001
61+
- "-QF1001"
62+
# 10 occurrences.
63+
# Convert if/else-if chain to tagged switch https://staticcheck.dev/docs/checks#QF1003
64+
- "-QF1003"
65+
66+
##### These have been vetted to be disabled.
67+
# 55 occurrences. Omit embedded fields from selector expression https://staticcheck.dev/docs/checks#QF1008
68+
# Usefulness is questionable.
69+
- "-QF1008"
70+
4271
revive:
4372
enable-all-rules: true
4473
rules:
4574
# See https://revive.run/r
46-
# Below are unsorted, and we might want to review them to decide which one we want to enable
47-
- name: exported
75+
76+
##### P0: we should do it ASAP.
77+
- name: max-control-nesting
78+
# 10 occurences (at default 5). Deep nesting hurts readibility.
79+
arguments: [7]
80+
- name: deep-exit
81+
# 11 occurrences. Do not exit in random places.
4882
disabled: true
49-
- name: add-constant
83+
- name: unchecked-type-assertion
84+
# 14 occurrences. This is generally risky and encourages bad coding for newcomers.
5085
disabled: true
51-
- name: cognitive-complexity
86+
- name: bare-return
87+
# 31 occurrences. Bare returns are just evil, very unfriendly, and make reading and editing much harder.
5288
disabled: true
53-
- name: package-comments
89+
- name: import-shadowing
90+
# 44 occurrences. Shadowing makes things prone to errors / confusing to read.
5491
disabled: true
55-
- name: cyclomatic
92+
- name: use-errors-new
93+
# 84 occurrences. Improves error testing.
5694
disabled: true
57-
- name: deep-exit
95+
96+
##### P1: consider making a dent on these, but not critical.
97+
- name: argument-limit
98+
# 4 occurrences (at default 8). Long windy arguments list for functions are hard to read. Use structs instead.
99+
arguments: [12]
100+
- name: unnecessary-stmt
101+
# 5 occurrences. Increase readability.
58102
disabled: true
59-
- name: function-length
103+
- name: defer
104+
# 7 occurrences. Confusing to read for newbies.
60105
disabled: true
61-
- name: flag-parameter
106+
- name: confusing-naming
107+
# 10 occurrences. Hurts readability.
62108
disabled: true
63-
- name: max-public-structs
109+
- name: early-return
110+
# 10 occurrences. Would improve readability.
64111
disabled: true
65-
- name: max-control-nesting
112+
- name: function-result-limit
113+
# 12 occurrences (at default 3). A function returning many results is probably too big.
114+
arguments: [7]
115+
- name: function-length
116+
# 155 occurrences (at default 0, 75). Really long functions should really be broken up in most cases.
117+
arguments: [0, 400]
118+
- name: cyclomatic
119+
# 204 occurrences (at default 10)
120+
arguments: [100]
121+
- name: unhandled-error
122+
# 222 occurrences. Could indicate failure to handle broken conditions.
66123
disabled: true
124+
- name: cognitive-complexity
125+
arguments: [197]
126+
# 441 occurrences (at default 7). We should try to lower it (involves significant refactoring).
127+
128+
##### P2: nice to have.
129+
- name: max-public-structs
130+
# 7 occurrences (at default 5). Might indicate overcrowding of public API.
131+
arguments: [21]
67132
- name: confusing-results
133+
# 13 occurrences. Have named returns when the type stutters.
134+
# Makes it a bit easier to figure out function behavior just looking at signature.
68135
disabled: true
69-
- name: nested-structs
70-
disabled: true
71-
- name: import-alias-naming
72-
disabled: true
73-
- name: filename-format
136+
- name: comment-spacings
137+
# 50 occurrences. Makes code look less wonky / ease readability.
74138
disabled: true
75139
- name: use-any
76-
disabled: true
77-
# FIXME: we should enable these below
78-
- name: struct-tag
79-
disabled: true
80-
- name: redundant-import-alias
140+
# 30 occurrences. `any` instead of `interface{}`. Cosmetic.
81141
disabled: true
82142
- name: empty-lines
143+
# 85 occurrences. Makes code look less wonky / ease readability.
83144
disabled: true
84-
- name: unhandled-error
85-
disabled: true
86-
- name: confusing-naming
87-
disabled: true
88-
- name: unused-parameter
89-
disabled: true
90-
- name: unused-receiver
91-
disabled: true
92-
- name: import-shadowing
93-
disabled: true
94-
- name: use-errors-new
95-
disabled: true
96-
- name: argument-limit
97-
disabled: true
98-
- name: time-equal
99-
disabled: true
100-
- name: defer
101-
disabled: true
102-
- name: early-return
103-
disabled: true
104-
- name: comment-spacings
145+
- name: package-comments
146+
# 100 occurrences. Better for documentation...
105147
disabled: true
106-
- name: function-result-limit
148+
- name: exported
149+
# 577 occurrences. Forces documentation of any exported symbol.
107150
disabled: true
108-
- name: unexported-naming
151+
152+
###### Permanently disabled. Below have been reviewed and vetted to be unnecessary.
153+
- name: line-length-limit
154+
# Formatter `golines` takes care of this.
109155
disabled: true
110-
- name: unnecessary-stmt
156+
- name: nested-structs
157+
# 5 occurrences. Trivial. This is not that hard to read.
111158
disabled: true
112-
- name: if-return
159+
- name: flag-parameter
160+
# 52 occurrences. Not sure if this is valuable.
113161
disabled: true
114-
- name: unchecked-type-assertion
162+
- name: unused-parameter
163+
# 505 occurrences. A lot of work for a marginal improvement.
115164
disabled: true
116-
- name: bare-return
165+
- name: unused-receiver
166+
# 31 occurrences. Ibid.
117167
disabled: true
118-
# Below have been reviewed and disabled
119-
- name: line-length-limit
120-
# Better dealt with by formatter golines
168+
- name: add-constant
169+
# 2605 occurrences. Kind of useful in itself, but unacceptable amount of effort to fix
121170
disabled: true
122171

123172
depguard:
@@ -141,67 +190,53 @@ linters:
141190
- pkg: github.com/containerd/nerdctl/v2/cmd
142191
desc: pkg must not depend on any cmd files
143192
gocritic:
144-
enabled-checks:
193+
disabled-checks:
194+
# Below are normally enabled by default, but we do not pass
145195
- appendAssign
146-
- argOrder
147-
- badCond
148-
- caseOrder
149-
- codegenComment
150-
- commentedOutCode
151-
- deprecatedComment
152-
- dupArg
153-
- dupBranchBody
154-
- dupCase
155-
- dupSubExpr
156-
- exitAfterDefer
157-
- flagDeref
158-
- flagName
196+
- ifElseChain
197+
- unslice
198+
- badCall
199+
- assignOp
200+
- commentFormatting
201+
- captLocal
202+
- singleCaseSwitch
203+
- wrapperFunc
204+
- elseif
205+
- regexpMust
206+
enabled-checks:
207+
# Below used to be enabled, but we do not pass anymore
208+
# - paramTypeCombine
209+
# - octalLiteral
210+
# - unnamedResult
211+
# - equalFold
212+
# - sloppyReassign
213+
# - emptyStringTest
214+
# - hugeParam
215+
# - appendCombine
216+
# - stringXbytes
217+
# - ptrToRefParam
218+
# - commentedOutCode
219+
# - rangeValCopy
220+
# - methodExprCall
221+
# - yodaStyleExpr
222+
# - typeUnparen
223+
224+
# We enabled these and we pass
159225
- nilValReturn
160-
- offBy1
161-
- sloppyReassign
162226
- weakCond
163-
- octalLiteral
164-
- appendCombine
165-
- equalFold
166-
- hugeParam
167227
- indexAlloc
168228
- rangeExprCopy
169-
- rangeValCopy
170-
- assignOp
171229
- boolExprSimplify
172-
- captLocal
173-
- commentFormatting
174230
- commentedOutImport
175-
- defaultCaseOrder
176231
- docStub
177-
- elseif
178232
- emptyFallthrough
179-
- emptyStringTest
180233
- hexLiteral
181-
- ifElseChain
182-
- methodExprCall
183-
- regexpMust
184-
- singleCaseSwitch
185-
- sloppyLen
186-
- stringXbytes
187-
- switchTrue
188234
- typeAssertChain
189-
- typeSwitchVar
190-
- underef
191235
- unlabelStmt
192-
- unlambda
193-
- unslice
194-
- valSwap
195-
- wrapperFunc
196-
- yodaStyleExpr
197236
- builtinShadow
198237
- importShadow
199238
- initClause
200239
- nestingReduce
201-
- paramTypeCombine
202-
- ptrToRefParam
203-
- typeUnparen
204-
- unnamedResult
205240
- unnecessaryBlock
206241
exclusions:
207242
generated: disable
@@ -220,14 +255,14 @@ formatters:
220255
gofumpt:
221256
extra-rules: true
222257
golines:
223-
max-len: 100
258+
max-len: 500
224259
tab-len: 4
225260
shorten-comments: true
226261
enable:
227262
- gci
228263
- gofmt
229264
# We might consider enabling the following:
230265
# - gofumpt
231-
# - golines
266+
- golines
232267
exclusions:
233268
generated: disable

Diff for: cmd/nerdctl/container/container_update.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
3939
"github.com/containerd/nerdctl/v2/pkg/api/types"
4040
"github.com/containerd/nerdctl/v2/pkg/clientutil"
41-
nerdctlContainer "github.com/containerd/nerdctl/v2/pkg/cmd/container"
41+
nerdctlcontainer "github.com/containerd/nerdctl/v2/pkg/cmd/container"
4242
"github.com/containerd/nerdctl/v2/pkg/formatter"
4343
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
4444
"github.com/containerd/nerdctl/v2/pkg/infoutil"
@@ -358,7 +358,7 @@ func updateContainer(ctx context.Context, client *containerd.Client, id string,
358358
return err
359359
}
360360
if cmd.Flags().Changed("restart") && restart != "" {
361-
if err := nerdctlContainer.UpdateContainerRestartPolicyLabel(ctx, client, container, restart); err != nil {
361+
if err := nerdctlcontainer.UpdateContainerRestartPolicyLabel(ctx, client, container, restart); err != nil {
362362
return err
363363
}
364364
}

Diff for: cmd/nerdctl/inspect/inspect.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
"github.com/spf13/cobra"
2424

2525
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
26-
containerCmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/container"
26+
containercmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/container"
2727
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
28-
imageCmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/image"
28+
imagecmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/image"
2929
"github.com/containerd/nerdctl/v2/pkg/api/types"
3030
"github.com/containerd/nerdctl/v2/pkg/clientutil"
3131
"github.com/containerd/nerdctl/v2/pkg/cmd/container"
@@ -117,13 +117,13 @@ func inspectAction(cmd *cobra.Command, args []string) error {
117117
var containerInspectOptions types.ContainerInspectOptions
118118
if inspectImage {
119119
platform := ""
120-
imageInspectOptions, err = imageCmd.InspectOptions(cmd, &platform)
120+
imageInspectOptions, err = imagecmd.InspectOptions(cmd, &platform)
121121
if err != nil {
122122
return err
123123
}
124124
}
125125
if inspectContainer {
126-
containerInspectOptions, err = containerCmd.InspectOptions(cmd)
126+
containerInspectOptions, err = containercmd.InspectOptions(cmd)
127127
if err != nil {
128128
return err
129129
}

Diff for: pkg/api/types/image_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package types
1919
import (
2020
"io"
2121

22-
v1 "github.com/opencontainers/image-spec/specs-go/v1"
22+
"github.com/opencontainers/image-spec/specs-go/v1"
2323
)
2424

2525
// ImageListOptions specifies options for `nerdctl image list`.

0 commit comments

Comments
 (0)