Skip to content

Commit ce7e4c8

Browse files
authored
Merge pull request #1856 from mmorel-35/gocritic/enable-all
enable all go-critic
2 parents afdd6f1 + 431dd63 commit ce7e4c8

40 files changed

+126
-120
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ linters:
4141
gocritic:
4242
disabled-checks:
4343
- captLocal
44+
- commentedOutCode
45+
- deferInLoop
46+
- hexLiteral
47+
- hugeParam
48+
- tooManyResultsChecker
49+
- unnamedResult
50+
enable-all: true
4451
gomodguard:
4552
blocked:
4653
modules:

cpu/cpu_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func finishCPUInfo(ctx context.Context, c *InfoStat) {
135135
var err error
136136
var value float64
137137

138-
if len(c.CoreID) == 0 {
138+
if c.CoreID == "" {
139139
lines, err = common.ReadLines(sysCPUPath(ctx, c.CPU, "topology/core_id"))
140140
if err == nil {
141141
c.CoreID = lines[0]

cpu/cpu_plan9_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var timesTests = []struct {
3535
func TestTimesPlan9(t *testing.T) {
3636
for _, tt := range timesTests {
3737
t.Run(tt.mockedRootFS, func(t *testing.T) {
38-
t.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
38+
t.Setenv("HOST_ROOT", filepath.Join("testdata", "plan9", tt.mockedRootFS))
3939
stats, err := Times(false)
4040
common.SkipIfNotImplementedErr(t, err)
4141
require.NoError(t, err)

cpu/cpu_solaris.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,16 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
140140
}
141141

142142
result := make([]InfoStat, 0, len(flags))
143-
for _, proc := range procs {
144-
procWithFlags := proc
143+
for i := range procs {
144+
procWithFlags := procs[i]
145145
procWithFlags.Flags = flags
146146
result = append(result, procWithFlags)
147147
}
148148

149149
return result, nil
150150
}
151151

152-
var flagsMatch = regexp.MustCompile(`[\w\.]+`)
152+
var flagsMatch = regexp.MustCompile(`[\w.]+`)
153153

154154
func parseISAInfo(cmdOutput string) ([]string, error) {
155155
words := flagsMatch.FindAllString(cmdOutput, -1)

cpu/cpu_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package cpu
33

44
import (
5-
"fmt"
65
"os"
76
"runtime"
87
"testing"
@@ -80,7 +79,7 @@ func TestTimeStat_String(t *testing.T) {
8079
Idle: 300.1,
8180
}
8281
e := `{"cpu":"cpu0","user":100.1,"system":200.1,"idle":300.1,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0}`
83-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "CPUTimesStat string is invalid: %v", v)
82+
assert.JSONEqf(t, e, v.String(), "CPUTimesStat string is invalid: %v", v)
8483
}
8584

8685
func TestInfo(t *testing.T) {

disk/disk_darwin.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func PartitionsWithContext(_ context.Context, _ bool) ([]PartitionStat, error) {
3535
// to prevent accessing uninitialized entries.
3636
// https://github.com/shirou/gopsutil/issues/1390
3737
fs = fs[:count]
38-
for _, stat := range fs {
38+
for i := range fs {
39+
stat := &fs[i]
3940
opts := []string{"rw"}
4041
if stat.Flags&unix.MNT_RDONLY != 0 {
4142
opts = []string{"ro"}
@@ -131,8 +132,10 @@ func SerialNumberWithContext(ctx context.Context, _ string) (string, error) {
131132

132133
// Extract all serial numbers into a single string
133134
var serialNumbers []string
134-
for _, spnvmeData := range data.SPNVMeDataType {
135-
for _, item := range spnvmeData.Items {
135+
for i := range data.SPNVMeDataType {
136+
spnvmeData := &data.SPNVMeDataType[i]
137+
for j := range spnvmeData.Items {
138+
item := &spnvmeData.Items[j]
136139
serialNumbers = append(serialNumbers, item.DeviceSerial)
137140
}
138141
}

disk/disk_freebsd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func PartitionsWithContext(_ context.Context, _ bool) ([]PartitionStat, error) {
3333
return ret, err
3434
}
3535

36-
for _, stat := range fs {
36+
for i := range fs {
37+
stat := &fs[i]
3738
opts := []string{"rw"}
3839
if stat.Flags&unix.MNT_RDONLY != 0 {
3940
opts = []string{"ro"}
@@ -182,7 +183,8 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
182183
break
183184
}
184185
}
185-
if err = s.Err(); err != nil {
186+
err = s.Err()
187+
if err != nil {
186188
return "", err
187189
}
188190
return serial, nil

disk/disk_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
506506
return ret, nil
507507
}
508508

509-
func udevData(ctx context.Context, major uint32, minor uint32, name string) (string, error) {
509+
func udevData(ctx context.Context, major, minor uint32, name string) (string, error) {
510510
udevDataPath := common.HostRunWithContext(ctx, fmt.Sprintf("udev/data/b%d:%d", major, minor))
511511
if f, err := os.Open(udevDataPath); err == nil {
512512
defer f.Close()

disk/disk_netbsd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func PartitionsWithContext(_ context.Context, _ bool) ([]PartitionStat, error) {
5757
return ret, err
5858
}
5959

60-
for _, stat := range buf {
60+
for i := range buf {
61+
stat := &buf[i]
6162
opts := []string{"rw"}
6263
if stat.Flag&MNT_RDONLY != 0 {
6364
opts = []string{"rw"}

disk/disk_openbsd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func PartitionsWithContext(_ context.Context, _ bool) ([]PartitionStat, error) {
2727
return ret, err
2828
}
2929

30-
for _, stat := range fs {
30+
for i := range fs {
31+
stat := &fs[i]
3132
opts := []string{"rw"}
3233
if stat.F_flags&unix.MNT_RDONLY != 0 {
3334
opts = []string{"rw"}

disk/disk_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package disk
33

44
import (
5-
"fmt"
65
"runtime"
76
"sync"
87
"testing"
@@ -81,7 +80,7 @@ func TestUsageStat_String(t *testing.T) {
8180
Fstype: "ext4",
8281
}
8382
e := `{"path":"/","fstype":"ext4","total":1000,"free":2000,"used":3000,"usedPercent":50.1,"inodesTotal":4000,"inodesUsed":5000,"inodesFree":6000,"inodesUsedPercent":49.1}`
84-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "DiskUsageStat string is invalid: %v", v)
83+
assert.JSONEqf(t, e, v.String(), "DiskUsageStat string is invalid: %v", v)
8584
}
8685

8786
func TestPartitionStat_String(t *testing.T) {
@@ -92,7 +91,7 @@ func TestPartitionStat_String(t *testing.T) {
9291
Opts: []string{"ro"},
9392
}
9493
e := `{"device":"sd01","mountpoint":"/","fstype":"ext4","opts":["ro"]}`
95-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "DiskUsageStat string is invalid: %v", v)
94+
assert.JSONEqf(t, e, v.String(), "DiskUsageStat string is invalid: %v", v)
9695
}
9796

9897
func TestIOCountersStat_String(t *testing.T) {
@@ -105,5 +104,5 @@ func TestIOCountersStat_String(t *testing.T) {
105104
SerialNumber: "SERIAL",
106105
}
107106
e := `{"readCount":100,"mergedReadCount":0,"writeCount":200,"mergedWriteCount":0,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"sd01","serialNumber":"SERIAL","label":""}`
108-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "DiskUsageStat string is invalid: %v", v)
107+
assert.JSONEqf(t, e, v.String(), "DiskUsageStat string is invalid: %v", v)
109108
}

disk/disk_windows.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -192,40 +192,41 @@ func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCou
192192
return drivemap, err
193193
}
194194
for _, v := range lpBuffer[:lpBufferLen] {
195-
if 'A' <= v && v <= 'Z' {
196-
path := string(rune(v)) + ":"
197-
typepath, _ := windows.UTF16PtrFromString(path)
198-
typeret := windows.GetDriveType(typepath)
199-
if typeret != windows.DRIVE_FIXED {
195+
if 'A' > v || v > 'Z' {
196+
continue
197+
}
198+
path := string(rune(v)) + ":"
199+
typepath, _ := windows.UTF16PtrFromString(path)
200+
typeret := windows.GetDriveType(typepath)
201+
if typeret != windows.DRIVE_FIXED {
202+
continue
203+
}
204+
szDevice := `\\.\` + path
205+
const IOCTL_DISK_PERFORMANCE = 0x70020
206+
h, err := windows.CreateFile(syscall.StringToUTF16Ptr(szDevice), 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
207+
if err != nil {
208+
if errors.Is(err, windows.ERROR_FILE_NOT_FOUND) {
200209
continue
201210
}
202-
szDevice := `\\.\` + path
203-
const IOCTL_DISK_PERFORMANCE = 0x70020
204-
h, err := windows.CreateFile(syscall.StringToUTF16Ptr(szDevice), 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
205-
if err != nil {
206-
if errors.Is(err, windows.ERROR_FILE_NOT_FOUND) {
207-
continue
208-
}
209-
return drivemap, err
210-
}
211-
defer windows.CloseHandle(h)
211+
return drivemap, err
212+
}
213+
defer windows.CloseHandle(h)
212214

213-
var diskPerformanceSize uint32
214-
err = windows.DeviceIoControl(h, IOCTL_DISK_PERFORMANCE, nil, 0, (*byte)(unsafe.Pointer(&diskPerformance)), uint32(unsafe.Sizeof(diskPerformance)), &diskPerformanceSize, nil)
215-
if err != nil {
216-
return drivemap, err
217-
}
215+
var diskPerformanceSize uint32
216+
err = windows.DeviceIoControl(h, IOCTL_DISK_PERFORMANCE, nil, 0, (*byte)(unsafe.Pointer(&diskPerformance)), uint32(unsafe.Sizeof(diskPerformance)), &diskPerformanceSize, nil)
217+
if err != nil {
218+
return drivemap, err
219+
}
218220

219-
if len(names) == 0 || common.StringsHas(names, path) {
220-
drivemap[path] = IOCountersStat{
221-
ReadBytes: uint64(diskPerformance.BytesRead),
222-
WriteBytes: uint64(diskPerformance.BytesWritten),
223-
ReadCount: uint64(diskPerformance.ReadCount),
224-
WriteCount: uint64(diskPerformance.WriteCount),
225-
ReadTime: uint64(diskPerformance.ReadTime / 10000 / 1000), // convert to ms: https://github.com/giampaolo/psutil/issues/1012
226-
WriteTime: uint64(diskPerformance.WriteTime / 10000 / 1000),
227-
Name: path,
228-
}
221+
if len(names) == 0 || common.StringsHas(names, path) {
222+
drivemap[path] = IOCountersStat{
223+
ReadBytes: uint64(diskPerformance.BytesRead),
224+
WriteBytes: uint64(diskPerformance.BytesWritten),
225+
ReadCount: uint64(diskPerformance.ReadCount),
226+
WriteCount: uint64(diskPerformance.WriteCount),
227+
ReadTime: uint64(diskPerformance.ReadTime / 10000 / 1000), // convert to ms: https://github.com/giampaolo/psutil/issues/1012
228+
WriteTime: uint64(diskPerformance.WriteTime / 10000 / 1000),
229+
Name: path,
229230
}
230231
}
231232
}

docker/docker_linux.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,26 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
8787
// containerID is same as docker id if you use docker.
8888
// If you use container via systemd.slice, you could use
8989
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
90-
func CgroupCPU(containerID string, base string) (*CgroupCPUStat, error) {
90+
func CgroupCPU(containerID, base string) (*CgroupCPUStat, error) {
9191
return CgroupCPUWithContext(context.Background(), containerID, base)
9292
}
9393

9494
// CgroupCPUUsage returns specified cgroup id CPU usage.
9595
// containerID is same as docker id if you use docker.
9696
// If you use container via systemd.slice, you could use
9797
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
98-
func CgroupCPUUsage(containerID string, base string) (float64, error) {
98+
func CgroupCPUUsage(containerID, base string) (float64, error) {
9999
return CgroupCPUUsageWithContext(context.Background(), containerID, base)
100100
}
101101

102-
func CgroupCPUWithContext(ctx context.Context, containerID string, base string) (*CgroupCPUStat, error) {
102+
func CgroupCPUWithContext(ctx context.Context, containerID, base string) (*CgroupCPUStat, error) {
103103
statfile := getCgroupFilePath(ctx, containerID, base, "cpuacct", "cpuacct.stat")
104104
lines, err := common.ReadLines(statfile)
105105
if err != nil {
106106
return nil, err
107107
}
108108
// empty containerID means all cgroup
109-
if len(containerID) == 0 {
109+
if containerID == "" {
110110
containerID = "all"
111111
}
112112

@@ -166,15 +166,15 @@ func CgroupCPUDockerUsageWithContext(ctx context.Context, containerID string) (f
166166
return CgroupCPUUsageWithContext(ctx, containerID, common.HostSysWithContext(ctx, "fs/cgroup/cpuacct/docker"))
167167
}
168168

169-
func CgroupMem(containerID string, base string) (*CgroupMemStat, error) {
169+
func CgroupMem(containerID, base string) (*CgroupMemStat, error) {
170170
return CgroupMemWithContext(context.Background(), containerID, base)
171171
}
172172

173-
func CgroupMemWithContext(ctx context.Context, containerID string, base string) (*CgroupMemStat, error) {
173+
func CgroupMemWithContext(ctx context.Context, containerID, base string) (*CgroupMemStat, error) {
174174
statfile := getCgroupFilePath(ctx, containerID, base, "memory", "memory.stat")
175175

176176
// empty containerID means all cgroup
177-
if len(containerID) == 0 {
177+
if containerID == "" {
178178
containerID = "all"
179179
}
180180
lines, err := common.ReadLines(statfile)
@@ -276,7 +276,7 @@ func CgroupMemDockerWithContext(ctx context.Context, containerID string) (*Cgrou
276276

277277
// getCgroupFilePath constructs file path to get targeted stats file.
278278
func getCgroupFilePath(ctx context.Context, containerID, base, target, file string) string {
279-
if len(base) == 0 {
279+
if base == "" {
280280
base = common.HostSysWithContext(ctx, fmt.Sprintf("fs/cgroup/%s/docker", target))
281281
}
282282
statfile := path.Join(base, containerID, file)

docker/docker_notlinux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func GetDockerIDListWithContext(_ context.Context) ([]string, error) {
3333
// containerID is same as docker id if you use docker.
3434
// If you use container via systemd.slice, you could use
3535
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
36-
func CgroupCPU(containerID string, base string) (*CgroupCPUStat, error) {
36+
func CgroupCPU(containerID, base string) (*CgroupCPUStat, error) {
3737
return CgroupCPUWithContext(context.Background(), containerID, base)
3838
}
3939

40-
func CgroupCPUWithContext(_ context.Context, _ string, _ string) (*CgroupCPUStat, error) {
40+
func CgroupCPUWithContext(_ context.Context, _, _ string) (*CgroupCPUStat, error) {
4141
return nil, ErrCgroupNotAvailable
4242
}
4343

@@ -49,11 +49,11 @@ func CgroupCPUDockerWithContext(ctx context.Context, containerID string) (*Cgrou
4949
return CgroupCPUWithContext(ctx, containerID, common.HostSysWithContext(ctx, "fs/cgroup/cpuacct/docker"))
5050
}
5151

52-
func CgroupMem(containerID string, base string) (*CgroupMemStat, error) {
52+
func CgroupMem(containerID, base string) (*CgroupMemStat, error) {
5353
return CgroupMemWithContext(context.Background(), containerID, base)
5454
}
5555

56-
func CgroupMemWithContext(_ context.Context, _ string, _ string) (*CgroupMemStat, error) {
56+
func CgroupMemWithContext(_ context.Context, _, _ string) (*CgroupMemStat, error) {
5757
return nil, ErrCgroupNotAvailable
5858
}
5959

host/host_aix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
160160
}
161161

162162
// Much of this function could be static. However, to be future proofed, I've made it call the OS for the information in all instances.
163-
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
163+
func PlatformInformationWithContext(ctx context.Context) (platform, family, version string, err error) {
164164
// Set the platform (which should always, and only be, "AIX") from `uname -s`
165165
out, err := invoke.CommandWithContext(ctx, "uname", "-s")
166166
if err != nil {

host/host_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func getlsbStruct(ctx context.Context) (*lsbStruct, error) {
170170
return ret, nil
171171
}
172172

173-
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
173+
func PlatformInformationWithContext(ctx context.Context) (platform, family, version string, err error) {
174174
lsb, err := getlsbStruct(ctx)
175175
if err != nil {
176176
lsb = &lsbStruct{}

host/host_solaris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func numProcs(_ context.Context) (uint64, error) {
6767
return uint64(len(dirs)), nil
6868
}
6969

70-
var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`)
70+
var kstatMatch = regexp.MustCompile(`(\S+)\s+(\S*)`)
7171

7272
func BootTimeWithContext(ctx context.Context) (uint64, error) {
7373
out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "unix:0:system_misc:boot_time")

host/host_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package host
33

44
import (
5-
"fmt"
65
"os"
76
"sync"
87
"testing"
@@ -86,7 +85,7 @@ func TestInfoStat_String(t *testing.T) {
8685
KernelArch: "x86_64",
8786
}
8887
e := `{"hostname":"test","uptime":3000,"bootTime":1447040000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","kernelVersion":"","kernelArch":"x86_64","virtualizationSystem":"","virtualizationRole":"","hostId":"edfd25ff-3c9c-b1a4-e660-bd826495ad35"}`
89-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "HostInfoStat string is invalid:\ngot %v\nwant %v", v, e)
88+
assert.JSONEqf(t, e, v.String(), "HostInfoStat string is invalid:\ngot %v\nwant %v", v, e)
9089
}
9190

9291
func TestUserStat_String(t *testing.T) {
@@ -97,7 +96,7 @@ func TestUserStat_String(t *testing.T) {
9796
Started: 100,
9897
}
9998
e := `{"user":"user","terminal":"term","host":"host","started":100}`
100-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "UserStat string is invalid: %v", v)
99+
assert.JSONEqf(t, e, v.String(), "UserStat string is invalid: %v", v)
101100
}
102101

103102
func TestGuid(t *testing.T) {

host/host_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func BootTimeWithContext(_ context.Context) (uint64, error) {
136136
return t, nil
137137
}
138138

139-
func PlatformInformationWithContext(_ context.Context) (platform string, family string, version string, err error) {
139+
func PlatformInformationWithContext(_ context.Context) (platform, family, version string, err error) {
140140
platform, family, _, displayVersion, err := platformInformation()
141141
if err != nil {
142142
return "", "", "", err

0 commit comments

Comments
 (0)