Skip to content

Commit d673fda

Browse files
committed
[chore]: enable perfsprint linter
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 02883cb commit d673fda

File tree

12 files changed

+68
-52
lines changed

12 files changed

+68
-52
lines changed

.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ linters:
2525
- ineffassign
2626
- misspell
2727
- nolintlint
28+
- perfsprint
2829
- predeclared
2930
- revive
3031
- staticcheck
@@ -66,6 +67,17 @@ linters-settings:
6667
local-prefixes: github.com/prometheus/client_golang
6768
gofumpt:
6869
extra-rules: true
70+
perfsprint:
71+
# Optimizes even if it requires an int or uint type cast.
72+
int-conversion: true
73+
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
74+
err-error: true
75+
# Optimizes `fmt.Errorf`.
76+
errorf: true
77+
# Optimizes `fmt.Sprintf` with only one argument.
78+
sprintf1: true
79+
# Optimizes into strings concatenation.
80+
strconcat: true
6981
revive:
7082
rules:
7183
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter

api/prometheus/v1/api_test.go

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ package v1
1616
import (
1717
"context"
1818
"errors"
19-
"fmt"
19+
2020
"io"
2121
"math"
2222
"net/http"
2323
"net/http/httptest"
2424
"net/url"
2525
"reflect"
26+
"strconv"
2627
"strings"
2728
"testing"
2829
"time"
@@ -260,7 +261,7 @@ func TestAPIs(t *testing.T) {
260261
},
261262
{
262263
do: doQuery("2", testTime),
263-
inErr: fmt.Errorf("some error"),
264+
inErr: errors.New("some error"),
264265

265266
reqMethod: "POST",
266267
reqPath: "/api/v1/query",
@@ -336,7 +337,7 @@ func TestAPIs(t *testing.T) {
336337
End: testTime,
337338
Step: 1 * time.Minute,
338339
}, WithTimeout(5*time.Second)),
339-
inErr: fmt.Errorf("some error"),
340+
inErr: errors.New("some error"),
340341

341342
reqMethod: "POST",
342343
reqPath: "/api/v1/query_range",
@@ -361,14 +362,14 @@ func TestAPIs(t *testing.T) {
361362

362363
{
363364
do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
364-
inErr: fmt.Errorf("some error"),
365+
inErr: errors.New("some error"),
365366
reqMethod: "POST",
366367
reqPath: "/api/v1/labels",
367368
err: errors.New("some error"),
368369
},
369370
{
370371
do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
371-
inErr: fmt.Errorf("some error"),
372+
inErr: errors.New("some error"),
372373
inWarnings: []string{"a"},
373374
reqMethod: "POST",
374375
reqPath: "/api/v1/labels",
@@ -400,14 +401,14 @@ func TestAPIs(t *testing.T) {
400401

401402
{
402403
do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
403-
inErr: fmt.Errorf("some error"),
404+
inErr: errors.New("some error"),
404405
reqMethod: "GET",
405406
reqPath: "/api/v1/label/mylabel/values",
406407
err: errors.New("some error"),
407408
},
408409
{
409410
do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
410-
inErr: fmt.Errorf("some error"),
411+
inErr: errors.New("some error"),
411412
inWarnings: []string{"a"},
412413
reqMethod: "GET",
413414
reqPath: "/api/v1/label/mylabel/values",
@@ -464,15 +465,15 @@ func TestAPIs(t *testing.T) {
464465

465466
{
466467
do: doSeries("up", testTime.Add(-time.Minute), testTime),
467-
inErr: fmt.Errorf("some error"),
468+
inErr: errors.New("some error"),
468469
reqMethod: "POST",
469470
reqPath: "/api/v1/series",
470471
err: errors.New("some error"),
471472
},
472473
// Series with error and warning.
473474
{
474475
do: doSeries("up", testTime.Add(-time.Minute), testTime),
475-
inErr: fmt.Errorf("some error"),
476+
inErr: errors.New("some error"),
476477
inWarnings: []string{"a"},
477478
reqMethod: "POST",
478479
reqPath: "/api/v1/series",
@@ -493,7 +494,7 @@ func TestAPIs(t *testing.T) {
493494

494495
{
495496
do: doSnapshot(true),
496-
inErr: fmt.Errorf("some error"),
497+
inErr: errors.New("some error"),
497498
reqMethod: "POST",
498499
reqPath: "/api/v1/admin/tsdb/snapshot",
499500
err: errors.New("some error"),
@@ -507,7 +508,7 @@ func TestAPIs(t *testing.T) {
507508

508509
{
509510
do: doCleanTombstones(),
510-
inErr: fmt.Errorf("some error"),
511+
inErr: errors.New("some error"),
511512
reqMethod: "POST",
512513
reqPath: "/api/v1/admin/tsdb/clean_tombstones",
513514
err: errors.New("some error"),
@@ -528,7 +529,7 @@ func TestAPIs(t *testing.T) {
528529

529530
{
530531
do: doDeleteSeries("up", testTime.Add(-time.Minute), testTime),
531-
inErr: fmt.Errorf("some error"),
532+
inErr: errors.New("some error"),
532533
reqMethod: "POST",
533534
reqPath: "/api/v1/admin/tsdb/delete_series",
534535
err: errors.New("some error"),
@@ -550,8 +551,8 @@ func TestAPIs(t *testing.T) {
550551
do: doConfig(),
551552
reqMethod: "GET",
552553
reqPath: "/api/v1/status/config",
553-
inErr: fmt.Errorf("some error"),
554-
err: fmt.Errorf("some error"),
554+
inErr: errors.New("some error"),
555+
err: errors.New("some error"),
555556
},
556557

557558
{
@@ -578,16 +579,16 @@ func TestAPIs(t *testing.T) {
578579
do: doFlags(),
579580
reqMethod: "GET",
580581
reqPath: "/api/v1/status/flags",
581-
inErr: fmt.Errorf("some error"),
582-
err: fmt.Errorf("some error"),
582+
inErr: errors.New("some error"),
583+
err: errors.New("some error"),
583584
},
584585

585586
{
586587
do: doBuildinfo(),
587588
reqMethod: "GET",
588589
reqPath: "/api/v1/status/buildinfo",
589-
inErr: fmt.Errorf("some error"),
590-
err: fmt.Errorf("some error"),
590+
inErr: errors.New("some error"),
591+
err: errors.New("some error"),
591592
},
592593

593594
{
@@ -616,8 +617,8 @@ func TestAPIs(t *testing.T) {
616617
do: doRuntimeinfo(),
617618
reqMethod: "GET",
618619
reqPath: "/api/v1/status/runtimeinfo",
619-
inErr: fmt.Errorf("some error"),
620-
err: fmt.Errorf("some error"),
620+
inErr: errors.New("some error"),
621+
err: errors.New("some error"),
621622
},
622623

623624
{
@@ -684,8 +685,8 @@ func TestAPIs(t *testing.T) {
684685
do: doAlertManagers(),
685686
reqMethod: "GET",
686687
reqPath: "/api/v1/alertmanagers",
687-
inErr: fmt.Errorf("some error"),
688-
err: fmt.Errorf("some error"),
688+
inErr: errors.New("some error"),
689+
err: errors.New("some error"),
689690
},
690691

691692
{
@@ -891,8 +892,8 @@ func TestAPIs(t *testing.T) {
891892
do: doRules(),
892893
reqMethod: "GET",
893894
reqPath: "/api/v1/rules",
894-
inErr: fmt.Errorf("some error"),
895-
err: fmt.Errorf("some error"),
895+
inErr: errors.New("some error"),
896+
err: errors.New("some error"),
896897
},
897898

898899
{
@@ -971,8 +972,8 @@ func TestAPIs(t *testing.T) {
971972
do: doTargets(),
972973
reqMethod: "GET",
973974
reqPath: "/api/v1/targets",
974-
inErr: fmt.Errorf("some error"),
975-
err: fmt.Errorf("some error"),
975+
inErr: errors.New("some error"),
976+
err: errors.New("some error"),
976977
},
977978

978979
{
@@ -1005,7 +1006,7 @@ func TestAPIs(t *testing.T) {
10051006

10061007
{
10071008
do: doTargetsMetadata("{job=\"prometheus\"}", "go_goroutines", "1"),
1008-
inErr: fmt.Errorf("some error"),
1009+
inErr: errors.New("some error"),
10091010
reqMethod: "GET",
10101011
reqPath: "/api/v1/targets/metadata",
10111012
err: errors.New("some error"),
@@ -1037,7 +1038,7 @@ func TestAPIs(t *testing.T) {
10371038

10381039
{
10391040
do: doMetadata("", "1"),
1040-
inErr: fmt.Errorf("some error"),
1041+
inErr: errors.New("some error"),
10411042
reqMethod: "GET",
10421043
reqPath: "/api/v1/metadata",
10431044
err: errors.New("some error"),
@@ -1047,8 +1048,8 @@ func TestAPIs(t *testing.T) {
10471048
do: doTSDB(),
10481049
reqMethod: "GET",
10491050
reqPath: "/api/v1/status/tsdb",
1050-
inErr: fmt.Errorf("some error"),
1051-
err: fmt.Errorf("some error"),
1051+
inErr: errors.New("some error"),
1052+
err: errors.New("some error"),
10521053
},
10531054

10541055
{
@@ -1127,8 +1128,8 @@ func TestAPIs(t *testing.T) {
11271128
do: doWalReply(),
11281129
reqMethod: "GET",
11291130
reqPath: "/api/v1/status/walreplay",
1130-
inErr: fmt.Errorf("some error"),
1131-
err: fmt.Errorf("some error"),
1131+
inErr: errors.New("some error"),
1132+
err: errors.New("some error"),
11321133
},
11331134

11341135
{
@@ -1212,7 +1213,7 @@ func TestAPIs(t *testing.T) {
12121213
tests = append(tests, queryTests...)
12131214

12141215
for i, test := range tests {
1215-
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
1216+
t.Run(strconv.Itoa(i), func(t *testing.T) {
12161217
tc.curTest = test
12171218

12181219
res, warnings, err := test.do()
@@ -1430,7 +1431,7 @@ func TestAPIClientDo(t *testing.T) {
14301431
}
14311432

14321433
for i, test := range tests {
1433-
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
1434+
t.Run(strconv.Itoa(i), func(t *testing.T) {
14341435
tc.ch <- test
14351436

14361437
_, body, warnings, err := client.Do(context.Background(), tc.req)

examples/exemplars/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package main
1818

1919
import (
20-
"fmt"
2120
"log"
2221
"math/rand"
2322
"net/http"
23+
"strconv"
2424
"time"
2525

2626
"github.com/prometheus/client_golang/prometheus"
@@ -50,7 +50,7 @@ func main() {
5050
// Record fictional latency.
5151
now := time.Now()
5252
requestDurations.(prometheus.ExemplarObserver).ObserveWithExemplar(
53-
time.Since(now).Seconds(), prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},
53+
time.Since(now).Seconds(), prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
5454
)
5555
time.Sleep(600 * time.Millisecond)
5656
}

examples/random/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package main
1818

1919
import (
2020
"flag"
21-
"fmt"
21+
2222
"log"
2323
"math"
2424
"math/rand"
2525
"net/http"
26+
"strconv"
2627
"time"
2728

2829
"github.com/prometheus/client_golang/prometheus"
@@ -116,7 +117,7 @@ func main() {
116117
// the ExemplarObserver interface and thus don't need to
117118
// check the outcome of the type assertion.
118119
m.rpcDurationsHistogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
119-
v, prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},
120+
v, prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
120121
)
121122
time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
122123
}

prometheus/counter_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package prometheus
1515

1616
import (
17-
"fmt"
1817
"math"
1918
"strings"
2019
"testing"
@@ -120,10 +119,10 @@ func TestCounterVecGetMetricWithInvalidLabelValues(t *testing.T) {
120119

121120
expectPanic(t, func() {
122121
counterVec.WithLabelValues(labelValues...)
123-
}, fmt.Sprintf("WithLabelValues: expected panic because: %s", test.desc))
122+
}, "WithLabelValues: expected panic because: "+test.desc)
124123
expectPanic(t, func() {
125124
counterVec.With(test.labels)
126-
}, fmt.Sprintf("WithLabelValues: expected panic because: %s", test.desc))
125+
}, "WithLabelValues: expected panic because: "+test.desc)
127126

128127
if _, err := counterVec.GetMetricWithLabelValues(labelValues...); err == nil {
129128
t.Errorf("GetMetricWithLabelValues: expected error because: %s", test.desc)

prometheus/internal/difflib.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"bytes"
2323
"fmt"
2424
"io"
25+
"strconv"
2526
"strings"
2627
)
2728

@@ -524,7 +525,7 @@ func formatRangeUnified(start, stop int) string {
524525
beginning := start + 1 // lines start numbering with one
525526
length := stop - start
526527
if length == 1 {
527-
return fmt.Sprintf("%d", beginning)
528+
return strconv.Itoa(beginning)
528529
}
529530
if length == 0 {
530531
beginning-- // empty ranges begin at line just before the range

prometheus/promhttp/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func readCompressedBody(r io.Reader, comp Compression) (string, error) {
9898
got, err := io.ReadAll(reader)
9999
return string(got), err
100100
}
101-
return "", fmt.Errorf("Unsupported compression")
101+
return "", errors.New("Unsupported compression")
102102
}
103103

104104
func TestHandlerErrorHandling(t *testing.T) {

prometheus/registry_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"net/http"
2828
"net/http/httptest"
2929
"os"
30+
"strconv"
3031
"sync"
3132
"testing"
3233
"time"
@@ -1282,7 +1283,7 @@ func ExampleRegistry_grouping() {
12821283
ConstLabels: prometheus.Labels{
12831284
// Generate a label unique to this worker so its metric doesn't
12841285
// collide with the metrics from other workers.
1285-
"worker_id": fmt.Sprintf("%d", workerID),
1286+
"worker_id": strconv.Itoa(workerID),
12861287
},
12871288
})
12881289
workerReg.MustRegister(workTime)

0 commit comments

Comments
 (0)