Skip to content

Commit 937c5b5

Browse files
authored
chore: enable perfsprint linter (#717)
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 28e4459 commit 937c5b5

15 files changed

+79
-60
lines changed

.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,25 @@ linters:
1111
- govet
1212
- ineffassign
1313
- misspell
14+
- perfsprint
1415
- revive
1516
- staticcheck
1617
- testifylint
1718
- unused
1819
linters-settings:
1920
goimports:
2021
local-prefixes: github.com/prometheus/common
22+
perfsprint:
23+
# Optimizes even if it requires an int or uint type cast.
24+
int-conversion: true
25+
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
26+
err-error: true
27+
# Optimizes `fmt.Errorf`.
28+
errorf: true
29+
# Optimizes `fmt.Sprintf` with only one argument.
30+
sprintf1: true
31+
# Optimizes into strings concatenation.
32+
strconcat: false
2133
revive:
2234
rules:
2335
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter

config/http_config.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -357,33 +357,33 @@ func nonZeroCount[T comparable](values ...T) int {
357357
func (c *HTTPClientConfig) Validate() error {
358358
// Backwards compatibility with the bearer_token field.
359359
if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
360-
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured")
360+
return errors.New("at most one of bearer_token & bearer_token_file must be configured")
361361
}
362362
if (c.BasicAuth != nil || c.OAuth2 != nil) && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) {
363-
return fmt.Errorf("at most one of basic_auth, oauth2, bearer_token & bearer_token_file must be configured")
363+
return errors.New("at most one of basic_auth, oauth2, bearer_token & bearer_token_file must be configured")
364364
}
365365
if c.BasicAuth != nil && nonZeroCount(string(c.BasicAuth.Username) != "", c.BasicAuth.UsernameFile != "", c.BasicAuth.UsernameRef != "") > 1 {
366-
return fmt.Errorf("at most one of basic_auth username, username_file & username_ref must be configured")
366+
return errors.New("at most one of basic_auth username, username_file & username_ref must be configured")
367367
}
368368
if c.BasicAuth != nil && nonZeroCount(string(c.BasicAuth.Password) != "", c.BasicAuth.PasswordFile != "", c.BasicAuth.PasswordRef != "") > 1 {
369-
return fmt.Errorf("at most one of basic_auth password, password_file & password_ref must be configured")
369+
return errors.New("at most one of basic_auth password, password_file & password_ref must be configured")
370370
}
371371
if c.Authorization != nil {
372372
if len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0 {
373-
return fmt.Errorf("authorization is not compatible with bearer_token & bearer_token_file")
373+
return errors.New("authorization is not compatible with bearer_token & bearer_token_file")
374374
}
375375
if nonZeroCount(string(c.Authorization.Credentials) != "", c.Authorization.CredentialsFile != "", c.Authorization.CredentialsRef != "") > 1 {
376-
return fmt.Errorf("at most one of authorization credentials & credentials_file must be configured")
376+
return errors.New("at most one of authorization credentials & credentials_file must be configured")
377377
}
378378
c.Authorization.Type = strings.TrimSpace(c.Authorization.Type)
379379
if len(c.Authorization.Type) == 0 {
380380
c.Authorization.Type = "Bearer"
381381
}
382382
if strings.ToLower(c.Authorization.Type) == "basic" {
383-
return fmt.Errorf(`authorization type cannot be set to "basic", use "basic_auth" instead`)
383+
return errors.New(`authorization type cannot be set to "basic", use "basic_auth" instead`)
384384
}
385385
if c.BasicAuth != nil || c.OAuth2 != nil {
386-
return fmt.Errorf("at most one of basic_auth, oauth2 & authorization must be configured")
386+
return errors.New("at most one of basic_auth, oauth2 & authorization must be configured")
387387
}
388388
} else {
389389
if len(c.BearerToken) > 0 {
@@ -399,16 +399,16 @@ func (c *HTTPClientConfig) Validate() error {
399399
}
400400
if c.OAuth2 != nil {
401401
if c.BasicAuth != nil {
402-
return fmt.Errorf("at most one of basic_auth, oauth2 & authorization must be configured")
402+
return errors.New("at most one of basic_auth, oauth2 & authorization must be configured")
403403
}
404404
if len(c.OAuth2.ClientID) == 0 {
405-
return fmt.Errorf("oauth2 client_id must be configured")
405+
return errors.New("oauth2 client_id must be configured")
406406
}
407407
if len(c.OAuth2.TokenURL) == 0 {
408-
return fmt.Errorf("oauth2 token_url must be configured")
408+
return errors.New("oauth2 token_url must be configured")
409409
}
410410
if nonZeroCount(len(c.OAuth2.ClientSecret) > 0, len(c.OAuth2.ClientSecretFile) > 0, len(c.OAuth2.ClientSecretRef) > 0) > 1 {
411-
return fmt.Errorf("at most one of oauth2 client_secret, client_secret_file & client_secret_ref must be configured")
411+
return errors.New("at most one of oauth2 client_secret, client_secret_file & client_secret_ref must be configured")
412412
}
413413
}
414414
if err := c.ProxyConfig.Validate(); err != nil {
@@ -735,7 +735,7 @@ func (s *FileSecret) Fetch(ctx context.Context) (string, error) {
735735
}
736736

737737
func (s *FileSecret) Description() string {
738-
return fmt.Sprintf("file %s", s.file)
738+
return "file " + s.file
739739
}
740740

741741
func (s *FileSecret) Immutable() bool {
@@ -753,7 +753,7 @@ func (s *refSecret) Fetch(ctx context.Context) (string, error) {
753753
}
754754

755755
func (s *refSecret) Description() string {
756-
return fmt.Sprintf("ref %s", s.ref)
756+
return "ref " + s.ref
757757
}
758758

759759
func (s *refSecret) Immutable() bool {
@@ -1045,7 +1045,7 @@ func NewTLSConfigWithContext(ctx context.Context, cfg *TLSConfig, optFuncs ...TL
10451045

10461046
if cfg.MaxVersion != 0 && cfg.MinVersion != 0 {
10471047
if cfg.MaxVersion < cfg.MinVersion {
1048-
return nil, fmt.Errorf("tls_config.max_version must be greater than or equal to tls_config.min_version if both are specified")
1048+
return nil, errors.New("tls_config.max_version must be greater than or equal to tls_config.min_version if both are specified")
10491049
}
10501050
}
10511051

@@ -1144,19 +1144,19 @@ func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
11441144
// used.
11451145
func (c *TLSConfig) Validate() error {
11461146
if nonZeroCount(len(c.CA) > 0, len(c.CAFile) > 0, len(c.CARef) > 0) > 1 {
1147-
return fmt.Errorf("at most one of ca, ca_file & ca_ref must be configured")
1147+
return errors.New("at most one of ca, ca_file & ca_ref must be configured")
11481148
}
11491149
if nonZeroCount(len(c.Cert) > 0, len(c.CertFile) > 0, len(c.CertRef) > 0) > 1 {
1150-
return fmt.Errorf("at most one of cert, cert_file & cert_ref must be configured")
1150+
return errors.New("at most one of cert, cert_file & cert_ref must be configured")
11511151
}
11521152
if nonZeroCount(len(c.Key) > 0, len(c.KeyFile) > 0, len(c.KeyRef) > 0) > 1 {
1153-
return fmt.Errorf("at most one of key and key_file must be configured")
1153+
return errors.New("at most one of key and key_file must be configured")
11541154
}
11551155

11561156
if c.usingClientCert() && !c.usingClientKey() {
1157-
return fmt.Errorf("exactly one of key or key_file must be configured when a client certificate is configured")
1157+
return errors.New("exactly one of key or key_file must be configured when a client certificate is configured")
11581158
} else if c.usingClientKey() && !c.usingClientCert() {
1159-
return fmt.Errorf("exactly one of cert or cert_file must be configured when a client key is configured")
1159+
return errors.New("exactly one of cert or cert_file must be configured when a client key is configured")
11601160
}
11611161

11621162
return nil
@@ -1460,16 +1460,16 @@ type ProxyConfig struct {
14601460
// UnmarshalYAML implements the yaml.Unmarshaler interface.
14611461
func (c *ProxyConfig) Validate() error {
14621462
if len(c.ProxyConnectHeader) > 0 && (!c.ProxyFromEnvironment && (c.ProxyURL.URL == nil || c.ProxyURL.String() == "")) {
1463-
return fmt.Errorf("if proxy_connect_header is configured, proxy_url or proxy_from_environment must also be configured")
1463+
return errors.New("if proxy_connect_header is configured, proxy_url or proxy_from_environment must also be configured")
14641464
}
14651465
if c.ProxyFromEnvironment && c.ProxyURL.URL != nil && c.ProxyURL.String() != "" {
1466-
return fmt.Errorf("if proxy_from_environment is configured, proxy_url must not be configured")
1466+
return errors.New("if proxy_from_environment is configured, proxy_url must not be configured")
14671467
}
14681468
if c.ProxyFromEnvironment && c.NoProxy != "" {
1469-
return fmt.Errorf("if proxy_from_environment is configured, no_proxy must not be configured")
1469+
return errors.New("if proxy_from_environment is configured, no_proxy must not be configured")
14701470
}
14711471
if c.ProxyURL.URL == nil && c.NoProxy != "" {
1472-
return fmt.Errorf("if no_proxy is configured, proxy_url must also be configured")
1472+
return errors.New("if no_proxy is configured, proxy_url must also be configured")
14731473
}
14741474
return nil
14751475
}

config/http_config_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ func TestNewClientFromInvalidConfig(t *testing.T) {
609609
InsecureSkipVerify: true,
610610
},
611611
},
612-
errorMsg: fmt.Sprintf("unable to read CA cert: unable to read file %s", MissingCA),
612+
errorMsg: "unable to read CA cert: unable to read file " + MissingCA,
613613
},
614614
{
615615
clientConfig: HTTPClientConfig{
@@ -618,7 +618,7 @@ func TestNewClientFromInvalidConfig(t *testing.T) {
618618
InsecureSkipVerify: true,
619619
},
620620
},
621-
errorMsg: fmt.Sprintf("unable to use specified CA cert file %s", InvalidCA),
621+
errorMsg: "unable to use specified CA cert file " + InvalidCA,
622622
},
623623
}
624624

@@ -864,7 +864,7 @@ func TestTLSConfigInvalidCA(t *testing.T) {
864864
ServerName: "",
865865
InsecureSkipVerify: false,
866866
},
867-
errorMessage: fmt.Sprintf("unable to read CA cert: unable to read file %s", MissingCA),
867+
errorMessage: "unable to read CA cert: unable to read file " + MissingCA,
868868
},
869869
{
870870
configTLSConfig: TLSConfig{
@@ -874,7 +874,7 @@ func TestTLSConfigInvalidCA(t *testing.T) {
874874
ServerName: "",
875875
InsecureSkipVerify: false,
876876
},
877-
errorMessage: fmt.Sprintf("unable to read specified client cert: unable to read file %s", MissingCert),
877+
errorMessage: "unable to read specified client cert: unable to read file " + MissingCert,
878878
},
879879
{
880880
configTLSConfig: TLSConfig{
@@ -884,7 +884,7 @@ func TestTLSConfigInvalidCA(t *testing.T) {
884884
ServerName: "",
885885
InsecureSkipVerify: false,
886886
},
887-
errorMessage: fmt.Sprintf("unable to read specified client key: unable to read file %s", MissingKey),
887+
errorMessage: "unable to read specified client key: unable to read file " + MissingKey,
888888
},
889889
{
890890
configTLSConfig: TLSConfig{
@@ -1715,7 +1715,7 @@ func TestOAuth2UserAgent(t *testing.T) {
17151715
ClientSecret: "2",
17161716
Scopes: []string{"A", "B"},
17171717
EndpointParams: map[string]string{"hi": "hello"},
1718-
TokenURL: fmt.Sprintf("%s/token", ts.URL),
1718+
TokenURL: ts.URL + "/token",
17191719
}
17201720

17211721
rt, err := NewRoundTripperFromConfig(config, "test_oauth2", WithUserAgent("myuseragent"))
@@ -2281,7 +2281,7 @@ func TestProxyConfig_Proxy(t *testing.T) {
22812281
},
22822282
{
22832283
name: "valid proxy_url and localhost",
2284-
proxyConfig: fmt.Sprintf(`proxy_url: %s`, proxyServer.URL),
2284+
proxyConfig: "proxy_url: " + proxyServer.URL,
22852285
expectedProxyURL: proxyServer.URL,
22862286
targetURL: "http://localhost/",
22872287
},
@@ -2294,7 +2294,7 @@ no_proxy: prometheus.io`, proxyServer.URL),
22942294
},
22952295
{
22962296
name: "valid proxy_url",
2297-
proxyConfig: fmt.Sprintf(`proxy_url: %s`, proxyServer.URL),
2297+
proxyConfig: "proxy_url: " + proxyServer.URL,
22982298
expectedProxyURL: proxyServer.URL,
22992299
targetURL: "http://prometheus.io/",
23002300
},

config/tls_config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"bytes"
1818
"crypto/tls"
1919
"encoding/json"
20+
"errors"
2021
"fmt"
2122
"os"
2223
"path/filepath"
@@ -159,7 +160,7 @@ func TestTLSVersionMarshalYAML(t *testing.T) {
159160
{
160161
input: TLSVersion(999),
161162
expected: "",
162-
err: fmt.Errorf("unknown TLS version: 999"),
163+
err: errors.New("unknown TLS version: 999"),
163164
},
164165
}
165166

@@ -199,7 +200,7 @@ func TestTLSVersionMarshalJSON(t *testing.T) {
199200
{
200201
input: TLSVersion(999),
201202
expected: "",
202-
err: fmt.Errorf("unknown TLS version: 999"),
203+
err: errors.New("unknown TLS version: 999"),
203204
},
204205
}
205206

expfmt/encode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func Negotiate(h http.Header) Format {
6868
if escapeParam := ac.Params[model.EscapingKey]; escapeParam != "" {
6969
switch Format(escapeParam) {
7070
case model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
71-
escapingScheme = Format(fmt.Sprintf("; escaping=%s", escapeParam))
71+
escapingScheme = Format("; escaping=" + escapeParam)
7272
default:
7373
// If the escaping parameter is unknown, ignore it.
7474
}
@@ -101,7 +101,7 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
101101
if escapeParam := ac.Params[model.EscapingKey]; escapeParam != "" {
102102
switch Format(escapeParam) {
103103
case model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
104-
escapingScheme = Format(fmt.Sprintf("; escaping=%s", escapeParam))
104+
escapingScheme = Format("; escaping=" + escapeParam)
105105
default:
106106
// If the escaping parameter is unknown, ignore it.
107107
}

expfmt/expfmt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package expfmt
1616

1717
import (
18-
"fmt"
18+
"errors"
1919
"strings"
2020

2121
"github.com/prometheus/common/model"
@@ -109,7 +109,7 @@ func NewOpenMetricsFormat(version string) (Format, error) {
109109
if version == OpenMetricsVersion_1_0_0 {
110110
return FmtOpenMetrics_1_0_0, nil
111111
}
112-
return FmtUnknown, fmt.Errorf("unknown open metrics version string")
112+
return FmtUnknown, errors.New("unknown open metrics version string")
113113
}
114114

115115
// WithEscapingScheme returns a copy of Format with the specified escaping

expfmt/openmetrics_create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
152152
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(compliantName, "_total") {
153153
compliantName = name[:len(name)-6]
154154
}
155-
if toOM.withUnit && in.Unit != nil && !strings.HasSuffix(compliantName, fmt.Sprintf("_%s", *in.Unit)) {
156-
compliantName = compliantName + fmt.Sprintf("_%s", *in.Unit)
155+
if toOM.withUnit && in.Unit != nil && !strings.HasSuffix(compliantName, "_"+*in.Unit) {
156+
compliantName = compliantName + "_" + *in.Unit
157157
}
158158

159159
// Comments, first HELP, then TYPE.

expfmt/text_parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ func histogramMetricName(name string) string {
895895

896896
func parseFloat(s string) (float64, error) {
897897
if strings.ContainsAny(s, "pP_") {
898-
return 0, fmt.Errorf("unsupported character in float")
898+
return 0, errors.New("unsupported character in float")
899899
}
900900
return strconv.ParseFloat(s, 64)
901901
}

model/alert.go

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

1616
import (
17+
"errors"
1718
"fmt"
1819
"time"
1920
)
@@ -89,16 +90,16 @@ func (a *Alert) StatusAt(ts time.Time) AlertStatus {
8990
// Validate checks whether the alert data is inconsistent.
9091
func (a *Alert) Validate() error {
9192
if a.StartsAt.IsZero() {
92-
return fmt.Errorf("start time missing")
93+
return errors.New("start time missing")
9394
}
9495
if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
95-
return fmt.Errorf("start time must be before end time")
96+
return errors.New("start time must be before end time")
9697
}
9798
if err := a.Labels.Validate(); err != nil {
9899
return fmt.Errorf("invalid label set: %w", err)
99100
}
100101
if len(a.Labels) == 0 {
101-
return fmt.Errorf("at least one label pair required")
102+
return errors.New("at least one label pair required")
102103
}
103104
if err := a.Annotations.Validate(); err != nil {
104105
return fmt.Errorf("invalid annotations: %w", err)

model/metric.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package model
1515

1616
import (
17+
"errors"
1718
"fmt"
1819
"regexp"
1920
"sort"
@@ -443,7 +444,7 @@ func (e EscapingScheme) String() string {
443444

444445
func ToEscapingScheme(s string) (EscapingScheme, error) {
445446
if s == "" {
446-
return NoEscaping, fmt.Errorf("got empty string instead of escaping scheme")
447+
return NoEscaping, errors.New("got empty string instead of escaping scheme")
447448
}
448449
switch s {
449450
case AllowUTF8:

0 commit comments

Comments
 (0)