Skip to content

Commit 8c1d729

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

File tree

13 files changed

+67
-55
lines changed

13 files changed

+67
-55
lines changed

Diff for: .golangci.yml

+12
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

Diff for: api/prometheus/v1/api_test.go

+34-34
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ package v1
1616
import (
1717
"context"
1818
"errors"
19-
"fmt"
2019
"io"
2120
"math"
2221
"net/http"
2322
"net/http/httptest"
2423
"net/url"
2524
"reflect"
25+
"strconv"
2626
"strings"
2727
"testing"
2828
"time"
@@ -260,7 +260,7 @@ func TestAPIs(t *testing.T) {
260260
},
261261
{
262262
do: doQuery("2", testTime),
263-
inErr: fmt.Errorf("some error"),
263+
inErr: errors.New("some error"),
264264

265265
reqMethod: "POST",
266266
reqPath: "/api/v1/query",
@@ -336,7 +336,7 @@ func TestAPIs(t *testing.T) {
336336
End: testTime,
337337
Step: 1 * time.Minute,
338338
}, WithTimeout(5*time.Second)),
339-
inErr: fmt.Errorf("some error"),
339+
inErr: errors.New("some error"),
340340

341341
reqMethod: "POST",
342342
reqPath: "/api/v1/query_range",
@@ -361,14 +361,14 @@ func TestAPIs(t *testing.T) {
361361

362362
{
363363
do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
364-
inErr: fmt.Errorf("some error"),
364+
inErr: errors.New("some error"),
365365
reqMethod: "POST",
366366
reqPath: "/api/v1/labels",
367367
err: errors.New("some error"),
368368
},
369369
{
370370
do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
371-
inErr: fmt.Errorf("some error"),
371+
inErr: errors.New("some error"),
372372
inWarnings: []string{"a"},
373373
reqMethod: "POST",
374374
reqPath: "/api/v1/labels",
@@ -400,14 +400,14 @@ func TestAPIs(t *testing.T) {
400400

401401
{
402402
do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
403-
inErr: fmt.Errorf("some error"),
403+
inErr: errors.New("some error"),
404404
reqMethod: "GET",
405405
reqPath: "/api/v1/label/mylabel/values",
406406
err: errors.New("some error"),
407407
},
408408
{
409409
do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
410-
inErr: fmt.Errorf("some error"),
410+
inErr: errors.New("some error"),
411411
inWarnings: []string{"a"},
412412
reqMethod: "GET",
413413
reqPath: "/api/v1/label/mylabel/values",
@@ -464,15 +464,15 @@ func TestAPIs(t *testing.T) {
464464

465465
{
466466
do: doSeries("up", testTime.Add(-time.Minute), testTime),
467-
inErr: fmt.Errorf("some error"),
467+
inErr: errors.New("some error"),
468468
reqMethod: "POST",
469469
reqPath: "/api/v1/series",
470470
err: errors.New("some error"),
471471
},
472472
// Series with error and warning.
473473
{
474474
do: doSeries("up", testTime.Add(-time.Minute), testTime),
475-
inErr: fmt.Errorf("some error"),
475+
inErr: errors.New("some error"),
476476
inWarnings: []string{"a"},
477477
reqMethod: "POST",
478478
reqPath: "/api/v1/series",
@@ -493,7 +493,7 @@ func TestAPIs(t *testing.T) {
493493

494494
{
495495
do: doSnapshot(true),
496-
inErr: fmt.Errorf("some error"),
496+
inErr: errors.New("some error"),
497497
reqMethod: "POST",
498498
reqPath: "/api/v1/admin/tsdb/snapshot",
499499
err: errors.New("some error"),
@@ -507,7 +507,7 @@ func TestAPIs(t *testing.T) {
507507

508508
{
509509
do: doCleanTombstones(),
510-
inErr: fmt.Errorf("some error"),
510+
inErr: errors.New("some error"),
511511
reqMethod: "POST",
512512
reqPath: "/api/v1/admin/tsdb/clean_tombstones",
513513
err: errors.New("some error"),
@@ -528,7 +528,7 @@ func TestAPIs(t *testing.T) {
528528

529529
{
530530
do: doDeleteSeries("up", testTime.Add(-time.Minute), testTime),
531-
inErr: fmt.Errorf("some error"),
531+
inErr: errors.New("some error"),
532532
reqMethod: "POST",
533533
reqPath: "/api/v1/admin/tsdb/delete_series",
534534
err: errors.New("some error"),
@@ -550,8 +550,8 @@ func TestAPIs(t *testing.T) {
550550
do: doConfig(),
551551
reqMethod: "GET",
552552
reqPath: "/api/v1/status/config",
553-
inErr: fmt.Errorf("some error"),
554-
err: fmt.Errorf("some error"),
553+
inErr: errors.New("some error"),
554+
err: errors.New("some error"),
555555
},
556556

557557
{
@@ -578,16 +578,16 @@ func TestAPIs(t *testing.T) {
578578
do: doFlags(),
579579
reqMethod: "GET",
580580
reqPath: "/api/v1/status/flags",
581-
inErr: fmt.Errorf("some error"),
582-
err: fmt.Errorf("some error"),
581+
inErr: errors.New("some error"),
582+
err: errors.New("some error"),
583583
},
584584

585585
{
586586
do: doBuildinfo(),
587587
reqMethod: "GET",
588588
reqPath: "/api/v1/status/buildinfo",
589-
inErr: fmt.Errorf("some error"),
590-
err: fmt.Errorf("some error"),
589+
inErr: errors.New("some error"),
590+
err: errors.New("some error"),
591591
},
592592

593593
{
@@ -616,8 +616,8 @@ func TestAPIs(t *testing.T) {
616616
do: doRuntimeinfo(),
617617
reqMethod: "GET",
618618
reqPath: "/api/v1/status/runtimeinfo",
619-
inErr: fmt.Errorf("some error"),
620-
err: fmt.Errorf("some error"),
619+
inErr: errors.New("some error"),
620+
err: errors.New("some error"),
621621
},
622622

623623
{
@@ -684,8 +684,8 @@ func TestAPIs(t *testing.T) {
684684
do: doAlertManagers(),
685685
reqMethod: "GET",
686686
reqPath: "/api/v1/alertmanagers",
687-
inErr: fmt.Errorf("some error"),
688-
err: fmt.Errorf("some error"),
687+
inErr: errors.New("some error"),
688+
err: errors.New("some error"),
689689
},
690690

691691
{
@@ -891,8 +891,8 @@ func TestAPIs(t *testing.T) {
891891
do: doRules(),
892892
reqMethod: "GET",
893893
reqPath: "/api/v1/rules",
894-
inErr: fmt.Errorf("some error"),
895-
err: fmt.Errorf("some error"),
894+
inErr: errors.New("some error"),
895+
err: errors.New("some error"),
896896
},
897897

898898
{
@@ -971,8 +971,8 @@ func TestAPIs(t *testing.T) {
971971
do: doTargets(),
972972
reqMethod: "GET",
973973
reqPath: "/api/v1/targets",
974-
inErr: fmt.Errorf("some error"),
975-
err: fmt.Errorf("some error"),
974+
inErr: errors.New("some error"),
975+
err: errors.New("some error"),
976976
},
977977

978978
{
@@ -1005,7 +1005,7 @@ func TestAPIs(t *testing.T) {
10051005

10061006
{
10071007
do: doTargetsMetadata("{job=\"prometheus\"}", "go_goroutines", "1"),
1008-
inErr: fmt.Errorf("some error"),
1008+
inErr: errors.New("some error"),
10091009
reqMethod: "GET",
10101010
reqPath: "/api/v1/targets/metadata",
10111011
err: errors.New("some error"),
@@ -1037,7 +1037,7 @@ func TestAPIs(t *testing.T) {
10371037

10381038
{
10391039
do: doMetadata("", "1"),
1040-
inErr: fmt.Errorf("some error"),
1040+
inErr: errors.New("some error"),
10411041
reqMethod: "GET",
10421042
reqPath: "/api/v1/metadata",
10431043
err: errors.New("some error"),
@@ -1047,8 +1047,8 @@ func TestAPIs(t *testing.T) {
10471047
do: doTSDB(),
10481048
reqMethod: "GET",
10491049
reqPath: "/api/v1/status/tsdb",
1050-
inErr: fmt.Errorf("some error"),
1051-
err: fmt.Errorf("some error"),
1050+
inErr: errors.New("some error"),
1051+
err: errors.New("some error"),
10521052
},
10531053

10541054
{
@@ -1127,8 +1127,8 @@ func TestAPIs(t *testing.T) {
11271127
do: doWalReply(),
11281128
reqMethod: "GET",
11291129
reqPath: "/api/v1/status/walreplay",
1130-
inErr: fmt.Errorf("some error"),
1131-
err: fmt.Errorf("some error"),
1130+
inErr: errors.New("some error"),
1131+
err: errors.New("some error"),
11321132
},
11331133

11341134
{
@@ -1212,7 +1212,7 @@ func TestAPIs(t *testing.T) {
12121212
tests = append(tests, queryTests...)
12131213

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

12181218
res, warnings, err := test.do()
@@ -1430,7 +1430,7 @@ func TestAPIClientDo(t *testing.T) {
14301430
}
14311431

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

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

Diff for: examples/exemplars/main.go

+2-2
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
}

Diff for: examples/random/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package main
1818

1919
import (
2020
"flag"
21-
"fmt"
2221
"log"
2322
"math"
2423
"math/rand"
2524
"net/http"
25+
"strconv"
2626
"time"
2727

2828
"github.com/prometheus/client_golang/prometheus"
@@ -116,7 +116,7 @@ func main() {
116116
// the ExemplarObserver interface and thus don't need to
117117
// check the outcome of the type assertion.
118118
m.rpcDurationsHistogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
119-
v, prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},
119+
v, prometheus.Labels{"dummyID": strconv.Itoa(rand.Intn(100000))},
120120
)
121121
time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
122122
}

Diff for: prometheus/counter_test.go

+2-3
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)

Diff for: prometheus/internal/difflib.go

+2-1
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

Diff for: prometheus/process_collector_cgo_darwin.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import "C"
2222
import "fmt"
2323

2424
func getMemory() (*memoryInfo, error) {
25-
var (
26-
rss, vsize C.ulonglong
27-
)
25+
var rss, vsize C.ulonglong
2826

2927
if err := C.get_memory_info(&rss, &vsize); err != 0 {
3028
return nil, fmt.Errorf("task_info() failed with 0x%x", int(err))

Diff for: prometheus/promhttp/http_test.go

+1-1
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) {

Diff for: prometheus/registry_test.go

+2-1
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)