Skip to content

Commit 08a78bc

Browse files
authored
chore: add support for perfsprint and other linters (#2186)
1 parent 9027da5 commit 08a78bc

File tree

8 files changed

+33
-26
lines changed

8 files changed

+33
-26
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ linters:
3636
- grouper # An analyzer to analyze expression groups. [fast: true, auto-fix: false]
3737
- importas # Enforces consistent import aliases [fast: false, auto-fix: false]
3838
- mirror # reports wrong mirror patterns of bytes/strings usage [fast: false, auto-fix: false]
39+
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
40+
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
41+
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false]
42+
- perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative. [fast: false, auto-fix: false]
43+
- rowserrcheck # checks whether Err of rows is checked successfully [fast: false, auto-fix: false]
44+
- sloglint # ensure consistent code style when using log/slog [fast: false, auto-fix: false]
45+
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. [fast: false, auto-fix: false]
3946
- staticcheck #(megacheck): Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false, auto-fix: false]
4047
- unused #(megacheck): Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
4148
- usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. [fast: true, auto-fix: false]

api/instance/v1/server_utils.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,21 @@ func (s *API) GetServerUserData(req *GetServerUserDataRequest, opts ...scw.Reque
196196
req.Zone = defaultZone
197197
}
198198

199-
if fmt.Sprint(req.Zone) == "" {
199+
if req.Zone == "" {
200200
return nil, errors.New("field Zone cannot be empty in request")
201201
}
202202

203-
if fmt.Sprint(req.ServerID) == "" {
203+
if req.ServerID == "" {
204204
return nil, errors.New("field ServerID cannot be empty in request")
205205
}
206206

207-
if fmt.Sprint(req.Key) == "" {
207+
if req.Key == "" {
208208
return nil, errors.New("field Key cannot be empty in request")
209209
}
210210

211211
scwReq := &scw.ScalewayRequest{
212212
Method: "GET",
213-
Path: "/instance/v1/zones/" + fmt.Sprint(req.Zone) + "/servers/" + fmt.Sprint(req.ServerID) + "/user_data/" + fmt.Sprint(req.Key),
213+
Path: "/instance/v1/zones/" + fmt.Sprint(req.Zone) + "/servers/" + req.ServerID + "/user_data/" + req.Key,
214214
Headers: http.Header{},
215215
}
216216

@@ -245,15 +245,15 @@ func (s *API) SetServerUserData(req *SetServerUserDataRequest, opts ...scw.Reque
245245
req.Zone = defaultZone
246246
}
247247

248-
if fmt.Sprint(req.Zone) == "" {
248+
if req.Zone == "" {
249249
return errors.New("field Zone cannot be empty in request")
250250
}
251251

252-
if fmt.Sprint(req.ServerID) == "" {
252+
if req.ServerID == "" {
253253
return errors.New("field ServerID cannot be empty in request")
254254
}
255255

256-
if fmt.Sprint(req.Key) == "" {
256+
if req.Key == "" {
257257
return errors.New("field Key cannot be empty in request")
258258
}
259259

@@ -263,7 +263,7 @@ func (s *API) SetServerUserData(req *SetServerUserDataRequest, opts ...scw.Reque
263263

264264
scwReq := &scw.ScalewayRequest{
265265
Method: "PATCH",
266-
Path: "/instance/v1/zones/" + fmt.Sprint(req.Zone) + "/servers/" + fmt.Sprint(req.ServerID) + "/user_data/" + fmt.Sprint(req.Key),
266+
Path: "/instance/v1/zones/" + fmt.Sprint(req.Zone) + "/servers/" + req.ServerID + "/user_data/" + req.Key,
267267
Headers: http.Header{},
268268
}
269269

@@ -304,7 +304,7 @@ func (s *API) GetAllServerUserData(req *GetAllServerUserDataRequest, opts ...scw
304304
return nil, errors.New("field Zone cannot be empty in request")
305305
}
306306

307-
if fmt.Sprint(req.ServerID) == "" {
307+
if req.ServerID == "" {
308308
return nil, errors.New("field ServerID cannot be empty in request")
309309
}
310310

@@ -357,11 +357,11 @@ func (s *API) SetAllServerUserData(req *SetAllServerUserDataRequest, opts ...scw
357357
req.Zone = defaultZone
358358
}
359359

360-
if fmt.Sprint(req.Zone) == "" {
360+
if req.Zone == "" {
361361
return errors.New("field Zone cannot be empty in request")
362362
}
363363

364-
if fmt.Sprint(req.ServerID) == "" {
364+
if req.ServerID == "" {
365365
return errors.New("field ServerID cannot be empty in request")
366366
}
367367

internal/async/wait_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package async
22

33
import (
4-
"fmt"
4+
"errors"
55
"testing"
66
"time"
77

@@ -71,7 +71,7 @@ func TestWaitSync(t *testing.T) {
7171
Timeout: time.Second,
7272
},
7373
expValue: nil,
74-
expErr: fmt.Errorf("timeout after 1s"),
74+
expErr: errors.New("timeout after 1s"),
7575
},
7676
{
7777
name: "With interval",
@@ -103,7 +103,7 @@ func TestWaitSync(t *testing.T) {
103103
IntervalStrategy: LinearIntervalStrategy(2 * time.Second),
104104
},
105105
expValue: nil,
106-
expErr: fmt.Errorf("timeout after 2s"),
106+
expErr: errors.New("timeout after 2s"),
107107
},
108108
}
109109
for _, c := range testsCases {

internal/testhelpers/httprecorder/recorder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func CreateRecordedScwClient(cassetteName string) (*scw.Client, *recorder.Record
4646
}
4747

4848
// Setup recorder and scw client
49-
r, err := recorder.NewAsMode(fmt.Sprintf("testdata/%s", cassetteName), recorderMode, nil)
49+
r, err := recorder.NewAsMode("testdata/"+cassetteName, recorderMode, nil)
5050
if err != nil {
5151
return nil, nil, err
5252
}

scw/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func (c *Client) doListAll(req *ScalewayRequest, res interface{}) (err error) {
357357
func (c *Client) doListLocalities(req *ScalewayRequest, res interface{}, localities []string) (err error) {
358358
path := req.Path
359359
if !strings.Contains(path, "%locality%") {
360-
return fmt.Errorf("request is not a valid locality request")
360+
return errors.New("request is not a valid locality request")
361361
}
362362
// Requests are parallelized
363363
responseMutex := sync.Mutex{}
@@ -418,7 +418,7 @@ func (c *Client) doListZones(req *ScalewayRequest, res interface{}, zones []Zone
418418
}
419419
}
420420
if !strings.Contains(req.Path, "%locality%") {
421-
return fmt.Errorf("request is not a valid zoned request")
421+
return errors.New("request is not a valid zoned request")
422422
}
423423
localities := make([]string, 0, len(zones))
424424
for _, zone := range zones {
@@ -449,7 +449,7 @@ func (c *Client) doListRegions(req *ScalewayRequest, res interface{}, regions []
449449
}
450450
}
451451
if !strings.Contains(req.Path, "%locality%") {
452-
return fmt.Errorf("request is not a valid zoned request")
452+
return errors.New("request is not a valid zoned request")
453453
}
454454
localities := make([]string, 0, len(regions))
455455
for _, region := range regions {

scw/custom_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type Money struct {
112112
// - (value = 1.123456789, precision = 9) => Money{Units = 1, Nanos = 123456789}
113113
func NewMoneyFromFloat(value float64, currencyCode string, precision int) *Money {
114114
if precision > 9 {
115-
panic(fmt.Errorf("max precision is 9"))
115+
panic(errors.New("max precision is 9"))
116116
}
117117

118118
strValue := strconv.FormatFloat(value, 'f', precision, 64)
@@ -206,7 +206,7 @@ func (tsp *TimeSeriesPoint) UnmarshalJSON(b []byte) error {
206206
}
207207

208208
if len(point) != 2 {
209-
return fmt.Errorf("invalid point array")
209+
return errors.New("invalid point array")
210210
}
211211

212212
strTimestamp, isStrTimestamp := point[0].(string)

scw/custom_types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scw
22

33
import (
44
"encoding/json"
5-
"fmt"
5+
"errors"
66
"io"
77
"net"
88
"strings"
@@ -269,7 +269,7 @@ func TestTimeSeries_UnmarshalJSON(t *testing.T) {
269269
{
270270
name: "with timestamp error",
271271
json: `{"name":"cpu_usage","points":[["2019/08/08T15-00-00Z",0.2]]}`,
272-
err: fmt.Errorf("2019/08/08T15-00-00Z timestamp is not in RFC 3339 format"),
272+
err: errors.New("2019/08/08T15-00-00Z timestamp is not in RFC 3339 format"),
273273
},
274274
}
275275

scw/errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (e *ResponseError) UnmarshalJSON(b []byte) error {
6161
func (e *ResponseError) IsScwSdkError() {}
6262

6363
func (e *ResponseError) Error() string {
64-
s := fmt.Sprintf("scaleway-sdk-go: http error %s", e.Status)
64+
s := "scaleway-sdk-go: http error " + e.Status
6565

6666
if e.Resource != "" {
6767
s = fmt.Sprintf("%s: resource %s", s, e.Resource)
@@ -472,7 +472,7 @@ func NewInvalidClientOptionError(format string, a ...interface{}) *InvalidClient
472472
func (e InvalidClientOptionError) IsScwSdkError() {}
473473

474474
func (e InvalidClientOptionError) Error() string {
475-
return fmt.Sprintf("scaleway-sdk-go: %s", e.errorType)
475+
return "scaleway-sdk-go: " + e.errorType
476476
}
477477

478478
// ConfigFileNotFound indicates that the config file could not be found
@@ -537,7 +537,7 @@ func (r DeniedAuthenticationError) Error() string {
537537
case "expired":
538538
reason = method + " is expired"
539539
}
540-
return fmt.Sprintf("scaleway-sdk-go: denied authentication: %s", reason)
540+
return "scaleway-sdk-go: denied authentication: " + reason
541541
}
542542

543543
func (r DeniedAuthenticationError) IsScwSdkError() {}
@@ -564,7 +564,7 @@ func (r PreconditionFailedError) Error() string {
564564
msg += ", " + r.HelpMessage
565565
}
566566

567-
return fmt.Sprintf("scaleway-sdk-go: precondition failed: %s", msg)
567+
return "scaleway-sdk-go: precondition failed: " + msg
568568
}
569569

570570
func (r PreconditionFailedError) IsScwSdkError() {}

0 commit comments

Comments
 (0)