Skip to content

Commit 132ee9b

Browse files
committed
fix: linter issue
megacheck, gosimple and unused has been deprecated and subsumed by staticcheck. And staticcheck also has been upgraded. we need to update code for the linter issue. close: containerd#2945 Signed-off-by: Wei Fu <[email protected]>
1 parent 35582cb commit 132ee9b

File tree

24 files changed

+42
-61
lines changed

24 files changed

+42
-61
lines changed

.gometalinter.json

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
"Enable": [
1212
"structcheck",
13-
"unused",
1413
"varcheck",
1514
"staticcheck",
1615
"unconvert",

archive/tar_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func TestBreakouts(t *testing.T) {
329329
if err != nil {
330330
return err
331331
}
332-
if bytes.Compare(b, content) != 0 {
332+
if !bytes.Equal(b, content) {
333333
return errors.Errorf("content differs: expected %v, got %v", content, b)
334334
}
335335
return nil
@@ -1179,7 +1179,7 @@ func fileEntry(name string, expected []byte, mode int) tarEntryValidator {
11791179
if hdr.Mode != int64(mode) {
11801180
return errors.Errorf("wrong mode %o, expected %o", hdr.Mode, mode)
11811181
}
1182-
if bytes.Compare(b, expected) != 0 {
1182+
if !bytes.Equal(b, expected) {
11831183
return errors.Errorf("different file content")
11841184
}
11851185
return nil

archive/tar_windows.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func tarName(p string) (string, error) {
7474
// in file names, it is mostly safe to replace however we must
7575
// check just in case
7676
if strings.Contains(p, "/") {
77-
return "", fmt.Errorf("Windows path contains forward slash: %s", p)
77+
return "", fmt.Errorf("windows path contains forward slash: %s", p)
7878
}
7979

8080
return strings.Replace(p, string(os.PathSeparator), "/", -1), nil
@@ -130,11 +130,7 @@ func skipFile(hdr *tar.Header) bool {
130130
// specific or Linux-specific, this warning should be changed to an error
131131
// to cater for the situation where someone does manage to upload a Linux
132132
// image but have it tagged as Windows inadvertently.
133-
if strings.Contains(hdr.Name, ":") {
134-
return true
135-
}
136-
137-
return false
133+
return strings.Contains(hdr.Name, ":")
138134
}
139135

140136
// handleTarTypeBlockCharFifo is an OS-specific helper function used by

cmd/containerd/command/service_windows.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,16 @@ func (h *handler) Execute(_ []string, r <-chan svc.ChangeRequest, s chan<- svc.S
387387
h.fromsvc <- nil
388388

389389
s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.Accepted(windows.SERVICE_ACCEPT_PARAMCHANGE)}
390+
390391
Loop:
391-
for {
392-
select {
393-
case c := <-r:
394-
switch c.Cmd {
395-
case svc.Interrogate:
396-
s <- c.CurrentStatus
397-
case svc.Stop, svc.Shutdown:
398-
s <- svc.Status{State: svc.StopPending, Accepts: 0}
399-
h.s.Stop()
400-
break Loop
401-
}
392+
for c := range r {
393+
switch c.Cmd {
394+
case svc.Interrogate:
395+
s <- c.CurrentStatus
396+
case svc.Stop, svc.Shutdown:
397+
s <- svc.Status{State: svc.StopPending, Accepts: 0}
398+
h.s.Stop()
399+
break Loop
402400
}
403401
}
404402

container_linux_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ func TestDaemonRestartWithRunningShim(t *testing.T) {
11201120
}
11211121

11221122
pid := task.Pid()
1123-
if pid <= 0 {
1123+
if pid < 1 {
11241124
t.Fatalf("invalid task pid %d", pid)
11251125
}
11261126

@@ -1410,7 +1410,7 @@ func testUserNamespaces(t *testing.T, readonlyRootFS bool) {
14101410
t.Fatal(err)
14111411
}
14121412

1413-
if pid := task.Pid(); pid <= 0 {
1413+
if pid := task.Pid(); pid < 1 {
14141414
t.Errorf("invalid task pid %d", pid)
14151415
}
14161416
if err := task.Start(ctx); err != nil {

container_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestContainerStart(t *testing.T) {
136136
t.Fatal(err)
137137
}
138138

139-
if pid := task.Pid(); pid <= 0 {
139+
if pid := task.Pid(); pid < 1 {
140140
t.Errorf("invalid task pid %d", pid)
141141
}
142142
if err := task.Start(ctx); err != nil {
@@ -435,7 +435,7 @@ func TestContainerPids(t *testing.T) {
435435
}
436436

437437
pid := task.Pid()
438-
if pid <= 0 {
438+
if pid < 1 {
439439
t.Errorf("invalid task pid %d", pid)
440440
}
441441
processes, err := task.Pids(ctx)
@@ -785,7 +785,7 @@ func TestWaitStoppedTask(t *testing.T) {
785785
t.Fatal(err)
786786
}
787787

788-
if pid := task.Pid(); pid <= 0 {
788+
if pid := task.Pid(); pid < 1 {
789789
t.Errorf("invalid task pid %d", pid)
790790
}
791791
if err := task.Start(ctx); err != nil {

content/local/locks.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,5 @@ func unlock(ref string) {
4747
locksMu.Lock()
4848
defer locksMu.Unlock()
4949

50-
if _, ok := locks[ref]; ok {
51-
delete(locks, ref)
52-
}
50+
delete(locks, ref)
5351
}

diff/apply/apply.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (s *fsApplier) Apply(ctx context.Context, desc ocispec.Descriptor, mounts [
5858
defer func() {
5959
if err == nil {
6060
log.G(ctx).WithFields(logrus.Fields{
61-
"d": time.Now().Sub(t1),
61+
"d": time.Since(t1),
6262
"dgst": desc.Digest,
6363
"size": desc.Size,
6464
"media": desc.MediaType,

diff/lcow/lcow.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (s windowsLcowDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mou
9999
defer func() {
100100
if err == nil {
101101
log.G(ctx).WithFields(logrus.Fields{
102-
"d": time.Now().Sub(t1),
102+
"d": time.Since(t1),
103103
"dgst": desc.Digest,
104104
"size": desc.Size,
105105
"media": desc.MediaType,

diff/windows/windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (s windowsDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
9292
defer func() {
9393
if err == nil {
9494
log.G(ctx).WithFields(logrus.Fields{
95-
"d": time.Now().Sub(t1),
95+
"d": time.Since(t1),
9696
"dgst": desc.Digest,
9797
"size": desc.Size,
9898
"media": desc.MediaType,

filters/scanner.go

-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ func (s *scanner) scanQuoted(quote rune) {
185185
ch = s.next()
186186
}
187187
}
188-
return
189188
}
190189

191190
func (s *scanner) scanEscape(quote rune) rune {

import.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
9999
})
100100
}
101101

102-
var handler images.HandlerFunc
103-
handler = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
102+
var handler images.HandlerFunc = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
104103
// Only save images at top level
105104
if desc.Digest != index.Digest {
106105
return images.Children(ctx, cs, desc)

metadata/content.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ func (cs *contentStore) garbageCollect(ctx context.Context) (d time.Duration, er
779779
t1 := time.Now()
780780
defer func() {
781781
if err == nil {
782-
d = time.Now().Sub(t1)
782+
d = time.Since(t1)
783783
}
784784
cs.l.Unlock()
785785
}()

metadata/db.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (m *DB) Init(ctx context.Context) error {
176176
if err := m.migrate(tx); err != nil {
177177
return errors.Wrapf(err, "failed to migrate to %s.%d", m.schema, m.version)
178178
}
179-
log.G(ctx).WithField("d", time.Now().Sub(t0)).Debugf("finished database migration to %s.%d", m.schema, m.version)
179+
log.G(ctx).WithField("d", time.Since(t0)).Debugf("finished database migration to %s.%d", m.schema, m.version)
180180
}
181181
}
182182

@@ -328,7 +328,7 @@ func (m *DB) GarbageCollect(ctx context.Context) (gc.Stats, error) {
328328
m.cleanupSnapshotter(snapshotterName)
329329

330330
sl.Lock()
331-
stats.SnapshotD[snapshotterName] = time.Now().Sub(st1)
331+
stats.SnapshotD[snapshotterName] = time.Since(st1)
332332
sl.Unlock()
333333

334334
wg.Done()
@@ -343,15 +343,15 @@ func (m *DB) GarbageCollect(ctx context.Context) (gc.Stats, error) {
343343
go func() {
344344
ct1 := time.Now()
345345
m.cleanupContent()
346-
stats.ContentD = time.Now().Sub(ct1)
346+
stats.ContentD = time.Since(ct1)
347347
wg.Done()
348348
}()
349349
m.dirtyCS = false
350350
}
351351

352352
m.dirtyL.Unlock()
353353

354-
stats.MetaD = time.Now().Sub(t1)
354+
stats.MetaD = time.Since(t1)
355355
m.wlock.Unlock()
356356

357357
wg.Wait()

metadata/snapshot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ func (s *snapshotter) garbageCollect(ctx context.Context) (d time.Duration, err
628628
}
629629
}
630630
if err == nil {
631-
d = time.Now().Sub(t1)
631+
d = time.Since(t1)
632632
}
633633
}()
634634

pkg/progress/escape.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ package progress
1919
const (
2020
escape = "\x1b"
2121
reset = escape + "[0m"
22-
red = escape + "[31m" // nolint: unused, varcheck
22+
red = escape + "[31m" // nolint: staticcheck, varcheck
2323
green = escape + "[32m"
2424
)

pkg/testutil/helpers_unix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func RequiresRootM() {
4949
fmt.Fprintln(os.Stderr, "skipping test that requires root")
5050
os.Exit(0)
5151
}
52-
if 0 != os.Getuid() {
52+
if os.Getuid() != 0 {
5353
fmt.Fprintln(os.Stderr, "This test must be run as root.")
5454
os.Exit(1)
5555
}

plugin/plugin.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ var (
4242

4343
// IsSkipPlugin returns true if the error is skipping the plugin
4444
func IsSkipPlugin(err error) bool {
45-
if errors.Cause(err) == ErrSkipPlugin {
46-
return true
47-
}
48-
return false
45+
return errors.Cause(err) == ErrSkipPlugin
4946
}
5047

5148
// Type is the type of the plugin

remotes/docker/auth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func init() {
7979
var t octetType
8080
isCtl := c <= 31 || c == 127
8181
isChar := 0 <= c && c <= 127
82-
isSeparator := strings.IndexRune(" \t\"(),/:;<=>?@[]\\{}", rune(c)) >= 0
83-
if strings.IndexRune(" \t\r\n", rune(c)) >= 0 {
82+
isSeparator := strings.ContainsRune(" \t\"(),/:;<=>?@[]\\{}", rune(c))
83+
if strings.ContainsRune(" \t\r\n", rune(c)) {
8484
t |= isSpace
8585
}
8686
if isChar && !isCtl && !isSeparator {

runtime/v2/shim/shim_unix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ func serveListener(path string) (net.Listener, error) {
7676

7777
func handleSignals(logger *logrus.Entry, signals chan os.Signal) error {
7878
logger.Info("starting signal loop")
79+
7980
for {
80-
select {
81-
case s := <-signals:
81+
for s := range signals {
8282
switch s {
8383
case unix.SIGCHLD:
8484
if err := Reap(); err != nil {

runtime/v2/shim/shim_windows.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,11 @@ func serveListener(path string) (net.Listener, error) {
108108

109109
func handleSignals(logger *logrus.Entry, signals chan os.Signal) error {
110110
logger.Info("starting signal loop")
111+
111112
for {
112-
select {
113-
case s := <-signals:
113+
for s := range signals {
114114
switch s {
115115
case os.Interrupt:
116-
break
117116
}
118117
}
119118
}

runtime/v2/shim/util_windows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func AnonDialer(address string, timeout time.Duration) (net.Conn, error) {
5454
timedOutError := errors.Errorf("timed out waiting for npipe %s", address)
5555
start := time.Now()
5656
for {
57-
remaining := timeout - time.Now().Sub(start)
57+
remaining := timeout - time.Since(start)
5858
if remaining <= 0 {
5959
lastError = timedOutError
6060
break
@@ -71,7 +71,7 @@ func AnonDialer(address string, timeout time.Duration) (net.Conn, error) {
7171
// serve it within 5 seconds. We use the passed in timeout for the
7272
// `DialPipe` timeout if the pipe exists however to give the pipe time
7373
// to `Accept` the connection.
74-
if time.Now().Sub(start) >= 5*time.Second {
74+
if time.Since(start) >= 5*time.Second {
7575
lastError = timedOutError
7676
break
7777
}

services/containers/local.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ func (l *local) Update(ctx context.Context, req *api.UpdateContainerRequest, _ .
147147
if err := l.withStoreUpdate(ctx, func(ctx context.Context, store containers.Store) error {
148148
var fieldpaths []string
149149
if req.UpdateMask != nil && len(req.UpdateMask.Paths) > 0 {
150-
for _, path := range req.UpdateMask.Paths {
151-
fieldpaths = append(fieldpaths, path)
152-
}
150+
fieldpaths = append(fieldpaths, req.UpdateMask.Paths...)
153151
}
154152

155153
updated, err := store.Update(ctx, container, fieldpaths...)

services/images/local.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ func (l *local) Update(ctx context.Context, req *imagesapi.UpdateImageRequest, _
137137
)
138138

139139
if req.UpdateMask != nil && len(req.UpdateMask.Paths) > 0 {
140-
for _, path := range req.UpdateMask.Paths {
141-
fieldpaths = append(fieldpaths, path)
142-
}
140+
fieldpaths = append(fieldpaths, req.UpdateMask.Paths...)
143141
}
144142

145143
updated, err := l.store.Update(ctx, image, fieldpaths...)

0 commit comments

Comments
 (0)