Skip to content

Commit 13879e7

Browse files
authored
Merge pull request moby#50082 from mmorel-35/go-critic
fix go-critic linter
2 parents 729cbbd + 20b6075 commit 13879e7

File tree

84 files changed

+226
-171
lines changed

Some content is hidden

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

84 files changed

+226
-171
lines changed

.golangci.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ linters:
2727
- fatcontext # Detects nested contexts in loops and function literals.
2828
- forbidigo
2929
- gocheckcompilerdirectives # Detects invalid go compiler directive comments (//go:).
30+
- gocritic # Detects for bugs, performance and style issues.
3031
- gosec # Detects security problems.
3132
- govet
3233
- iface # Detects incorrect use of interfaces. Currently only used for "identical" interfaces in the same package.
@@ -103,6 +104,56 @@ linters:
103104
msg: Add a wrapper to nlwrap.Handle for EINTR handling and update the list in .golangci.yml.
104105
analyze-types: true
105106

107+
gocritic:
108+
disabled-checks:
109+
- appendAssign
110+
- appendCombine
111+
- assignOp
112+
- builtinShadow
113+
- builtinShadowDecl
114+
- captLocal
115+
- commentedOutCode
116+
- deferInLoop
117+
- dupImport
118+
- dupSubExpr
119+
- elseif
120+
- emptyFallthrough
121+
- equalFold
122+
- evalOrder
123+
- exitAfterDefer
124+
- exposedSyncMutex
125+
- filepathJoin
126+
- hexLiteral
127+
- hugeParam
128+
- ifElseChain
129+
- importShadow
130+
- indexAlloc
131+
- methodExprCall
132+
- nestingReduce
133+
- nilValReturn
134+
- octalLiteral
135+
- paramTypeCombine
136+
- preferStringWriter
137+
- ptrToRefParam
138+
- rangeValCopy
139+
- redundantSprint
140+
- regexpMust
141+
- regexpSimplify
142+
- singleCaseSwitch
143+
- sloppyReassign
144+
- stringXbytes
145+
- typeAssertChain
146+
- typeDefFirst
147+
- typeUnparen
148+
- uncheckedInlineErr
149+
- unlambda
150+
- unnamedResult
151+
- unnecessaryDefer
152+
- unslice
153+
- valSwap
154+
- whyNoLint
155+
enable-all: true
156+
106157
gosec:
107158
excludes:
108159
- G104 # G104: Errors unhandled; (TODO: reduce unhandled errors, or explicitly ignore)

