Skip to content

Commit 9d29c70

Browse files
authored
Merge pull request #1813 from mmorel-35/gocritic
chore: enable gocritic linter
2 parents 1bf1d3c + dfdd90a commit 9d29c70

19 files changed

+102
-95
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ linters:
88
- durationcheck
99
- errorlint
1010
- gci
11+
- gocritic
1112
- gofmt
1213
- gofumpt
1314
- goimports
@@ -38,6 +39,9 @@ linters-settings:
3839
- standard
3940
- default
4041
- prefix(github.com/shirou)
42+
gocritic:
43+
disabled-checks:
44+
- captLocal
4145
gomodguard:
4246
blocked:
4347
modules:

cpu/cpu_aix_nocgo.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,15 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
105105

106106
ret := InfoStat{}
107107
for _, line := range strings.Split(string(out), "\n") {
108-
if strings.HasPrefix(line, "Number Of Processors:") {
108+
switch {
109+
case strings.HasPrefix(line, "Number Of Processors:"):
109110
p := strings.Fields(line)
110111
if len(p) > 3 {
111112
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
112113
ret.Cores = int32(t)
113114
}
114115
}
115-
} else if strings.HasPrefix(line, "Processor Clock Speed:") {
116+
case strings.HasPrefix(line, "Processor Clock Speed:"):
116117
p := strings.Fields(line)
117118
if len(p) > 4 {
118119
if t, err := strconv.ParseFloat(p[3], 64); err == nil {
@@ -128,13 +129,12 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
128129
}
129130
}
130131
}
131-
break
132-
} else if strings.HasPrefix(line, "System Model:") {
132+
case strings.HasPrefix(line, "System Model:"):
133133
p := strings.Split(string(line), ":")
134134
if p != nil {
135135
ret.VendorID = strings.TrimSpace(p[1])
136136
}
137-
} else if strings.HasPrefix(line, "Processor Type:") {
137+
case strings.HasPrefix(line, "Processor Type:"):
138138
p := strings.Split(string(line), ":")
139139
if p != nil {
140140
c := strings.Split(string(p[1]), "_")

cpu/cpu_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func finishCPUInfo(ctx context.Context, c *InfoStat) {
157157
}
158158
c.Mhz = value / 1000.0 // value is in kHz
159159
if c.Mhz > 9999 {
160-
c.Mhz = c.Mhz / 1000.0 // value in Hz
160+
c.Mhz /= 1000.0 // value in Hz
161161
}
162162
}
163163

cpu/cpu_solaris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
8181
if err != nil {
8282
return nil, fmt.Errorf("cannot parse iowait: %w", err)
8383
}
84-
//not sure how this translates, don't report, add to kernel, something else?
84+
// not sure how this translates, don't report, add to kernel, something else?
8585
/*case "swap":
8686
swap[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
8787
if err != nil {

disk/disk_aix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
3434

3535
ret := ""
3636
// Kind of inefficient, but it works
37-
lines := strings.Split(string(out[:]), "\n")
37+
lines := strings.Split(string(out), "\n")
3838
for line := 1; line < len(lines); line++ {
3939
v := strings.TrimSpace(lines[line])
4040
if strings.HasPrefix(v, "Serial Number...............") {

disk/disk_aix_nocgo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
9898
return &UsageStat{}, common.ErrNotImplementedError
9999
}
100100

101-
hf := strings.Fields(strings.Replace(lines[0], "Mounted on", "Path", -1)) // headers
101+
hf := strings.Fields(strings.ReplaceAll(lines[0], "Mounted on", "Path")) // headers
102102
for line := 1; line < len(lines); line++ {
103103
fs := strings.Fields(lines[line]) // values
104104
for i, header := range hf {
@@ -137,7 +137,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
137137
return nil, err
138138
}
139139
case `%Used`:
140-
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
140+
val, err := strconv.ParseInt(strings.ReplaceAll(fs[i], "%", ""), 10, 32)
141141
if err != nil {
142142
return nil, err
143143
}
@@ -153,7 +153,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
153153
return nil, err
154154
}
155155
case `%Iused`:
156-
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
156+
val, err := strconv.ParseInt(strings.ReplaceAll(fs[i], "%", ""), 10, 32)
157157
if err != nil {
158158
return nil, err
159159
}
@@ -178,7 +178,7 @@ func GetMountFSTypeWithContext(ctx context.Context, mp string) (string, error) {
178178
}
179179

180180
// Kind of inefficient, but it works
181-
lines := strings.Split(string(out[:]), "\n")
181+
lines := strings.Split(string(out), "\n")
182182
for line := 1; line < len(lines); line++ {
183183
fields := strings.Fields(lines[line])
184184
if strings.TrimSpace(fields[0]) == mp {

host/host_aix.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func HostIDWithContext(ctx context.Context) (string, error) {
2424
}
2525

2626
// The command always returns an extra newline, so we make use of Split() to get only the first line
27-
return strings.Split(string(out[:]), "\n")[0], nil
27+
return strings.Split(string(out), "\n")[0], nil
2828
}
2929

3030
func numProcs(_ context.Context) (uint64, error) {
@@ -41,7 +41,7 @@ func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
4141
return 0, errors.New("uptime was not set, so cannot calculate boot time from it")
4242
}
4343

44-
ut = ut * 60
44+
ut *= 60
4545
return timeSince(ut), nil
4646
}
4747

@@ -59,7 +59,7 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
5959
return 0, err
6060
}
6161

62-
return parseUptime(string(out[:])), nil
62+
return parseUptime(string(out)), nil
6363
}
6464

6565
func parseUptime(uptime string) uint64 {
@@ -166,17 +166,17 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
166166
if err != nil {
167167
return "", "", "", err
168168
}
169-
platform = strings.TrimRight(string(out[:]), "\n")
169+
platform = strings.TrimRight(string(out), "\n")
170170

171171
// Set the family
172-
family = strings.TrimRight(string(out[:]), "\n")
172+
family = strings.TrimRight(string(out), "\n")
173173

174174
// Set the version
175175
out, err = invoke.CommandWithContext(ctx, "oslevel")
176176
if err != nil {
177177
return "", "", "", err
178178
}
179-
version = strings.TrimRight(string(out[:]), "\n")
179+
version = strings.TrimRight(string(out), "\n")
180180

181181
return platform, family, version, nil
182182
}
@@ -186,7 +186,7 @@ func KernelVersionWithContext(ctx context.Context) (version string, err error) {
186186
if err != nil {
187187
return "", err
188188
}
189-
version = strings.TrimRight(string(out[:]), "\n")
189+
version = strings.TrimRight(string(out), "\n")
190190

191191
return version, nil
192192
}
@@ -196,7 +196,7 @@ func KernelArch() (arch string, err error) {
196196
if err != nil {
197197
return "", err
198198
}
199-
arch = strings.TrimRight(string(out[:]), "\n")
199+
arch = strings.TrimRight(string(out), "\n")
200200

201201
return arch, nil
202202
}

host/host_linux.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -176,45 +176,47 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
176176
lsb = &lsbStruct{}
177177
}
178178

179-
if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "oracle-release")) {
179+
switch {
180+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "oracle-release")):
180181
platform = "oracle"
181182
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "oracle-release"))
182183
if err == nil {
183184
version = getRedhatishVersion(contents)
184185
}
185186

186-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "enterprise-release")) {
187+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "enterprise-release")):
187188
platform = "oracle"
188189
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "enterprise-release"))
189190
if err == nil {
190191
version = getRedhatishVersion(contents)
191192
}
192-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "slackware-version")) {
193+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "slackware-version")):
193194
platform = "slackware"
194195
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "slackware-version"))
195196
if err == nil {
196197
version = getSlackwareVersion(contents)
197198
}
198-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "debian_version")) {
199-
if lsb.ID == "Ubuntu" {
199+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "debian_version")):
200+
switch lsb.ID {
201+
case "Ubuntu":
200202
platform = "ubuntu"
201203
version = lsb.Release
202-
} else if lsb.ID == "LinuxMint" {
204+
case "LinuxMint":
203205
platform = "linuxmint"
204206
version = lsb.Release
205-
} else if lsb.ID == "Kylin" {
207+
case "Kylin":
206208
platform = "Kylin"
207209
version = lsb.Release
208-
} else if lsb.ID == `"Cumulus Linux"` {
210+
case `"Cumulus Linux"`:
209211
platform = "cumuluslinux"
210212
version = lsb.Release
211-
} else if lsb.ID == "uos" {
213+
case "uos":
212214
platform = "uos"
213215
version = lsb.Release
214-
} else if lsb.ID == "Deepin" {
216+
case "Deepin":
215217
platform = "Deepin"
216218
version = lsb.Release
217-
} else {
219+
default:
218220
if common.PathExistsWithContents("/usr/bin/raspi-config") {
219221
platform = "raspbian"
220222
} else {
@@ -225,65 +227,65 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
225227
version = contents[0]
226228
}
227229
}
228-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "neokylin-release")) {
230+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "neokylin-release")):
229231
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "neokylin-release"))
230232
if err == nil {
231233
version = getRedhatishVersion(contents)
232234
platform = getRedhatishPlatform(contents)
233235
}
234-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "redhat-release")) {
236+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "redhat-release")):
235237
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "redhat-release"))
236238
if err == nil {
237239
version = getRedhatishVersion(contents)
238240
platform = getRedhatishPlatform(contents)
239241
}
240-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "system-release")) {
242+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "system-release")):
241243
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "system-release"))
242244
if err == nil {
243245
version = getRedhatishVersion(contents)
244246
platform = getRedhatishPlatform(contents)
245247
}
246-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "gentoo-release")) {
248+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "gentoo-release")):
247249
platform = "gentoo"
248250
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "gentoo-release"))
249251
if err == nil {
250252
version = getRedhatishVersion(contents)
251253
}
252-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "SuSE-release")) {
254+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "SuSE-release")):
253255
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "SuSE-release"))
254256
if err == nil {
255257
version = getSuseVersion(contents)
256258
platform = getSusePlatform(contents)
257259
}
258260
// TODO: slackware detecion
259-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "arch-release")) {
261+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "arch-release")):
260262
platform = "arch"
261263
version = lsb.Release
262-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "alpine-release")) {
264+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "alpine-release")):
263265
platform = "alpine"
264266
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "alpine-release"))
265267
if err == nil && len(contents) > 0 && contents[0] != "" {
266268
version = contents[0]
267269
}
268-
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "os-release")) {
270+
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "os-release")):
269271
p, v, err := common.GetOSReleaseWithContext(ctx)
270272
if err == nil {
271273
platform = p
272274
version = v
273275
}
274-
} else if lsb.ID == "RedHat" {
276+
case lsb.ID == "RedHat":
275277
platform = "redhat"
276278
version = lsb.Release
277-
} else if lsb.ID == "Amazon" {
279+
case lsb.ID == "Amazon":
278280
platform = "amazon"
279281
version = lsb.Release
280-
} else if lsb.ID == "ScientificSL" {
282+
case lsb.ID == "ScientificSL":
281283
platform = "scientific"
282284
version = lsb.Release
283-
} else if lsb.ID == "XenServer" {
285+
case lsb.ID == "XenServer":
284286
platform = "xenserver"
285287
version = lsb.Release
286-
} else if lsb.ID != "" {
288+
case lsb.ID != "":
287289
platform = strings.ToLower(lsb.ID)
288290
version = lsb.Release
289291
}

host/host_windows.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ func platformInformation() (platform, family, version, displayVersion string, er
174174
if err != nil {
175175
return //nolint:nakedret //FIXME
176176
}
177-
platform = windows.UTF16ToString(regBuf[:])
177+
platform = windows.UTF16ToString(regBuf)
178178
if strings.Contains(platform, "Windows 10") { // check build number to determine whether it's actually Windows 11
179179
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, nil, &bufLen)
180180
if err == nil {
181181
regBuf = make([]uint16, bufLen/2+1)
182182
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
183183
if err == nil {
184-
buildNumberStr := windows.UTF16ToString(regBuf[:])
184+
buildNumberStr := windows.UTF16ToString(regBuf)
185185
if buildNumber, err := strconv.ParseInt(buildNumberStr, 10, 32); err == nil && buildNumber >= 22000 {
186186
platform = strings.Replace(platform, "Windows 10", "Windows 11", 1)
187187
}
@@ -196,7 +196,7 @@ func platformInformation() (platform, family, version, displayVersion string, er
196196
regBuf = make([]uint16, bufLen/2+1)
197197
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CSDVersion`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
198198
if err == nil {
199-
platform += " " + windows.UTF16ToString(regBuf[:])
199+
platform += " " + windows.UTF16ToString(regBuf)
200200
}
201201
}
202202

@@ -213,7 +213,7 @@ func platformInformation() (platform, family, version, displayVersion string, er
213213
if err == nil {
214214
regBuf := make([]uint16, bufLen/2+1)
215215
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`DisplayVersion`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
216-
displayVersion = windows.UTF16ToString(regBuf[:])
216+
displayVersion = windows.UTF16ToString(regBuf)
217217
}
218218

219219
// PlatformFamily

0 commit comments

Comments
 (0)