Skip to content

Commit ce22ac2

Browse files
authored
Merge pull request #3770 from thaJeztah/update_golangci_lint_step2
golangci-lint: enable more linters
2 parents 8046bb2 + 30d36e9 commit ce22ac2

184 files changed

Lines changed: 1526 additions & 1466 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.golangci.yml

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@ linters:
33
- bodyclose
44
- depguard
55
- dogsled
6+
- dupword # Detects duplicate words.
7+
- durationcheck
8+
- errchkjson
9+
- exportloopref # Detects pointers to enclosing loop variables.
10+
- gocritic # Metalinter; detects bugs, performance, and styling issues.
611
- gocyclo
7-
- gofumpt
12+
- gofumpt # Detects whether code was gofumpt-ed.
813
- goimports
9-
- gosec
14+
- gosec # Detects security problems.
1015
- gosimple
1116
- govet
1217
- ineffassign
1318
- lll
1419
- megacheck
15-
- misspell
20+
- misspell # Detects commonly misspelled English words in comments.
1621
- nakedret
17-
- revive
22+
- nilerr # Detects code that returns nil even if it checks that the error is not nil.
23+
- nolintlint # Detects ill-formed or insufficient nolint directives.
24+
- perfsprint # Detects fmt.Sprintf uses that can be replaced with a faster alternative.
25+
- prealloc # Detects slice declarations that could potentially be pre-allocated.
26+
- predeclared # Detects code that shadows one of Go's predeclared identifiers
27+
- reassign
28+
- revive # Metalinter; drop-in replacement for golint.
1829
- staticcheck
30+
- stylecheck # Replacement for golint
31+
- tenv # Detects using os.Setenv instead of t.Setenv.
32+
- thelper # Detects test helpers without t.Helper().
33+
- tparallel # Detects inappropriate usage of t.Parallel().
1934
- typecheck
20-
- unconvert
35+
- unconvert # Detects unnecessary type conversions.
2136
- unparam
2237
- unused
38+
- usestdlibvars
39+
- vet
40+
- wastedassign
2341

2442
disable:
2543
- errcheck
@@ -40,13 +58,35 @@ linters-settings:
4058
gocyclo:
4159
min-complexity: 16
4260
govet:
43-
check-shadowing: false
61+
check-shadowing: true
62+
settings:
63+
shadow:
64+
strict: true
4465
lll:
4566
line-length: 200
4667
nakedret:
4768
command: nakedret
4869
pattern: ^(?P<path>.*?\\.go):(?P<line>\\d+)\\s*(?P<message>.*)$
4970

71+
revive:
72+
rules:
73+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#import-shadowing
74+
- name: import-shadowing
75+
severity: warning
76+
disabled: false
77+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#empty-block
78+
- name: empty-block
79+
severity: warning
80+
disabled: false
81+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#empty-lines
82+
- name: empty-lines
83+
severity: warning
84+
disabled: false
85+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#use-any
86+
- name: use-any
87+
severity: warning
88+
disabled: false
89+
5090
issues:
5191
# The default exclusion rules are a bit too permissive, so copying the relevant ones below
5292
exclude-use-default: false
@@ -83,7 +123,7 @@ issues:
83123
- gosec
84124
# EXC0008
85125
# TODO: evaluate these and fix where needed: G307: Deferring unsafe method "*os.File" on type "Close" (gosec)
86-
- text: "(G104|G307)"
126+
- text: "G307"
87127
linters:
88128
- gosec
89129
# EXC0009
@@ -97,10 +137,13 @@ issues:
97137

98138
# G113 Potential uncontrolled memory consumption in Rat.SetString (CVE-2022-23772)
99139
# only affects gp < 1.16.14. and go < 1.17.7
100-
- text: "(G113)"
140+
- text: "G113"
141+
linters:
142+
- gosec
143+
# TODO: G104: Errors unhandled. (gosec)
144+
- text: "G104"
101145
linters:
102146
- gosec
103-
104147
# Looks like the match in "EXC0007" above doesn't catch this one
105148
# TODO: consider upstreaming this to golangci-lint's default exclusion rules
106149
- text: "G204: Subprocess launched with a potential tainted input or cmd arguments"
@@ -125,6 +168,15 @@ issues:
125168
linters:
126169
- errcheck
127170
- gosec
171+
- text: "ST1000: at least one file in a package should have a package comment"
172+
linters:
173+
- stylecheck
174+
175+
# Allow "err" and "ok" vars to shadow existing declarations, otherwise we get too many false positives.
176+
- text: '^shadow: declaration of "(err|ok)" shadows declaration'
177+
linters:
178+
- govet
179+
128180

