Skip to content

Commit f5aa9e4

Browse files
authored
chore: enable perfsprint linter (#676)
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 367311b commit f5aa9e4

File tree

7 files changed

+31
-20
lines changed

7 files changed

+31
-20
lines changed

.golangci.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ linters:
2020
- misspell
2121
# Disabled because of https://github.com/argoproj/argo-cd/issues/21705
2222
# - nolintlint
23-
# - perfsprint
23+
- perfsprint
2424
- revive
2525
- staticcheck
2626
- testifylint
@@ -45,6 +45,17 @@ linters-settings:
4545
- wrapperFunc #FIXME
4646
goimports:
4747
local-prefixes: github.com/argoproj/gitops-engine
48+
perfsprint:
49+
# Optimizes even if it requires an int or uint type cast.
50+
int-conversion: true
51+
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
52+
err-error: true
53+
# Optimizes `fmt.Errorf`.
54+
errorf: true
55+
# Optimizes `fmt.Sprintf` with only one argument.
56+
sprintf1: true
57+
# Optimizes into strings concatenation.
58+
strconcat: true
4859
revive:
4960
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
5061
rules:

pkg/cache/cluster_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cache
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"sort"
78
"strings"
@@ -625,7 +626,7 @@ metadata:
625626
convertToVersionWasCalled = true
626627

627628
if testCaseCopy.localConvertFails {
628-
return nil, fmt.Errorf("failed to convert resource client-side")
629+
return nil, errors.New("failed to convert resource client-side")
629630
}
630631

631632
return obj, nil

pkg/diff/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func ServerSideDiff(config, live *unstructured.Unstructured, opts ...Option) (*D
161161
func serverSideDiff(config, live *unstructured.Unstructured, opts ...Option) (*DiffResult, error) {
162162
o := applyOptions(opts)
163163
if o.serverSideDryRunner == nil {
164-
return nil, fmt.Errorf("serverSideDryRunner is null")
164+
return nil, errors.New("serverSideDryRunner is null")
165165
}
166166
predictedLiveStr, err := o.serverSideDryRunner.Run(context.Background(), config, o.manager)
167167
if err != nil {

pkg/diff/diff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func printDiffInternal(name string, live *unstructured.Unstructured, target *uns
6363
if err != nil {
6464
return nil, err
6565
}
66-
liveFile := filepath.Join(tempDir, fmt.Sprintf("%s-live.yaml", name))
66+
liveFile := filepath.Join(tempDir, name+"-live.yaml")
6767
liveData := []byte("")
6868
if live != nil {
6969
liveData, err = yaml.Marshal(live)

pkg/sync/sync_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ const (
299299
// getOperationPhase returns a hook status from an _live_ unstructured object
300300
func (sc *syncContext) getOperationPhase(hook *unstructured.Unstructured) (common.OperationPhase, string, error) {
301301
phase := common.OperationSucceeded
302-
message := fmt.Sprintf("%s created", hook.GetName())
302+
message := hook.GetName() + " created"
303303

304304
resHealth, err := health.GetResourceHealth(hook, sc.healthOverride)
305305
if err != nil {

pkg/sync/sync_context_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7-
"fmt"
87
"net/http"
98
"net/http/httptest"
109
"reflect"
@@ -107,7 +106,7 @@ func TestSyncValidate(t *testing.T) {
107106

108107
func TestSyncNotPermittedNamespace(t *testing.T) {
109108
syncCtx := newTestSyncCtx(nil, WithPermissionValidator(func(_ *unstructured.Unstructured, _ *v1.APIResource) error {
110-
return fmt.Errorf("not permitted in project")
109+
return errors.New("not permitted in project")
111110
}))
112111
targetPod := NewPod()
113112
targetPod.SetNamespace("kube-system")
@@ -307,7 +306,7 @@ func TestSyncCreateFailure(t *testing.T) {
307306
Commands: map[string]kubetest.KubectlOutput{
308307
testSvc.GetName(): {
309308
Output: "",
310-
Err: fmt.Errorf("foo"),
309+
Err: errors.New("foo"),
311310
},
312311
},
313312
}
@@ -316,7 +315,7 @@ func TestSyncCreateFailure(t *testing.T) {
316315
Commands: map[string]kubetest.KubectlOutput{
317316
testSvc.GetName(): {
318317
Output: "",
319-
Err: fmt.Errorf("foo"),
318+
Err: errors.New("foo"),
320319
},
321320
},
322321
}
@@ -457,7 +456,7 @@ func TestSyncPruneFailure(t *testing.T) {
457456
Commands: map[string]kubetest.KubectlOutput{
458457
"test-service": {
459458
Output: "",
460-
Err: fmt.Errorf("foo"),
459+
Err: errors.New("foo"),
461460
},
462461
},
463462
}
@@ -466,7 +465,7 @@ func TestSyncPruneFailure(t *testing.T) {
466465
Commands: map[string]kubetest.KubectlOutput{
467466
"test-service": {
468467
Output: "",
469-
Err: fmt.Errorf("foo"),
468+
Err: errors.New("foo"),
470469
},
471470
},
472471
}
@@ -1195,11 +1194,11 @@ func TestSyncFailureHookWithFailedSync(t *testing.T) {
11951194
})
11961195
syncCtx.hooks = []*unstructured.Unstructured{newHook(synccommon.HookTypeSyncFail)}
11971196
mockKubectl := &kubetest.MockKubectlCmd{
1198-
Commands: map[string]kubetest.KubectlOutput{pod.GetName(): {Err: fmt.Errorf("")}},
1197+
Commands: map[string]kubetest.KubectlOutput{pod.GetName(): {Err: errors.New("")}},
11991198
}
12001199
syncCtx.kubectl = mockKubectl
12011200
mockResourceOps := kubetest.MockResourceOps{
1202-
Commands: map[string]kubetest.KubectlOutput{pod.GetName(): {Err: fmt.Errorf("")}},
1201+
Commands: map[string]kubetest.KubectlOutput{pod.GetName(): {Err: errors.New("")}},
12031202
}
12041203
syncCtx.resourceOps = &mockResourceOps
12051204

@@ -1248,18 +1247,18 @@ func TestRunSyncFailHooksFailed(t *testing.T) {
12481247
mockKubectl := &kubetest.MockKubectlCmd{
12491248
Commands: map[string]kubetest.KubectlOutput{
12501249
// Fail operation
1251-
pod.GetName(): {Err: fmt.Errorf("")},
1250+
pod.GetName(): {Err: errors.New("")},
12521251
// Fail a single SyncFail hook
1253-
failedSyncFailHook.GetName(): {Err: fmt.Errorf("")},
1252+
failedSyncFailHook.GetName(): {Err: errors.New("")},
12541253
},
12551254
}
12561255
syncCtx.kubectl = mockKubectl
12571256
mockResourceOps := kubetest.MockResourceOps{
12581257
Commands: map[string]kubetest.KubectlOutput{
12591258
// Fail operation
1260-
pod.GetName(): {Err: fmt.Errorf("")},
1259+
pod.GetName(): {Err: errors.New("")},
12611260
// Fail a single SyncFail hook
1262-
failedSyncFailHook.GetName(): {Err: fmt.Errorf("")},
1261+
failedSyncFailHook.GetName(): {Err: errors.New("")},
12631262
},
12641263
}
12651264
syncCtx.resourceOps = &mockResourceOps

pkg/utils/kube/kube.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,17 +410,17 @@ func GetDeploymentReplicas(u *unstructured.Unstructured) *int64 {
410410
// RetryUntilSucceed keep retrying given action with specified interval until action succeed or specified context is done.
411411
func RetryUntilSucceed(ctx context.Context, interval time.Duration, desc string, log logr.Logger, action func() error) {
412412
pollErr := wait.PollUntilContextCancel(ctx, interval, true, func(_ context.Context) (bool /*done*/, error) {
413-
log.V(1).Info(fmt.Sprintf("Start %s", desc))
413+
log.V(1).Info("Start " + desc)
414414
err := action()
415415
if err == nil {
416-
log.V(1).Info(fmt.Sprintf("Completed %s", desc))
416+
log.V(1).Info("Completed " + desc)
417417
return true, nil
418418
}
419419
log.V(1).Info(fmt.Sprintf("Failed to %s: %+v, retrying in %v", desc, err, interval))
420420
return false, nil
421421
})
422422
if pollErr != nil {
423423
// The only error that can happen here is wait.ErrWaitTimeout if ctx is done.
424-
log.V(1).Info(fmt.Sprintf("Stop retrying %s", desc))
424+
log.V(1).Info("Stop retrying " + desc)
425425
}
426426
}

0 commit comments

Comments
 (0)