api/server/httputils/form_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestBoolValue(t *testing.T) {
3030
for c, e := range cases {
3131
v := url.Values{}
3232
v.Set("test", c)
33-
r, _ := http.NewRequest(http.MethodPost, "", nil)
33+
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
3434
r.Form = v
3535

3636
a := BoolValue(r, "test")
@@ -41,14 +41,14 @@ func TestBoolValue(t *testing.T) {
4141
}
4242

4343
func TestBoolValueOrDefault(t *testing.T) {
44-
r, _ := http.NewRequest(http.MethodGet, "", nil)
44+
r, _ := http.NewRequest(http.MethodGet, "", http.NoBody)
4545
if !BoolValueOrDefault(r, "queryparam", true) {
4646
t.Fatal("Expected to get true default value, got false")
4747
}
4848

4949
v := url.Values{}
5050
v.Set("param", "")
51-
r, _ = http.NewRequest(http.MethodGet, "", nil)
51+
r, _ = http.NewRequest(http.MethodGet, "", http.NoBody)
5252
r.Form = v
5353
if BoolValueOrDefault(r, "param", true) {
5454
t.Fatal("Expected not to get true")
@@ -66,7 +66,7 @@ func TestInt64ValueOrZero(t *testing.T) {
6666
for c, e := range cases {
6767
v := url.Values{}
6868
v.Set("test", c)
69-
r, _ := http.NewRequest(http.MethodPost, "", nil)
69+
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
7070
r.Form = v
7171

7272
a := Int64ValueOrZero(r, "test")
@@ -86,7 +86,7 @@ func TestInt64ValueOrDefault(t *testing.T) {
8686
for c, e := range cases {
8787
v := url.Values{}
8888
v.Set("test", c)
89-
r, _ := http.NewRequest(http.MethodPost, "", nil)
89+
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
9090
r.Form = v
9191

9292
a, err := Int64ValueOrDefault(r, "test", -1)
@@ -102,7 +102,7 @@ func TestInt64ValueOrDefault(t *testing.T) {
102102
func TestInt64ValueOrDefaultWithError(t *testing.T) {
103103
v := url.Values{}
104104
v.Set("test", "invalid")
105-
r, _ := http.NewRequest(http.MethodPost, "", nil)
105+
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
106106
r.Form = v
107107

108108
_, err := Int64ValueOrDefault(r, "test", -1)
@@ -150,7 +150,7 @@ func TestUint32Value(t *testing.T) {
150150
}
151151
for _, tc := range tests {
152152
t.Run(tc.value, func(t *testing.T) {
153-
r, _ := http.NewRequest(http.MethodPost, "", nil)
153+
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
154154
r.Form = url.Values{}
155155
if tc.value != valueNotSet {
156156
r.Form.Set("field", tc.value)

api/server/httputils/httputils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestJsonContentType(t *testing.T) {
3333

3434
func TestReadJSON(t *testing.T) {
3535
t.Run("nil body", func(t *testing.T) {
36-
req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", nil)
36+
req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", http.NoBody)
3737
if err != nil {
3838
t.Error(err)
3939
}

api/server/middleware/version_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestVersionMiddlewareVersion(t *testing.T) {
7979
assert.NilError(t, err)
8080
h := m.WrapHandler(handler)
8181

82-
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
82+
req, _ := http.NewRequest(http.MethodGet, "/containers/json", http.NoBody)
8383
resp := httptest.NewRecorder()
8484
ctx := context.Background()
8585

@@ -121,15 +121,15 @@ func TestVersionMiddlewareVersion(t *testing.T) {
121121
func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
122122
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
123123
v := httputils.VersionFromContext(ctx)
124-
assert.Check(t, len(v) != 0)
124+
assert.Check(t, v != "")
125125
return nil
126126
}
127127

128128
m, err := NewVersionMiddleware("1.2.3", api.DefaultVersion, api.MinSupportedAPIVersion)
129129
assert.NilError(t, err)
130130
h := m.WrapHandler(handler)
131131

132-
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
132+
req, _ := http.NewRequest(http.MethodGet, "/containers/json", http.NoBody)
133133
resp := httptest.NewRecorder()
134134
ctx := context.Background()
135135

api/server/router/image/image_routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (ir *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWrit
113113
return errdefs.InvalidParameter(err)
114114
}
115115

116-
if len(comment) == 0 {
116+
if comment == "" {
117117
comment = "Imported from " + src
118118
}
119119

api/server/router/volume/volume_routes_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
func callGetVolume(v *volumeRouter, name string) (*httptest.ResponseRecorder, error) {
2323
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
2424
vars := map[string]string{"name": name}
25-
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/volumes/%s", name), nil)
25+
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/volumes/%s", name), http.NoBody)
2626
resp := httptest.NewRecorder()
2727

2828
err := v.getVolumeByName(ctx, resp, req, vars)
@@ -32,7 +32,7 @@ func callGetVolume(v *volumeRouter, name string) (*httptest.ResponseRecorder, er
3232
func callListVolumes(v *volumeRouter) (*httptest.ResponseRecorder, error) {
3333
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
3434
vars := map[string]string{}
35-
req := httptest.NewRequest(http.MethodGet, "/volumes", nil)
35+
req := httptest.NewRequest(http.MethodGet, "/volumes", http.NoBody)
3636
resp := httptest.NewRecorder()
3737

3838
err := v.getVolumesList(ctx, resp, req, vars)
@@ -428,7 +428,7 @@ func TestVolumeRemove(t *testing.T) {
428428
}
429429

430430
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
431-
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
431+
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
432432
resp := httptest.NewRecorder()
433433

434434
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -455,7 +455,7 @@ func TestVolumeRemoveSwarm(t *testing.T) {
455455
}
456456

457457
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
458-
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
458+
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
459459
resp := httptest.NewRecorder()
460460

461461
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -472,7 +472,7 @@ func TestVolumeRemoveNotFoundNoSwarm(t *testing.T) {
472472
}
473473

474474
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
475-
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
475+
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
476476
resp := httptest.NewRecorder()
477477

478478
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -489,7 +489,7 @@ func TestVolumeRemoveNotFoundNoManager(t *testing.T) {
489489
}
490490

491491
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
492-
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
492+
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
493493
resp := httptest.NewRecorder()
494494

495495
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -513,7 +513,7 @@ func TestVolumeRemoveFoundNoSwarm(t *testing.T) {
513513
}
514514

515515
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
516-
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
516+
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
517517
resp := httptest.NewRecorder()
518518

519519
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -536,7 +536,7 @@ func TestVolumeRemoveNoSwarmInUse(t *testing.T) {
536536
}
537537

538538
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
539-
req := httptest.NewRequest(http.MethodDelete, "/volumes/inuse", nil)
539+
req := httptest.NewRequest(http.MethodDelete, "/volumes/inuse", http.NoBody)
540540
resp := httptest.NewRecorder()
541541

542542
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "inuse"})
@@ -564,7 +564,7 @@ func TestVolumeRemoveSwarmForce(t *testing.T) {
564564
}
565565

566566
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
567-
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
567+
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
568568
resp := httptest.NewRecorder()
569569

570570
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -573,7 +573,7 @@ func TestVolumeRemoveSwarmForce(t *testing.T) {
573573
assert.Assert(t, cerrdefs.IsConflict(err))
574574

575575
ctx = context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
576-
req = httptest.NewRequest(http.MethodDelete, "/volumes/vol1?force=1", nil)
576+
req = httptest.NewRequest(http.MethodDelete, "/volumes/vol1?force=1", http.NoBody)
577577
resp = httptest.NewRecorder()
578578

579579
err = v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})

api/server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestMiddlewares(t *testing.T) {
2121
}
2222
srv.UseMiddleware(*m)
2323

24-
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
24+
req, _ := http.NewRequest(http.MethodGet, "/containers/json", http.NoBody)
2525
resp := httptest.NewRecorder()
2626
ctx := context.Background()
2727

api/types/events/events.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,14 @@ type Actor struct {
112112

113113
// Message represents the information an event contains
114114
type Message struct {
115-
// Deprecated information from JSONMessage.
115+
// Deprecated: use Action instead.
116+
// Information from JSONMessage.
116117
// With data only in container events.
117-
Status string `json:"status,omitempty"` // Deprecated: use Action instead.
118-
ID string `json:"id,omitempty"` // Deprecated: use Actor.ID instead.
119-
From string `json:"from,omitempty"` // Deprecated: use Actor.Attributes["image"] instead.
118+
Status string `json:"status,omitempty"`
119+
// Deprecated: use Actor.ID instead.
120+
ID string `json:"id,omitempty"`
121+
// Deprecated: use Actor.Attributes["image"] instead.
122+
From string `json:"from,omitempty"`
120123

121124
Type Type
122125
Action Action

builder/builder-next/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
368368
ulimits, err := toBuildkitUlimits(opt.Options.Ulimits)
369369
if err != nil {
370370
return nil, err
371-
} else if len(ulimits) > 0 {
371+
} else if ulimits != "" {
372372
frontendAttrs["ulimit"] = ulimits
373373
}
374374

builder/dockerfile/imageprobe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *imageProber) Probe(parentID string, runConfig *container.Config, platfo
6060
if err != nil {
6161
return "", err
6262
}
63-
if len(cacheID) == 0 {
63+
if cacheID == "" {
6464
log.G(context.TODO()).Debugf("[BUILDER] Cache miss: %s", runConfig.Cmd)
6565
c.cacheBusted = true
6666
return "", nil

0 commit comments

Comments
 (0)