Skip to content

chore: enable gocritic linter #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ linters:
- durationcheck
- errorlint
- gci
- gocritic
- gofmt
- gofumpt
- goimports
Expand Down Expand Up @@ -38,6 +39,9 @@ linters-settings:
- standard
- default
- prefix(github.com/shirou)
gocritic:
disabled-checks:
- captLocal
gomodguard:
blocked:
modules:
Expand Down
10 changes: 5 additions & 5 deletions cpu/cpu_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {

ret := InfoStat{}
for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "Number Of Processors:") {
switch {
case strings.HasPrefix(line, "Number Of Processors:"):
p := strings.Fields(line)
if len(p) > 3 {
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
ret.Cores = int32(t)
}
}
} else if strings.HasPrefix(line, "Processor Clock Speed:") {
case strings.HasPrefix(line, "Processor Clock Speed:"):
p := strings.Fields(line)
if len(p) > 4 {
if t, err := strconv.ParseFloat(p[3], 64); err == nil {
Expand All @@ -128,13 +129,12 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
}
}
}
break
} else if strings.HasPrefix(line, "System Model:") {
case strings.HasPrefix(line, "System Model:"):
p := strings.Split(string(line), ":")
if p != nil {
ret.VendorID = strings.TrimSpace(p[1])
}
} else if strings.HasPrefix(line, "Processor Type:") {
case strings.HasPrefix(line, "Processor Type:"):
p := strings.Split(string(line), ":")
if p != nil {
c := strings.Split(string(p[1]), "_")
Expand Down
2 changes: 1 addition & 1 deletion cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func finishCPUInfo(ctx context.Context, c *InfoStat) {
}
c.Mhz = value / 1000.0 // value is in kHz
if c.Mhz > 9999 {
c.Mhz = c.Mhz / 1000.0 // value in Hz
c.Mhz /= 1000.0 // value in Hz
}
}

Expand Down
2 changes: 1 addition & 1 deletion cpu/cpu_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
if err != nil {
return nil, fmt.Errorf("cannot parse iowait: %w", err)
}
//not sure how this translates, don't report, add to kernel, something else?
// not sure how this translates, don't report, add to kernel, something else?
/*case "swap":
swap[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {

ret := ""
// Kind of inefficient, but it works
lines := strings.Split(string(out[:]), "\n")
lines := strings.Split(string(out), "\n")
for line := 1; line < len(lines); line++ {
v := strings.TrimSpace(lines[line])
if strings.HasPrefix(v, "Serial Number...............") {
Expand Down
8 changes: 4 additions & 4 deletions disk/disk_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return &UsageStat{}, common.ErrNotImplementedError
}

hf := strings.Fields(strings.Replace(lines[0], "Mounted on", "Path", -1)) // headers
hf := strings.Fields(strings.ReplaceAll(lines[0], "Mounted on", "Path")) // headers
for line := 1; line < len(lines); line++ {
fs := strings.Fields(lines[line]) // values
for i, header := range hf {
Expand Down Expand Up @@ -137,7 +137,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, err
}
case `%Used`:
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
val, err := strconv.ParseInt(strings.ReplaceAll(fs[i], "%", ""), 10, 32)
if err != nil {
return nil, err
}
Expand All @@ -153,7 +153,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, err
}
case `%Iused`:
val, err := strconv.ParseInt(strings.Replace(fs[i], "%", "", -1), 10, 32)
val, err := strconv.ParseInt(strings.ReplaceAll(fs[i], "%", ""), 10, 32)
if err != nil {
return nil, err
}
Expand All @@ -178,7 +178,7 @@ func GetMountFSTypeWithContext(ctx context.Context, mp string) (string, error) {
}

// Kind of inefficient, but it works
lines := strings.Split(string(out[:]), "\n")
lines := strings.Split(string(out), "\n")
for line := 1; line < len(lines); line++ {
fields := strings.Fields(lines[line])
if strings.TrimSpace(fields[0]) == mp {
Expand Down
16 changes: 8 additions & 8 deletions host/host_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func HostIDWithContext(ctx context.Context) (string, error) {
}

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

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

ut = ut * 60
ut *= 60
return timeSince(ut), nil
}

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

return parseUptime(string(out[:])), nil
return parseUptime(string(out)), nil
}

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

// Set the family
family = strings.TrimRight(string(out[:]), "\n")
family = strings.TrimRight(string(out), "\n")

// Set the version
out, err = invoke.CommandWithContext(ctx, "oslevel")
if err != nil {
return "", "", "", err
}
version = strings.TrimRight(string(out[:]), "\n")
version = strings.TrimRight(string(out), "\n")

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

return version, nil
}
Expand All @@ -196,7 +196,7 @@ func KernelArch() (arch string, err error) {
if err != nil {
return "", err
}
arch = strings.TrimRight(string(out[:]), "\n")
arch = strings.TrimRight(string(out), "\n")

return arch, nil
}
Expand Down
50 changes: 26 additions & 24 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,45 +176,47 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
lsb = &lsbStruct{}
}

if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "oracle-release")) {
switch {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "oracle-release")):
platform = "oracle"
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "oracle-release"))
if err == nil {
version = getRedhatishVersion(contents)
}

} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "enterprise-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "enterprise-release")):
platform = "oracle"
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "enterprise-release"))
if err == nil {
version = getRedhatishVersion(contents)
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "slackware-version")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "slackware-version")):
platform = "slackware"
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "slackware-version"))
if err == nil {
version = getSlackwareVersion(contents)
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "debian_version")) {
if lsb.ID == "Ubuntu" {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "debian_version")):
switch lsb.ID {
case "Ubuntu":
platform = "ubuntu"
version = lsb.Release
} else if lsb.ID == "LinuxMint" {
case "LinuxMint":
platform = "linuxmint"
version = lsb.Release
} else if lsb.ID == "Kylin" {
case "Kylin":
platform = "Kylin"
version = lsb.Release
} else if lsb.ID == `"Cumulus Linux"` {
case `"Cumulus Linux"`:
platform = "cumuluslinux"
version = lsb.Release
} else if lsb.ID == "uos" {
case "uos":
platform = "uos"
version = lsb.Release
} else if lsb.ID == "Deepin" {
case "Deepin":
platform = "Deepin"
version = lsb.Release
} else {
default:
if common.PathExistsWithContents("/usr/bin/raspi-config") {
platform = "raspbian"
} else {
Expand All @@ -225,65 +227,65 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
version = contents[0]
}
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "neokylin-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "neokylin-release")):
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "neokylin-release"))
if err == nil {
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "redhat-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "redhat-release")):
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "redhat-release"))
if err == nil {
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "system-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "system-release")):
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "system-release"))
if err == nil {
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "gentoo-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "gentoo-release")):
platform = "gentoo"
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "gentoo-release"))
if err == nil {
version = getRedhatishVersion(contents)
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "SuSE-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "SuSE-release")):
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "SuSE-release"))
if err == nil {
version = getSuseVersion(contents)
platform = getSusePlatform(contents)
}
// TODO: slackware detecion
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "arch-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "arch-release")):
platform = "arch"
version = lsb.Release
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "alpine-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "alpine-release")):
platform = "alpine"
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "alpine-release"))
if err == nil && len(contents) > 0 && contents[0] != "" {
version = contents[0]
}
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "os-release")) {
case common.PathExistsWithContents(common.HostEtcWithContext(ctx, "os-release")):
p, v, err := common.GetOSReleaseWithContext(ctx)
if err == nil {
platform = p
version = v
}
} else if lsb.ID == "RedHat" {
case lsb.ID == "RedHat":
platform = "redhat"
version = lsb.Release
} else if lsb.ID == "Amazon" {
case lsb.ID == "Amazon":
platform = "amazon"
version = lsb.Release
} else if lsb.ID == "ScientificSL" {
case lsb.ID == "ScientificSL":
platform = "scientific"
version = lsb.Release
} else if lsb.ID == "XenServer" {
case lsb.ID == "XenServer":
platform = "xenserver"
version = lsb.Release
} else if lsb.ID != "" {
case lsb.ID != "":
platform = strings.ToLower(lsb.ID)
version = lsb.Release
}
Expand Down
8 changes: 4 additions & 4 deletions host/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ func platformInformation() (platform, family, version, displayVersion string, er
if err != nil {
return //nolint:nakedret //FIXME
}
platform = windows.UTF16ToString(regBuf[:])
platform = windows.UTF16ToString(regBuf)
if strings.Contains(platform, "Windows 10") { // check build number to determine whether it's actually Windows 11
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, nil, &bufLen)
if err == nil {
regBuf = make([]uint16, bufLen/2+1)
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CurrentBuildNumber`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
if err == nil {
buildNumberStr := windows.UTF16ToString(regBuf[:])
buildNumberStr := windows.UTF16ToString(regBuf)
if buildNumber, err := strconv.ParseInt(buildNumberStr, 10, 32); err == nil && buildNumber >= 22000 {
platform = strings.Replace(platform, "Windows 10", "Windows 11", 1)
}
Expand All @@ -196,7 +196,7 @@ func platformInformation() (platform, family, version, displayVersion string, er
regBuf = make([]uint16, bufLen/2+1)
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`CSDVersion`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
if err == nil {
platform += " " + windows.UTF16ToString(regBuf[:])
platform += " " + windows.UTF16ToString(regBuf)
}
}

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

// PlatformFamily
Expand Down
Loading