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: #2945

Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
Wei Fu 2019-01-23 22:18:28 +08:00
parent 35582cb7a3
commit 132ee9b826
24 changed files with 42 additions and 61 deletions

View File

@ -10,7 +10,6 @@
"Enable": [
"structcheck",
"unused",
"varcheck",
"staticcheck",
"unconvert",

View File

@ -329,7 +329,7 @@ func TestBreakouts(t *testing.T) {
if err != nil {
return err
}
if bytes.Compare(b, content) != 0 {
if !bytes.Equal(b, content) {
return errors.Errorf("content differs: expected %v, got %v", content, b)
}
return nil
@ -1179,7 +1179,7 @@ func fileEntry(name string, expected []byte, mode int) tarEntryValidator {
if hdr.Mode != int64(mode) {
return errors.Errorf("wrong mode %o, expected %o", hdr.Mode, mode)
}
if bytes.Compare(b, expected) != 0 {
if !bytes.Equal(b, expected) {
return errors.Errorf("different file content")
}
return nil

View File

@ -74,7 +74,7 @@ func tarName(p string) (string, error) {
// in file names, it is mostly safe to replace however we must
// check just in case
if strings.Contains(p, "/") {
return "", fmt.Errorf("Windows path contains forward slash: %s", p)
return "", fmt.Errorf("windows path contains forward slash: %s", p)
}
return strings.Replace(p, string(os.PathSeparator), "/", -1), nil
@ -130,11 +130,7 @@ func skipFile(hdr *tar.Header) bool {
// specific or Linux-specific, this warning should be changed to an error
// to cater for the situation where someone does manage to upload a Linux
// image but have it tagged as Windows inadvertently.
if strings.Contains(hdr.Name, ":") {
return true
}
return false
return strings.Contains(hdr.Name, ":")
}
// handleTarTypeBlockCharFifo is an OS-specific helper function used by

View File

@ -387,18 +387,16 @@ func (h *handler) Execute(_ []string, r <-chan svc.ChangeRequest, s chan<- svc.S
h.fromsvc <- nil
s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.Accepted(windows.SERVICE_ACCEPT_PARAMCHANGE)}
Loop:
for {
select {
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
s <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
s <- svc.Status{State: svc.StopPending, Accepts: 0}
h.s.Stop()
break Loop
}
for c := range r {
switch c.Cmd {
case svc.Interrogate:
s <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
s <- svc.Status{State: svc.StopPending, Accepts: 0}
h.s.Stop()
break Loop
}
}

View File

@ -1120,7 +1120,7 @@ func TestDaemonRestartWithRunningShim(t *testing.T) {
}
pid := task.Pid()
if pid <= 0 {
if pid < 1 {
t.Fatalf("invalid task pid %d", pid)
}
@ -1410,7 +1410,7 @@ func testUserNamespaces(t *testing.T, readonlyRootFS bool) {
t.Fatal(err)
}
if pid := task.Pid(); pid <= 0 {
if pid := task.Pid(); pid < 1 {
t.Errorf("invalid task pid %d", pid)
}
if err := task.Start(ctx); err != nil {

View File

@ -136,7 +136,7 @@ func TestContainerStart(t *testing.T) {
t.Fatal(err)
}
if pid := task.Pid(); pid <= 0 {
if pid := task.Pid(); pid < 1 {
t.Errorf("invalid task pid %d", pid)
}
if err := task.Start(ctx); err != nil {
@ -435,7 +435,7 @@ func TestContainerPids(t *testing.T) {
}
pid := task.Pid()
if pid <= 0 {
if pid < 1 {
t.Errorf("invalid task pid %d", pid)
}
processes, err := task.Pids(ctx)
@ -785,7 +785,7 @@ func TestWaitStoppedTask(t *testing.T) {
t.Fatal(err)
}
if pid := task.Pid(); pid <= 0 {
if pid := task.Pid(); pid < 1 {
t.Errorf("invalid task pid %d", pid)
}
if err := task.Start(ctx); err != nil {

View File

@ -47,7 +47,5 @@ func unlock(ref string) {
locksMu.Lock()
defer locksMu.Unlock()
if _, ok := locks[ref]; ok {
delete(locks, ref)
}
delete(locks, ref)
}

View File

@ -58,7 +58,7 @@ func (s *fsApplier) Apply(ctx context.Context, desc ocispec.Descriptor, mounts [
defer func() {
if err == nil {
log.G(ctx).WithFields(logrus.Fields{
"d": time.Now().Sub(t1),
"d": time.Since(t1),
"dgst": desc.Digest,
"size": desc.Size,
"media": desc.MediaType,

View File

@ -99,7 +99,7 @@ func (s windowsLcowDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mou
defer func() {
if err == nil {
log.G(ctx).WithFields(logrus.Fields{
"d": time.Now().Sub(t1),
"d": time.Since(t1),
"dgst": desc.Digest,
"size": desc.Size,
"media": desc.MediaType,

View File

@ -92,7 +92,7 @@ func (s windowsDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
defer func() {
if err == nil {
log.G(ctx).WithFields(logrus.Fields{
"d": time.Now().Sub(t1),
"d": time.Since(t1),
"dgst": desc.Digest,
"size": desc.Size,
"media": desc.MediaType,

View File

@ -185,7 +185,6 @@ func (s *scanner) scanQuoted(quote rune) {
ch = s.next()
}
}
return
}
func (s *scanner) scanEscape(quote rune) rune {

View File

@ -99,8 +99,7 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
})
}
var handler images.HandlerFunc
handler = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
var handler images.HandlerFunc = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
// Only save images at top level
if desc.Digest != index.Digest {
return images.Children(ctx, cs, desc)

View File

@ -779,7 +779,7 @@ func (cs *contentStore) garbageCollect(ctx context.Context) (d time.Duration, er
t1 := time.Now()
defer func() {
if err == nil {
d = time.Now().Sub(t1)
d = time.Since(t1)
}
cs.l.Unlock()
}()

View File

@ -176,7 +176,7 @@ func (m *DB) Init(ctx context.Context) error {
if err := m.migrate(tx); err != nil {
return errors.Wrapf(err, "failed to migrate to %s.%d", m.schema, m.version)
}
log.G(ctx).WithField("d", time.Now().Sub(t0)).Debugf("finished database migration to %s.%d", m.schema, m.version)
log.G(ctx).WithField("d", time.Since(t0)).Debugf("finished database migration to %s.%d", m.schema, m.version)
}
}
@ -328,7 +328,7 @@ func (m *DB) GarbageCollect(ctx context.Context) (gc.Stats, error) {
m.cleanupSnapshotter(snapshotterName)
sl.Lock()
stats.SnapshotD[snapshotterName] = time.Now().Sub(st1)
stats.SnapshotD[snapshotterName] = time.Since(st1)
sl.Unlock()
wg.Done()
@ -343,7 +343,7 @@ func (m *DB) GarbageCollect(ctx context.Context) (gc.Stats, error) {
go func() {
ct1 := time.Now()
m.cleanupContent()
stats.ContentD = time.Now().Sub(ct1)
stats.ContentD = time.Since(ct1)
wg.Done()
}()
m.dirtyCS = false
@ -351,7 +351,7 @@ func (m *DB) GarbageCollect(ctx context.Context) (gc.Stats, error) {
m.dirtyL.Unlock()
stats.MetaD = time.Now().Sub(t1)
stats.MetaD = time.Since(t1)
m.wlock.Unlock()
wg.Wait()

View File

@ -628,7 +628,7 @@ func (s *snapshotter) garbageCollect(ctx context.Context) (d time.Duration, err
}
}
if err == nil {
d = time.Now().Sub(t1)
d = time.Since(t1)
}
}()

View File

@ -19,6 +19,6 @@ package progress
const (
escape = "\x1b"
reset = escape + "[0m"
red = escape + "[31m" // nolint: unused, varcheck
red = escape + "[31m" // nolint: staticcheck, varcheck
green = escape + "[32m"
)

View File

@ -49,7 +49,7 @@ func RequiresRootM() {
fmt.Fprintln(os.Stderr, "skipping test that requires root")
os.Exit(0)
}
if 0 != os.Getuid() {
if os.Getuid() != 0 {
fmt.Fprintln(os.Stderr, "This test must be run as root.")
os.Exit(1)
}

View File

@ -42,10 +42,7 @@ var (
// IsSkipPlugin returns true if the error is skipping the plugin
func IsSkipPlugin(err error) bool {
if errors.Cause(err) == ErrSkipPlugin {
return true
}
return false
return errors.Cause(err) == ErrSkipPlugin
}
// Type is the type of the plugin

View File

@ -79,8 +79,8 @@ func init() {
var t octetType
isCtl := c <= 31 || c == 127
isChar := 0 <= c && c <= 127
isSeparator := strings.IndexRune(" \t\"(),/:;<=>?@[]\\{}", rune(c)) >= 0
if strings.IndexRune(" \t\r\n", rune(c)) >= 0 {
isSeparator := strings.ContainsRune(" \t\"(),/:;<=>?@[]\\{}", rune(c))
if strings.ContainsRune(" \t\r\n", rune(c)) {
t |= isSpace
}
if isChar && !isCtl && !isSeparator {

View File

@ -76,9 +76,9 @@ func serveListener(path string) (net.Listener, error) {
func handleSignals(logger *logrus.Entry, signals chan os.Signal) error {
logger.Info("starting signal loop")
for {
select {
case s := <-signals:
for s := range signals {
switch s {
case unix.SIGCHLD:
if err := Reap(); err != nil {

View File

@ -108,12 +108,11 @@ func serveListener(path string) (net.Listener, error) {
func handleSignals(logger *logrus.Entry, signals chan os.Signal) error {
logger.Info("starting signal loop")
for {
select {
case s := <-signals:
for s := range signals {
switch s {
case os.Interrupt:
break
}
}
}

View File

@ -54,7 +54,7 @@ func AnonDialer(address string, timeout time.Duration) (net.Conn, error) {
timedOutError := errors.Errorf("timed out waiting for npipe %s", address)
start := time.Now()
for {
remaining := timeout - time.Now().Sub(start)
remaining := timeout - time.Since(start)
if remaining <= 0 {
lastError = timedOutError
break
@ -71,7 +71,7 @@ func AnonDialer(address string, timeout time.Duration) (net.Conn, error) {
// serve it within 5 seconds. We use the passed in timeout for the
// `DialPipe` timeout if the pipe exists however to give the pipe time
// to `Accept` the connection.
if time.Now().Sub(start) >= 5*time.Second {
if time.Since(start) >= 5*time.Second {
lastError = timedOutError
break
}

View File

@ -147,9 +147,7 @@ func (l *local) Update(ctx context.Context, req *api.UpdateContainerRequest, _ .
if err := l.withStoreUpdate(ctx, func(ctx context.Context, store containers.Store) error {
var fieldpaths []string
if req.UpdateMask != nil && len(req.UpdateMask.Paths) > 0 {
for _, path := range req.UpdateMask.Paths {
fieldpaths = append(fieldpaths, path)
}
fieldpaths = append(fieldpaths, req.UpdateMask.Paths...)
}
updated, err := store.Update(ctx, container, fieldpaths...)

View File

@ -137,9 +137,7 @@ func (l *local) Update(ctx context.Context, req *imagesapi.UpdateImageRequest, _
)
if req.UpdateMask != nil && len(req.UpdateMask.Paths) > 0 {
for _, path := range req.UpdateMask.Paths {
fieldpaths = append(fieldpaths, path)
}
fieldpaths = append(fieldpaths, req.UpdateMask.Paths...)
}
updated, err := l.store.Update(ctx, image, fieldpaths...)