Skip to content

Commit b5847a0

Browse files
committed
enhance linters configuration and improve code readability
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent d7c3b9e commit b5847a0

10 files changed

Lines changed: 51 additions & 24 deletions

File tree

.golangci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ version: "2"
33
linters:
44
enable:
55
- errorlint
6+
- gocritic
7+
- modernize
8+
- revive
69
- unconvert
710
- unparam
811
exclusions:
@@ -11,6 +14,28 @@ linters:
1114
- comments
1215
- std-error-handling
1316
settings:
17+
gocritic:
18+
disabled-checks:
19+
- appendAssign
20+
- builtinShadow
21+
- deferInLoop
22+
- hugeParam
23+
- unnamedResult
24+
- whyNoLint
25+
enable-all: true
26+
revive:
27+
enable-all-rules: false
28+
enable-default-rules: true
29+
max-open-files: 2048
30+
rules:
31+
- name: dot-imports
32+
disabled: true
33+
- name: package-comments
34+
disabled: true
35+
- name: redefines-builtin-id
36+
disabled: true
37+
- name: var-naming
38+
disabled: true
1439
staticcheck:
1540
# Enable all options, with some exceptions.
1641
# For defaults, see https://golangci-lint.run/usage/linters/#staticcheck
@@ -24,3 +49,7 @@ formatters:
2449
- gofumpt
2550
exclusions:
2651
generated: disable
52+
53+
issues:
54+
max-issues-per-linter: 0
55+
max-same-issues: 0

capability/capability_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ func mkString(c Capabilities, max CapType) (ret string) {
7777
ret = "{"
7878
for i := CapType(1); i <= max; i <<= 1 {
7979
ret += " " + i.String() + "=\""
80-
if c.Empty(i) {
80+
switch {
81+
case c.Empty(i):
8182
ret += "empty"
82-
} else if c.Full(i) {
83+
case c.Full(i):
8384
ret += "full"
84-
} else {
85+
default:
8586
ret += c.StringCap(i)
8687
}
8788
ret += "\""

capability/capability_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestNewPid2Load(t *testing.T) {
133133
// Assuming that at least bounding set is not empty.
134134
bset := c.StringCap(BOUNDING)
135135
t.Logf("Bounding set: %s", bset)
136-
if len(bset) == 0 {
136+
if bset == "" {
137137
t.Fatal("loaded bounding set: want non-empty, got empty")
138138
}
139139
}

devices/device_unix_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestDeviceFromPathLstatFailure(t *testing.T) {
4242
testError := errors.New("test error")
4343

4444
// Override unix.Lstat to inject error.
45-
unixLstat = func(path string, stat *unix.Stat_t) error {
45+
unixLstat = func(_ string, _ *unix.Stat_t) error {
4646
return testError
4747
}
4848
defer cleanupTest()
@@ -57,7 +57,7 @@ func TestHostDevicesIoutilReadDirFailure(t *testing.T) {
5757
testError := errors.New("test error")
5858

5959
// Override os.ReadDir to inject error.
60-
osReadDir = func(dirname string) ([]fs.DirEntry, error) {
60+
osReadDir = func(_ string) ([]fs.DirEntry, error) {
6161
return nil, testError
6262
}
6363
defer cleanupTest()
@@ -73,7 +73,7 @@ func TestHostDevicesIoutilReadDirDeepFailure(t *testing.T) {
7373
called := false
7474

7575
// Override os.ReadDir to inject error after the first call.
76-
osReadDir = func(dirname string) ([]fs.DirEntry, error) {
76+
osReadDir = func(_ string) ([]fs.DirEntry, error) {
7777
if called {
7878
return nil, testError
7979
}

mount/mounter_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func ensureUnmount(t *testing.T, mnt string) {
106106
}
107107

108108
// validateMount checks that mnt has the given options
109-
func validateMount(t *testing.T, mnt string, opts, optional, vfs string) {
109+
func validateMount(t *testing.T, mnt, opts, optional, vfs string) {
110110
info, err := mountinfo.GetMounts(nil)
111111
if err != nil {
112112
t.Fatal(err)

mount/sharedsubtree_linux_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ func TestSubtreeUnbindable(t *testing.T) {
313313
} else if err == nil {
314314
t.Fatalf("%q should not have been bindable", sourceDir)
315315
}
316-
defer func() {
317-
if err := Unmount(targetDir); err != nil {
318-
t.Fatal(err)
319-
}
320-
}()
316+
if err := Unmount(targetDir); err != nil {
317+
t.Fatal(err)
318+
}
321319
}

mountinfo/mounted_linux_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var testMounts = []testMount{
6060
{
6161
desc: "non-existent path",
6262
isNotExist: true,
63-
prepare: func(t *testing.T) string {
63+
prepare: func(_ *testing.T) string {
6464
return "/non/existent/path"
6565
},
6666
},
@@ -385,11 +385,10 @@ func TestMountedBy(t *testing.T) {
385385
t.Errorf("%s: expected false on error", name)
386386
}
387387
} else if mounted != tc.isMount {
388-
if tc.isBind && strings.HasSuffix(name, "mountedByStat") {
389-
// mountedByStat can not detect bind mounts.
390-
} else {
388+
if !tc.isBind || !strings.HasSuffix(name, "mountedByStat") {
391389
t.Errorf("%s: expected %v, got %v", name, tc.isMount, mounted)
392390
}
391+
// mountedByStat can not detect bind mounts.
393392
}
394393
checked = true
395394
}

reexec/reexec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func TestDispatch(t *testing.T) {
212212
}{
213213
{
214214
name: "not-registered",
215-
ctx: func(t *testing.T) context.Context {
215+
ctx: func(_ *testing.T) context.Context {
216216
return context.Background()
217217
},
218218
check: func(t *testing.T, ok bool, err error) {

signal/signal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ func ParseSignal(rawSignal string) (syscall.Signal, error) {
4141
}
4242
return syscall.Signal(s), nil
4343
}
44-
signal, ok := SignalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
44+
sig, ok := SignalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
4545
if !ok {
4646
return -1, fmt.Errorf("invalid signal: %s", rawSignal)
4747
}
48-
return signal, nil
48+
return sig, nil
4949
}
5050

5151
// ValidSignalForPlatform returns true if a signal is valid on the platform

user/idtools_unix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll, onlyNew bool) e
5151
paths = append(paths, dirPath)
5252
}
5353
}
54-
if err = os.MkdirAll(path, mode); err != nil {
54+
if err := os.MkdirAll(path, mode); err != nil {
5555
return err
5656
}
57-
} else if err = os.Mkdir(path, mode); err != nil {
57+
} else if err := os.Mkdir(path, mode); err != nil {
5858
return err
5959
}
6060
// even if it existed, we will chown the requested path + any subpaths that
6161
// didn't exist when we called MkdirAll
6262
for _, pathComponent := range paths {
63-
if err = setPermissions(pathComponent, mode, uid, gid, nil); err != nil {
63+
if err := setPermissions(pathComponent, mode, uid, gid, nil); err != nil {
6464
return err
6565
}
6666
}
@@ -137,7 +137,7 @@ func lookupSubRangesFile(path string, usr User) ([]IDMap, error) {
137137
ParentID: idrange.SubID,
138138
Count: idrange.Count,
139139
})
140-
containerID = containerID + idrange.Count
140+
containerID += idrange.Count
141141
}
142142
return idMap, nil
143143
}

0 commit comments

Comments
 (0)