Skip to content

Commit 2acaa44

Browse files
authored
[chore]: enable perfsprint linter (#1039)
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent a245442 commit 2acaa44

File tree

9 files changed

+32
-17
lines changed

9 files changed

+32
-17
lines changed

.golangci.yml

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ linters:
1717
- goimports
1818
- gosec
1919
- misspell
20+
- perfsprint
2021
- revive
2122
- testifylint
2223
- unconvert
@@ -36,6 +37,17 @@ linters-settings:
3637
- G115
3738
misspell:
3839
locale: US
40+
perfsprint:
41+
# Optimizes even if it requires an int or uint type cast.
42+
int-conversion: true
43+
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
44+
err-error: true
45+
# Optimizes `fmt.Errorf`.
46+
errorf: true
47+
# Optimizes `fmt.Sprintf` with only one argument.
48+
sprintf1: true
49+
# Optimizes into strings concatenation.
50+
strconcat: true
3951
testifylint:
4052
disable:
4153
- float-compare

examples/dyplomat/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (rt *TokenRoundtripper) RoundTrip(req *http.Request) (*http.Response, error
150150

151151
}
152152

153-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.Token))
153+
req.Header.Set("Authorization", "Bearer "+token.Token)
154154
return rt.RoundTripper.RoundTrip(req)
155155
}
156156

