Skip to content

Commit 38a61f6

Browse files
authored
chore: enable several rules from go-critic (#741)
* chore: enable-all rules from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable assignOp rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable captLocal rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable elseif rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable emptyStringTest rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable ifElseChain rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable regexpSimplify rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable stringsCompare rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable stringXbytes rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> * chore: enable sloppyReassign rule from go-critic Signed-off-by: Matthieu MOREL <[email protected]> --------- Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 2f4b14c commit 38a61f6

17 files changed

+68
-46
lines changed

.golangci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ linters:
33
enable:
44
- errorlint
55
- forbidigo
6+
- gocritic
67
- godot
78
- misspell
89
- revive
@@ -12,6 +13,20 @@ linters:
1213
forbid:
1314
- pattern: ^fmt\.Print.*$
1415
msg: Do not commit print statements.
16+
gocritic:
17+
enable-all: true
18+
disabled-checks:
19+
- commentFormatting
20+
- commentedOutCode
21+
- deferInLoop
22+
- filepathJoin
23+
- hugeParam
24+
- importShadow
25+
- paramTypeCombine
26+
- rangeValCopy
27+
- tooManyResultsChecker
28+
- unnamedResult
29+
- whyNoLint
1530
godot:
1631
exclude:
1732
# Ignore "See: URL".

arp.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ func parseARPEntries(data []byte) ([]ARPEntry, error) {
7373
columns := strings.Fields(line)
7474
width := len(columns)
7575

76-
if width == expectedHeaderWidth || width == 0 {
76+
switch width {
77+
case expectedHeaderWidth, 0:
7778
continue
78-
} else if width == expectedDataWidth {
79+
case expectedDataWidth:
7980
entry, err := parseARPEntry(columns)
8081
if err != nil {
8182
return []ARPEntry{}, fmt.Errorf("%w: Failed to parse ARP entry: %v: %w", ErrFileParse, entry, err)
8283
}
8384
entries = append(entries, entry)
84-
} else {
85+
default:
8586
return []ARPEntry{}, fmt.Errorf("%w: %d columns found, but expected %d: %w", ErrFileParse, width, expectedDataWidth, err)
8687
}
8788

bcache/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func parsePseudoFloat(str string) (float64, error) {
111111
// v4.12-rc3).
112112

113113
// Restore the proper order:
114-
fracPart = fracPart / 10.24
114+
fracPart /= 10.24
115115
return intPart + fracPart, nil
116116
}
117117

buddyinfo.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ func parseBuddyInfo(r io.Reader) ([]BuddyInfo, error) {
6464

6565
if bucketCount == -1 {
6666
bucketCount = arraySize
67-
} else {
68-
if bucketCount != arraySize {
69-
return nil, fmt.Errorf("%w: mismatch in number of buddyinfo buckets, previous count %d, new count %d", ErrFileParse, bucketCount, arraySize)
70-
}
67+
} else if bucketCount != arraySize {
68+
return nil, fmt.Errorf("%w: mismatch in number of buddyinfo buckets, previous count %d, new count %d", ErrFileParse, bucketCount, arraySize)
7169
}
7270

7371
sizes := make([]float64, arraySize)

fscache.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,20 +388,21 @@ func parseFscacheinfo(r io.Reader) (*Fscacheinfo, error) {
388388
}
389389
}
390390
case "CacheOp:":
391-
if strings.Split(fields[1], "=")[0] == "alo" {
391+
switch strings.Split(fields[1], "=")[0] {
392+
case "alo":
392393
err := setFSCacheFields(fields[1:], &m.CacheopAllocationsinProgress, &m.CacheopLookupObjectInProgress,
393394
&m.CacheopLookupCompleteInPorgress, &m.CacheopGrabObjectInProgress)
394395
if err != nil {
395396
return &m, err
396397
}
397-
} else if strings.Split(fields[1], "=")[0] == "inv" {
398+
case "inv":
398399
err := setFSCacheFields(fields[1:], &m.CacheopInvalidations, &m.CacheopUpdateObjectInProgress,
399400
&m.CacheopDropObjectInProgress, &m.CacheopPutObjectInProgress, &m.CacheopAttributeChangeInProgress,
400401
&m.CacheopSyncCacheInProgress)
401402
if err != nil {
402403
return &m, err
403404
}
404-
} else {
405+
default:
405406
err := setFSCacheFields(fields[1:], &m.CacheopReadOrAllocPageInProgress, &m.CacheopReadOrAllocPagesInProgress,
406407
&m.CacheopAllocatePageInProgress, &m.CacheopAllocatePagesInProgress, &m.CacheopWritePagesInProgress,
407408
&m.CacheopUncachePagesInProgress, &m.CacheopDissociatePagesInProgress)

iscsi/get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (fs FS) GetRBDMatch(rbdNumber string, poolImage string) (*RBD, error) {
208208
}
209209
systemImage = strings.TrimSpace(string(bSystemImage))
210210

211-
if strings.Compare(strconv.FormatInt(int64(systemRbdNumber), 10), rbdNumber) == 0 &&
211+
if strconv.FormatInt(int64(systemRbdNumber), 10) == rbdNumber &&
212212
matchPoolImage(systemPool, systemImage, poolImage) {
213213
rbd.Pool = systemPool
214214
rbd.Image = systemImage
@@ -241,5 +241,5 @@ func (fs FS) GetRDMCPPath(rdmcpNumber string, objectName string) (*RDMCP, error)
241241

242242
func matchPoolImage(pool string, image string, matchPoolImage string) (isEqual bool) {
243243
var poolImage = fmt.Sprintf("%s-%s", pool, image)
244-
return strings.Compare(poolImage, matchPoolImage) == 0
244+
return poolImage == matchPoolImage
245245
}

mdstat.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
129129

130130
// Append recovery and resyncing state info.
131131
if recovering || resyncing || checking || reshaping {
132-
if recovering {
132+
switch {
133+
case recovering:
133134
state = "recovering"
134-
} else if reshaping {
135+
case reshaping:
135136
state = "reshaping"
136-
} else if checking {
137+
case checking:
137138
state = "checking"
138-
} else {
139+
default:
139140
state = "resyncing"
140141
}
141142

proc_cgroups.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ type CgroupSummary struct {
4040

4141
// parseCgroupSummary parses each line of the /proc/cgroup file
4242
// Line format is `subsys_name hierarchy num_cgroups enabled`.
43-
func parseCgroupSummaryString(CgroupSummaryStr string) (*CgroupSummary, error) {
43+
func parseCgroupSummaryString(cgroupSummaryStr string) (*CgroupSummary, error) {
4444
var err error
4545

46-
fields := strings.Fields(CgroupSummaryStr)
46+
fields := strings.Fields(cgroupSummaryStr)
4747
// require at least 4 fields
4848
if len(fields) < 4 {
49-
return nil, fmt.Errorf("%w: 4+ fields required, found %d fields in cgroup info string: %s", ErrFileParse, len(fields), CgroupSummaryStr)
49+
return nil, fmt.Errorf("%w: 4+ fields required, found %d fields in cgroup info string: %s", ErrFileParse, len(fields), cgroupSummaryStr)
5050
}
5151

5252
CgroupSummary := &CgroupSummary{

proc_fdinfo.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,16 @@ func (p Proc) FDInfo(fd string) (*ProcFDInfo, error) {
6060
scanner := bufio.NewScanner(bytes.NewReader(data))
6161
for scanner.Scan() {
6262
text = scanner.Text()
63-
if rPos.MatchString(text) {
63+
switch {
64+
case rPos.MatchString(text):
6465
pos = rPos.FindStringSubmatch(text)[1]
65-
} else if rFlags.MatchString(text) {
66+
case rFlags.MatchString(text):
6667
flags = rFlags.FindStringSubmatch(text)[1]
67-
} else if rMntID.MatchString(text) {
68+
case rMntID.MatchString(text):
6869
mntid = rMntID.FindStringSubmatch(text)[1]
69-
} else if rIno.MatchString(text) {
70+
case rIno.MatchString(text):
7071
ino = rIno.FindStringSubmatch(text)[1]
71-
} else if rInotify.MatchString(text) {
72+
case rInotify.MatchString(text):
7273
newInotify, err := parseInotifyInfo(text)
7374
if err != nil {
7475
return nil, err

proc_limits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const (
7474
)
7575

7676
var (
77-
limitsMatch = regexp.MustCompile(`(Max \w+\s{0,1}?\w*\s{0,1}\w*)\s{2,}(\w+)\s+(\w+)`)
77+
limitsMatch = regexp.MustCompile(`(Max \w+\s??\w*\s?\w*)\s{2,}(\w+)\s+(\w+)`)
7878
)
7979

8080
// NewLimits returns the current soft limits of the process.

0 commit comments

Comments
 (0)