129181
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
130182
max-issues-per-linter: 0

cli-plugins/manager/candidate_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ func TestValidateCandidate(t *testing.T) {
7575
} {
7676
t.Run(tc.name, func(t *testing.T) {
7777
p, err := newPlugin(tc.c, fakeroot.Commands())
78-
if tc.err != "" {
78+
switch {
79+
case tc.err != "":
7980
assert.ErrorContains(t, err, tc.err)
80-
} else if tc.invalid != "" {
81+
case tc.invalid != "":
8182
assert.NilError(t, err)
8283
assert.Assert(t, cmp.ErrorType(p.Err, reflect.TypeOf(&pluginError{})))
8384
assert.ErrorContains(t, p.Err, tc.invalid)
84-
} else {
85+
default:
8586
assert.NilError(t, err)
8687
assert.Equal(t, NamePrefix+p.Name, goodPluginName)
8788
assert.Equal(t, p.SchemaVersion, "0.1.0")

cli-plugins/manager/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ func wrapAsPluginError(err error, msg string) error {
4343

4444
// NewPluginError creates a new pluginError, analogous to
4545
// errors.Errorf.
46-
func NewPluginError(msg string, args ...interface{}) error {
46+
func NewPluginError(msg string, args ...any) error {
4747
return &pluginError{cause: errors.Errorf(msg, args...)}
4848
}

cli-plugins/manager/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestListPluginCandidates(t *testing.T) {
4646
)
4747
defer dir.Remove()
4848

49-
var dirs []string
49+
dirs := make([]string, 0, 6)
5050
for _, d := range []string{"plugins1", "nonexistent", "plugins2", "plugins3", "plugins4", "plugins5"} {
5151
dirs = append(dirs, dir.Join(d))
5252
}

cli/command/checkpoint/formatter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const (
1212

1313
// NewFormat returns a format for use with a checkpoint Context
1414
func NewFormat(source string) formatter.Format {
15-
switch source {
16-
case formatter.TableFormatKey:
15+
if source == formatter.TableFormatKey {
1716
return defaultCheckpointFormat
1817
}
1918
return formatter.Format(source)

cli/command/cli.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (cli *DockerCli) CurrentContext() string {
394394
// occur when trying to use it.
395395
//
396396
// Refer to [DockerCli.CurrentContext] above for further details.
397-
func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigFile) string {
397+
func resolveContextName(opts *cliflags.ClientOptions, cfg *configfile.ConfigFile) string {
398398
if opts != nil && opts.Context != "" {
399399
return opts.Context
400400
}
@@ -407,9 +407,9 @@ func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigF
407407
if ctxName := os.Getenv(EnvOverrideContext); ctxName != "" {
408408
return ctxName
409409
}
410-
if config != nil && config.CurrentContext != "" {
410+
if cfg != nil && cfg.CurrentContext != "" {
411411
// We don't validate if this context exists: errors may occur when trying to use it.
412-
return config.CurrentContext
412+
return cfg.CurrentContext
413413
}
414414
return DefaultContextName
415415
}
@@ -514,7 +514,7 @@ func UserAgent() string {
514514
}
515515

516516
var defaultStoreEndpoints = []store.NamedTypeGetter{
517-
store.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }),
517+
store.EndpointTypeGetter(docker.DockerEndpoint, func() any { return &docker.EndpointMeta{} }),
518518
}
519519

520520
// RegisterDefaultStoreEndpoints registers a new named endpoint
@@ -528,7 +528,7 @@ func RegisterDefaultStoreEndpoints(ep ...store.NamedTypeGetter) {
528528
// DefaultContextStoreConfig returns a new store.Config with the default set of endpoints configured.
529529
func DefaultContextStoreConfig() store.Config {
530530
return store.NewConfig(
531-
func() interface{} { return &DockerContext{} },
531+
func() any { return &DockerContext{} },
532532
defaultStoreEndpoints...,
533533
)
534534
}

cli/command/cli_options_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
func contentTrustEnabled(t *testing.T) bool {
11+
t.Helper()
1112
var cli DockerCli
1213
assert.NilError(t, WithContentTrustFromEnv()(&cli))
1314
return cli.contentTrust

cli/command/config/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (c *configContext) Labels() string {
101101
if mapLabels == nil {
102102
return ""
103103
}
104-
var joinLabels []string
104+
joinLabels := make([]string, 0, len(mapLabels))
105105
for k, v := range mapLabels {
106106
joinLabels = append(joinLabels, k+"="+v)
107107
}

cli/command/config/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func RunConfigInspect(dockerCli command.Cli, opts InspectOptions) error {
4848
opts.Format = "pretty"
4949
}
5050

51-
getRef := func(id string) (interface{}, []byte, error) {
51+
getRef := func(id string) (any, []byte, error) {
5252
return client.ConfigInspectWithRaw(ctx, id)
5353
}
5454
f := opts.Format

cli/command/container/attach.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type AttachOptions struct {
2424
DetachKeys string
2525
}
2626

27-
func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, args string) (*types.ContainerJSON, error) {
28-
c, err := cli.ContainerInspect(ctx, args)
27+
func inspectContainerAndCheckState(ctx context.Context, apiClient client.APIClient, args string) (*types.ContainerJSON, error) {
28+
c, err := apiClient.ContainerInspect(ctx, args)
2929
if err != nil {
3030
return nil, err
3131
}
@@ -45,21 +45,21 @@ func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, ar
4545
// NewAttachCommand creates a new cobra.Command for `docker attach`
4646
func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
4747
var opts AttachOptions
48-
var container string
48+
var ctr string
4949

5050
cmd := &cobra.Command{
5151
Use: "attach [OPTIONS] CONTAINER",
5252
Short: "Attach local standard input, output, and error streams to a running container",
5353
Args: cli.ExactArgs(1),
5454
RunE: func(cmd *cobra.Command, args []string) error {
55-
container = args[0]
56-
return RunAttach(context.Background(), dockerCli, container, &opts)
55+
ctr = args[0]
56+
return RunAttach(context.Background(), dockerCli, ctr, &opts)
5757
},
5858
Annotations: map[string]string{
5959
"aliases": "docker container attach, docker attach",
6060
},
61-
ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(container types.Container) bool {
62-
return container.State != "paused"
61+
ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(ctr types.Container) bool {
62+
return ctr.State != "paused"
6363
}),
6464
}
6565

@@ -71,8 +71,8 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
7171
}
7272

7373
// RunAttach executes an `attach` command
74-
func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts *AttachOptions) error {
75-
apiClient := dockerCli.Client()
74+
func RunAttach(ctx context.Context, dockerCLI command.Cli, target string, opts *AttachOptions) error {
75+
apiClient := dockerCLI.Client()
7676

7777
// request channel to wait for client
7878
resultC, errC := apiClient.ContainerWait(ctx, target, "")
@@ -82,11 +82,11 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts *
8282
return err
8383
}
8484

85-
if err := dockerCli.In().CheckTty(!opts.NoStdin, c.Config.Tty); err != nil {
85+
if err := dockerCLI.In().CheckTty(!opts.NoStdin, c.Config.Tty); err != nil {
8686
return err
8787
}
8888

89-
detachKeys := dockerCli.ConfigFile().DetachKeys
89+
detachKeys := dockerCLI.ConfigFile().DetachKeys
9090
if opts.DetachKeys != "" {
9191
detachKeys = opts.DetachKeys
9292
}
@@ -101,7 +101,7 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts *
101101

102102
var in io.ReadCloser
103103
if options.Stdin {
104-
in = dockerCli.In()
104+
in = dockerCLI.In()
105105
}
106106

107107
if opts.Proxy && !c.Config.Tty {
@@ -129,15 +129,15 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts *
129129
return err
130130
}
131131

132-
if c.Config.Tty && dockerCli.Out().IsTerminal() {
133-
resizeTTY(ctx, dockerCli, target)
132+
if c.Config.Tty && dockerCLI.Out().IsTerminal() {
133+
resizeTTY(ctx, dockerCLI, target)
134134
}
135135

136136
streamer := hijackedIOStreamer{
137-
streams: dockerCli,
137+
streams: dockerCLI,
138138
inputStream: in,
139-
outputStream: dockerCli.Out(),
140-
errorStream: dockerCli.Err(),
139+
outputStream: dockerCLI.Out(),
140+
errorStream: dockerCLI.Err(),
141141
resp: resp,
142142
tty: c.Config.Tty,
143143
detachKeys: options.DetachKeys,

0 commit comments

Comments
 (0)