pkg/cache/v3/cache.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cache
1818
import (
1919
"context"
2020
"errors"
21-
"fmt"
2221
"sync/atomic"
2322

2423
"google.golang.org/protobuf/proto"
@@ -362,7 +361,7 @@ func (r *PassthroughResponse) GetVersion() (string, error) {
362361
if discoveryResponse != nil {
363362
return discoveryResponse.GetVersionInfo(), nil
364363
}
365-
return "", fmt.Errorf("DiscoveryResponse is nil")
364+
return "", errors.New("DiscoveryResponse is nil")
366365
}
367366

368367
func (r *PassthroughResponse) GetContext() context.Context {
@@ -375,7 +374,7 @@ func (r *DeltaPassthroughResponse) GetSystemVersion() (string, error) {
375374
if deltaDiscoveryResponse != nil {
376375
return deltaDiscoveryResponse.GetSystemVersionInfo(), nil
377376
}
378-
return "", fmt.Errorf("DeltaDiscoveryResponse is nil")
377+
return "", errors.New("DeltaDiscoveryResponse is nil")
379378
}
380379

381380
// NextVersionMap returns the version map from a DeltaPassthroughResponse

pkg/cache/v3/delta_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"reflect"
7+
"strconv"
78
"testing"
89
"time"
910

@@ -181,7 +182,7 @@ func TestConcurrentSetDeltaWatch(t *testing.T) {
181182
func(i int) {
182183
t.Run(fmt.Sprintf("worker%d", i), func(t *testing.T) {
183184
t.Parallel()
184-
id := fmt.Sprintf("%d", i%2)
185+
id := strconv.Itoa(i % 2)
185186
responses := make(chan cache.DeltaResponse, 1)
186187
if i < 25 {
187188
snap, err := cache.NewSnapshot("", map[rsrc.Type][]types.Resource{})

pkg/cache/v3/linear_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"errors"
1919
"fmt"
2020
"reflect"
21+
"strconv"
2122
"testing"
2223
"time"
2324

@@ -397,12 +398,12 @@ func TestLinearConcurrentSetWatch(t *testing.T) {
397398
func(i int) {
398399
t.Run(fmt.Sprintf("worker%d", i), func(t *testing.T) {
399400
t.Parallel()
400-
id := fmt.Sprintf("%d", i)
401+
id := strconv.Itoa(i)
401402
if i%2 == 0 {
402403
t.Logf("update resource %q", id)
403404
require.NoError(t, c.UpdateResource(id, testResource(id)))
404405
} else {
405-
id2 := fmt.Sprintf("%d", i-1)
406+
id2 := strconv.Itoa(i - 1)
406407
t.Logf("request resources %q and %q", id, id2)
407408
value := make(chan Response, 1)
408409
c.CreateWatch(&Request{

pkg/cache/v3/snapshot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (s *Snapshot) GetVersionMap(typeURL string) map[string]string {
168168
// ConstructVersionMap will construct a version map based on the current state of a snapshot
169169
func (s *Snapshot) ConstructVersionMap() error {
170170
if s == nil {
171-
return fmt.Errorf("missing snapshot")
171+
return errors.New("missing snapshot")
172172
}
173173

174174
// The snapshot resources never change, so no need to ever rebuild.

pkg/server/v3/delta_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strconv"
78
"testing"
89
"time"
910

@@ -115,7 +116,7 @@ func (stream *mockDeltaStream) Context() context.Context {
115116
func (stream *mockDeltaStream) Send(resp *discovery.DeltaDiscoveryResponse) error {
116117
// Check that nonce is incremented by one
117118
stream.nonce++
118-
if resp.GetNonce() != fmt.Sprintf("%d", stream.nonce) {
119+
if resp.GetNonce() != strconv.Itoa(stream.nonce) {
119120
stream.t.Errorf("Nonce => got %q, want %d", resp.GetNonce(), stream.nonce)
120121
}
121122
// Check that resources are non-empty
@@ -434,7 +435,7 @@ func TestDeltaOpaqueRequestsChannelMuxing(t *testing.T) {
434435
resp.recv <- &discovery.DeltaDiscoveryRequest{
435436
Node: node,
436437
TypeUrl: fmt.Sprintf("%s%d", opaqueType, i%2),
437-
ResourceNamesSubscribe: []string{fmt.Sprintf("%d", i)},
438+
ResourceNamesSubscribe: []string{strconv.Itoa(i)},
438439
}
439440
}
440441
close(resp.recv)
@@ -651,7 +652,7 @@ func TestDeltaMultipleStreams(t *testing.T) {
651652
config,
652653
server.CallbackFuncs{
653654
StreamDeltaRequestFunc: func(int64, *discovery.DeltaDiscoveryRequest) error {
654-
return fmt.Errorf("error")
655+
return errors.New("error")
655656
},
656657
},
657658
)

pkg/server/v3/gateway.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ func (h *HTTPGateway) ServeHTTP(req *http.Request) ([]byte, int, error) {
5757
case resource.FetchExtensionConfigs:
5858
typeURL = resource.ExtensionConfigType
5959
default:
60-
return nil, http.StatusNotFound, fmt.Errorf("no endpoint")
60+
return nil, http.StatusNotFound, errors.New("no endpoint")
6161
}
6262

6363
if req.Body == nil {
64-
return nil, http.StatusBadRequest, fmt.Errorf("empty body")
64+
return nil, http.StatusBadRequest, errors.New("empty body")
6565
}
6666

6767
body, err := io.ReadAll(req.Body)
6868
if err != nil {
69-
return nil, http.StatusBadRequest, fmt.Errorf("cannot read body")
69+
return nil, http.StatusBadRequest, errors.New("cannot read body")
7070
}
7171

7272
// parse as JSON

pkg/server/v3/server_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"reflect"
22+
"strconv"
2223
"strings"
2324
"sync"
2425
"testing"
@@ -104,7 +105,7 @@ func (stream *mockStream) Context() context.Context {
104105
func (stream *mockStream) Send(resp *discovery.DiscoveryResponse) error {
105106
// check that nonce is monotonically incrementing
106107
stream.nonce++
107-
assert.Equal(stream.t, resp.GetNonce(), fmt.Sprintf("%d", stream.nonce))
108+
assert.Equal(stream.t, resp.GetNonce(), strconv.Itoa(stream.nonce))
108109
// check that version is set
109110
assert.NotEmpty(stream.t, resp.GetVersionInfo())
110111
// check resources are non-empty
@@ -653,7 +654,7 @@ func TestOpaqueRequestsChannelMuxing(t *testing.T) {
653654
Node: node,
654655
TypeUrl: fmt.Sprintf("%s%d", opaqueType, i%2),
655656
// each subsequent request is assumed to supercede the previous request
656-
ResourceNames: []string{fmt.Sprintf("%d", i)},
657+
ResourceNames: []string{strconv.Itoa(i)},
657658
}
658659
}
659660
close(resp.recv)
@@ -671,7 +672,7 @@ func TestNilPropagationOverResponseChannelShouldCloseTheStream(t *testing.T) {
671672
Node: node,
672673
TypeUrl: nilType,
673674
// each subsequent request is assumed to supercede the previous request
674-
ResourceNames: []string{fmt.Sprintf("%d", i)},
675+
ResourceNames: []string{strconv.Itoa(i)},
675676
}
676677
}
677678
close(resp.recv)

0 commit comments

Comments
 (0)