diff --git a/.golangci.yml b/.golangci.yml index fcffed8..e4bcc5a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,4 +1,8 @@ linters: + enable: + - errorlint + - gosimple + - typecheck disable: - deadcode - errcheck diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index 022499c..66390b7 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -1,6 +1,7 @@ package cpu import ( + "errors" "fmt" "os" "runtime" @@ -12,7 +13,7 @@ import ( ) func skipIfNotImplementedErr(t *testing.T, err error) { - if err == common.ErrNotImplementedError { + if errors.Is(err, common.ErrNotImplementedError) { t.Skip("not implemented") } } diff --git a/disk/disk_linux.go b/disk/disk_linux.go index c49220e..6d98c18 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -6,6 +6,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "io/ioutil" "os" @@ -224,7 +225,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro filename := common.HostProc("1/mountinfo") lines, err := common.ReadLines(filename) if err != nil { - if err != err.(*os.PathError) { + var pathErr *os.PathError + if !errors.As(err, &pathErr) { return nil, err } // if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26) diff --git a/disk/disk_test.go b/disk/disk_test.go index f7b5f1e..5adae5c 100644 --- a/disk/disk_test.go +++ b/disk/disk_test.go @@ -1,6 +1,7 @@ package disk import ( + "errors" "fmt" "runtime" "sync" @@ -10,7 +11,7 @@ import ( ) func skipIfNotImplementedErr(t *testing.T, err error) { - if err == common.ErrNotImplementedError { + if errors.Is(err, common.ErrNotImplementedError) { t.Skip("not implemented") } } diff --git a/host/host.go b/host/host.go index 09910b6..7c53e20 100644 --- a/host/host.go +++ b/host/host.go @@ -3,6 +3,7 @@ package host import ( "context" "encoding/json" + "errors" "os" "runtime" "time" @@ -70,47 +71,47 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) { } ret.Hostname, err = os.Hostname() - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.Platform, ret.PlatformFamily, ret.PlatformVersion, err = PlatformInformationWithContext(ctx) - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.KernelVersion, err = KernelVersionWithContext(ctx) - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.KernelArch, err = KernelArch() - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.VirtualizationSystem, ret.VirtualizationRole, err = VirtualizationWithContext(ctx) - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.BootTime, err = BootTimeWithContext(ctx) - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.Uptime, err = UptimeWithContext(ctx) - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.Procs, err = numProcs(ctx) - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } ret.HostID, err = HostIDWithContext(ctx) - if err != nil && err != common.ErrNotImplementedError { + if err != nil && !errors.Is(err, common.ErrNotImplementedError) { return nil, err } diff --git a/host/host_test.go b/host/host_test.go index e4a75f9..7630789 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -1,6 +1,7 @@ package host import ( + "errors" "fmt" "os" "sync" @@ -10,7 +11,7 @@ import ( ) func skipIfNotImplementedErr(t *testing.T, err error) { - if err == common.ErrNotImplementedError { + if errors.Is(err, common.ErrNotImplementedError) { t.Skip("not implemented") } } diff --git a/internal/common/sleep_test.go b/internal/common/sleep_test.go index 5d96503..2c846db 100644 --- a/internal/common/sleep_test.go +++ b/internal/common/sleep_test.go @@ -2,6 +2,7 @@ package common_test import ( "context" + "errors" "testing" "time" @@ -13,7 +14,7 @@ func TestSleep(test *testing.T) { var t = func(name string, ctx context.Context, expected error) { test.Run(name, func(test *testing.T) { var err = common.Sleep(ctx, dt) - if err != expected { + if !errors.Is(err, expected) { test.Errorf("expected %v, got %v", expected, err) } }) diff --git a/load/load_test.go b/load/load_test.go index a5d2866..a9db11b 100644 --- a/load/load_test.go +++ b/load/load_test.go @@ -1,6 +1,7 @@ package load import ( + "errors" "fmt" "testing" @@ -8,7 +9,7 @@ import ( ) func skipIfNotImplementedErr(t testing.TB, err error) { - if err == common.ErrNotImplementedError { + if errors.Is(err, common.ErrNotImplementedError) { t.Skip("not implemented") } } diff --git a/mem/mem_test.go b/mem/mem_test.go index 88ee1ac..d6c1df7 100644 --- a/mem/mem_test.go +++ b/mem/mem_test.go @@ -1,6 +1,7 @@ package mem import ( + "errors" "fmt" "runtime" "testing" @@ -10,7 +11,7 @@ import ( ) func skipIfNotImplementedErr(t *testing.T, err error) { - if err == common.ErrNotImplementedError { + if errors.Is(err, common.ErrNotImplementedError) { t.Skip("not implemented") } } diff --git a/net/net_linux.go b/net/net_linux.go index e88aa2c..b1d1626 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -468,7 +468,7 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p } } if err != nil { - return nil, fmt.Errorf("cound not get pid(s), %d: %s", pid, err) + return nil, fmt.Errorf("cound not get pid(s), %d: %w", pid, err) } return statsFromInodes(root, pid, tmap, inodes, skipUids) } @@ -672,7 +672,7 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) { t, err := getProcInodes(root, pid, max) if err != nil { // skip if permission error or no longer exists - if os.IsPermission(err) || os.IsNotExist(err) || err == io.EOF { + if os.IsPermission(err) || os.IsNotExist(err) || errors.Is(err, io.EOF) { continue } return ret, err @@ -702,7 +702,7 @@ func decodeAddress(family uint32, src string) (Addr, error) { } decoded, err := hex.DecodeString(addr) if err != nil { - return Addr{}, fmt.Errorf("decode error, %s", err) + return Addr{}, fmt.Errorf("decode error, %w", err) } var ip net.IP // Assumes this is little_endian diff --git a/net/net_test.go b/net/net_test.go index fff0c88..ed633cf 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -1,6 +1,7 @@ package net import ( + "errors" "fmt" "math" "os" @@ -11,7 +12,7 @@ import ( ) func skipIfNotImplementedErr(t *testing.T, err error) { - if err == common.ErrNotImplementedError { + if errors.Is(err, common.ErrNotImplementedError) { t.Skip("not implemented") } } diff --git a/process/process.go b/process/process.go index e766a80..bcfd616 100644 --- a/process/process.go +++ b/process/process.go @@ -281,7 +281,7 @@ func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) { return false, err } p2, err := NewProcessWithContext(ctx, p.Pid) - if err == ErrorProcessNotRunning { + if errors.Is(err, ErrorProcessNotRunning) { return false, nil } createTime2, err := p2.CreateTimeWithContext(ctx) diff --git a/process/process_posix.go b/process/process_posix.go index 2e4a04e..11cf9f7 100644 --- a/process/process_posix.go +++ b/process/process_posix.go @@ -4,6 +4,7 @@ package process import ( "context" + "errors" "fmt" "os" "os/user" @@ -122,8 +123,8 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) { if err.Error() == "os: process already finished" { return false, nil } - errno, ok := err.(syscall.Errno) - if !ok { + var errno syscall.Errno + if !errors.As(err, &errno) { return false, err } switch errno { diff --git a/process/process_test.go b/process/process_test.go index 78827d8..07b8fc5 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -1,6 +1,7 @@ package process import ( + "errors" "fmt" "io/ioutil" "net" @@ -23,7 +24,7 @@ import ( var mu sync.Mutex func skipIfNotImplementedErr(t *testing.T, err error) { - if err == common.ErrNotImplementedError { + if errors.Is(err, common.ErrNotImplementedError) { t.Skip("not implemented") } }