Skip to content
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

enable all rules of perfsprint linter #1762

Merged
merged 1 commit into from
Dec 23, 2024
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
12 changes: 12 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ linters:
- misspell
- nakedret
- nolintlint
- perfsprint
- predeclared
- revive
- testifylint
Expand All @@ -57,6 +58,17 @@ linters-settings:
recommandations:
- io
- os
perfsprint:
# Optimizes even if it requires an int or uint type cast.
int-conversion: true
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
err-error: true
# Optimizes `fmt.Errorf`.
errorf: true
# Optimizes `fmt.Sprintf` with only one argument.
sprintf1: true
# Optimizes into strings concatenation.
strconcat: true
testifylint:
disable:
- expected-actual
Expand Down
3 changes: 2 additions & 1 deletion cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cpu
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"runtime"
Expand Down Expand Up @@ -195,7 +196,7 @@ func percentUsedFromLastCallWithContext(ctx context.Context, percpu bool) ([]flo
}

if lastTimes == nil {
return nil, fmt.Errorf("error getting times for cpu percent. lastTimes was nil")
return nil, errors.New("error getting times for cpu percent. lastTimes was nil")
}
return calculateAllBusy(lastTimes, cpuTimes)
}
8 changes: 4 additions & 4 deletions internal/common/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package common

import (
"context"
"fmt"
"errors"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -102,7 +102,7 @@ func BootTimeWithContext(ctx context.Context, enableCache bool) (uint64, error)
currentTime := float64(time.Now().UnixNano()) / float64(time.Second)

if len(lines) != 1 {
return 0, fmt.Errorf("wrong uptime format")
return 0, errors.New("wrong uptime format")
}
f := strings.Fields(lines[0])
b, err := strconv.ParseFloat(f[0], 64)
Expand Down Expand Up @@ -142,7 +142,7 @@ func readBootTimeStat(ctx context.Context) (uint64, error) {
if strings.HasPrefix(line, "btime") {
f := strings.Fields(line)
if len(f) != 2 {
return 0, fmt.Errorf("wrong btime format")
return 0, errors.New("wrong btime format")
}
b, err := strconv.ParseInt(f[1], 10, 64)
if err != nil {
Expand All @@ -151,7 +151,7 @@ func readBootTimeStat(ctx context.Context) (uint64, error) {
t := uint64(b)
return t, nil
}
return 0, fmt.Errorf("could not find btime")
return 0, errors.New("could not find btime")
}

func Virtualization() (string, string, error) {
Expand Down
2 changes: 1 addition & 1 deletion net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ func parseIPv6HexString(src []byte) (net.IP, error) {

func parseIPv6HexStringWithContext(ctx context.Context, src []byte) (net.IP, error) {
if len(src) != 16 {
return nil, fmt.Errorf("invalid IPv6 string")
return nil, errors.New("invalid IPv6 string")
}

buf := make([]byte, 0, 16)
Expand Down
2 changes: 1 addition & 1 deletion process/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestSplitProcStat(t *testing.T) {
for _, expectedName := range cases {
statLineContent[commandNameIndex-1] = "(" + expectedName + ")"
statLine := strings.Join(statLineContent, " ")
t.Run(fmt.Sprintf("name: %s", expectedName), func(t *testing.T) {
t.Run("name: "+expectedName, func(t *testing.T) {
parsedStatLine := splitProcStat([]byte(statLine))
assert.Equal(t, expectedName, parsedStatLine[commandNameIndex])
for _, idx := range consideredFields {
Expand Down
Loading