Skip to content

Commit 4215bc3

Browse files
authored
chore: fix gocritic linter (#1292)
1 parent aa38c72 commit 4215bc3

10 files changed

+43
-46
lines changed

.golangci.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ linters:
2121
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
2222
- gci # Gci controls golang package import order and makes it always deterministic. [fast: true, auto-fix: false]
2323
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
24+
- gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: false]
2425
- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
2526
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
2627
- gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true]
@@ -66,12 +67,10 @@ linters:
6667
- dupl # Tool for code clone detection [fast: true, auto-fix: false]
6768
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false]
6869
- exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false]
69-
- forcetypeassert # finds forced type assertions [fast: true, auto-fix: false]
7070
- funlen # Tool for detection of long functions [fast: true, auto-fix: false]
7171
- gochecknoglobals # check that no global variables exist [fast: true, auto-fix: false]
7272
- gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false]
7373
- gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false]
74-
- gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: false]
7574
- godot # Check if comments end in a period [fast: true, auto-fix: true]
7675
- godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false]
7776
- goerr113 # Golang linter to check the errors handling expressions [fast: false, auto-fix: false]

scaleway/helpers.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1818
"github.com/scaleway/scaleway-sdk-go/namegenerator"
1919
"github.com/scaleway/scaleway-sdk-go/scw"
20-
"golang.org/x/xerrors"
2120
)
2221

2322
// DefaultWaitRetryInterval is used to set the retry interval to 0 during acceptance tests
@@ -42,7 +41,7 @@ func newRegionalID(region scw.Region, id string) RegionalID {
4241

4342
func expandRegionalID(id interface{}) RegionalID {
4443
regionalID := RegionalID{}
45-
tab := strings.SplitN(id.(string), "/", -1)
44+
tab := strings.Split(id.(string), "/")
4645
if len(tab) != 2 {
4746
regionalID.ID = id.(string)
4847
} else {
@@ -73,7 +72,7 @@ func newZonedID(zone scw.Zone, id string) ZonedID {
7372

7473
func expandZonedID(id interface{}) ZonedID {
7574
zonedID := ZonedID{}
76-
tab := strings.SplitN(id.(string), "/", -1)
75+
tab := strings.Split(id.(string), "/")
7776
if len(tab) != 2 {
7877
zonedID.ID = id.(string)
7978
} else {
@@ -86,8 +85,8 @@ func expandZonedID(id interface{}) ZonedID {
8685
}
8786

8887
// parseLocalizedID parses a localizedID and extracts the resource locality and id.
89-
func parseLocalizedID(localizedID string) (locality string, ID string, err error) {
90-
tab := strings.SplitN(localizedID, "/", -1)
88+
func parseLocalizedID(localizedID string) (locality string, id string, err error) {
89+
tab := strings.Split(localizedID, "/")
9190
if len(tab) != 2 {
9291
return "", localizedID, fmt.Errorf("cant parse localized id: %s", localizedID)
9392
}
@@ -96,7 +95,7 @@ func parseLocalizedID(localizedID string) (locality string, ID string, err error
9695

9796
// parseLocalizedNestedID parses a localizedNestedID and extracts the resource locality, the inner and outer id.
9897
func parseLocalizedNestedID(localizedID string) (locality string, innerID, outerID string, err error) {
99-
tab := strings.SplitN(localizedID, "/", -1)
98+
tab := strings.Split(localizedID, "/")
10099
if len(tab) != 3 {
101100
return "", "", localizedID, fmt.Errorf("cant parse localized id: %s", localizedID)
102101
}
@@ -217,7 +216,7 @@ func isHTTPCodeError(err error, statusCode int) bool {
217216
}
218217

219218
responseError := &scw.ResponseError{}
220-
if xerrors.As(err, &responseError) && responseError.StatusCode == statusCode {
219+
if errors.As(err, &responseError) && responseError.StatusCode == statusCode {
221220
return true
222221
}
223222
return false
@@ -226,25 +225,25 @@ func isHTTPCodeError(err error, statusCode int) bool {
226225
// is404Error returns true if err is an HTTP 404 error
227226
func is404Error(err error) bool {
228227
notFoundError := &scw.ResourceNotFoundError{}
229-
return isHTTPCodeError(err, http.StatusNotFound) || xerrors.As(err, &notFoundError)
228+
return isHTTPCodeError(err, http.StatusNotFound) || errors.As(err, &notFoundError)
230229
}
231230

232231
func is412Error(err error) bool {
233232
preConditionFailedError := &scw.PreconditionFailedError{}
234-
return isHTTPCodeError(err, http.StatusPreconditionFailed) || xerrors.As(err, &preConditionFailedError)
233+
return isHTTPCodeError(err, http.StatusPreconditionFailed) || errors.As(err, &preConditionFailedError)
235234
}
236235

237236
// is403Error returns true if err is an HTTP 403 error
238237
func is403Error(err error) bool {
239238
permissionsDeniedError := &scw.PermissionsDeniedError{}
240-
return isHTTPCodeError(err, http.StatusForbidden) || xerrors.As(err, &permissionsDeniedError)
239+
return isHTTPCodeError(err, http.StatusForbidden) || errors.As(err, &permissionsDeniedError)
241240
}
242241

243242
// is409Error return true is err is an HTTP 409 error
244243
func is409Error(err error) bool {
245244
// check transient error
246245
transientStateError := &scw.TransientStateError{}
247-
return isHTTPCodeError(err, http.StatusConflict) || xerrors.As(err, &transientStateError)
246+
return isHTTPCodeError(err, http.StatusConflict) || errors.As(err, &transientStateError)
248247
}
249248

250249
// organizationIDSchema returns a standard schema for a organization_id
@@ -558,7 +557,7 @@ func diffSuppressFuncIgnoreCase(k, oldValue, newValue string, d *schema.Resource
558557
}
559558

560559
func diffSuppressFuncIgnoreCaseAndHyphen(k, oldValue, newValue string, d *schema.ResourceData) bool {
561-
return strings.Replace(strings.ToLower(oldValue), "-", "_", -1) == strings.Replace(strings.ToLower(newValue), "-", "_", -1)
560+
return strings.ReplaceAll(strings.ToLower(oldValue), "-", "_") == strings.ReplaceAll(strings.ToLower(newValue), "-", "_")
562561
}
563562

564563
// diffSuppressFuncLocality is a SuppressDiffFunc to remove the locality from an ID when checking diff.

scaleway/helpers_apple_silicon.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func asAPIWithZoneAndID(m interface{}, id string) (*applesilicon.API, scw.Zone,
4242
return asAPI, zone, ID, nil
4343
}
4444

45-
func waitForAppleSiliconServer(ctx context.Context, api *applesilicon.API, zone scw.Zone, ID string, timeout time.Duration) (*applesilicon.Server, error) {
45+
func waitForAppleSiliconServer(ctx context.Context, api *applesilicon.API, zone scw.Zone, serverID string, timeout time.Duration) (*applesilicon.Server, error) {
4646
retryInterval := defaultAppleSiliconServerRetryInterval
4747
if DefaultWaitRetryInterval != nil {
4848
retryInterval = *DefaultWaitRetryInterval
4949
}
5050

5151
server, err := api.WaitForServer(&applesilicon.WaitForServerRequest{
52-
ServerID: ID,
52+
ServerID: serverID,
5353
Zone: zone,
5454
Timeout: scw.TimeDurationPtr(timeout),
5555
RetryInterval: &retryInterval,

scaleway/helpers_baremetal.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,31 +102,31 @@ func flattenBaremetalIPs(ips []*baremetal.IP) interface{} {
102102
return flattendIPs
103103
}
104104

105-
func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
105+
func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zone, serverID string, timeout time.Duration) (*baremetal.Server, error) {
106106
retryInterval := baremetalRetryInterval
107107
if DefaultWaitRetryInterval != nil {
108108
retryInterval = *DefaultWaitRetryInterval
109109
}
110110

111111
server, err := api.WaitForServer(&baremetal.WaitForServerRequest{
112112
Zone: zone,
113-
ServerID: ID,
113+
ServerID: serverID,
114114
Timeout: scw.TimeDurationPtr(timeout),
115115
RetryInterval: &retryInterval,
116116
}, scw.WithContext(ctx))
117117

118118
return server, err
119119
}
120120

121-
func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
121+
func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone scw.Zone, serverID string, timeout time.Duration) (*baremetal.Server, error) {
122122
retryInterval := baremetalRetryInterval
123123
if DefaultWaitRetryInterval != nil {
124124
retryInterval = *DefaultWaitRetryInterval
125125
}
126126

127127
server, err := api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
128128
Zone: zone,
129-
ServerID: ID,
129+
ServerID: serverID,
130130
Timeout: scw.TimeDurationPtr(timeout),
131131
RetryInterval: &retryInterval,
132132
}, scw.WithContext(ctx))

scaleway/helpers_lb.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ func expandPrivateNetworks(data interface{}, lbID string) ([]*lbSDK.ZonedAPIAtta
117117
return res, nil
118118
}
119119

120-
func isPrivateNetworkEqual(A, B interface{}) bool {
120+
func isPrivateNetworkEqual(a, b interface{}) bool {
121121
// Find out the diff Private Network or not
122-
if _, ok := A.(*lbSDK.PrivateNetwork); ok {
123-
if _, ok := B.(*lbSDK.PrivateNetwork); ok {
124-
if A.(*lbSDK.PrivateNetwork).PrivateNetworkID == B.(*lbSDK.PrivateNetwork).PrivateNetworkID {
122+
if _, ok := a.(*lbSDK.PrivateNetwork); ok {
123+
if _, ok := b.(*lbSDK.PrivateNetwork); ok {
124+
if a.(*lbSDK.PrivateNetwork).PrivateNetworkID == b.(*lbSDK.PrivateNetwork).PrivateNetworkID {
125125
// if both has dhcp config should not update
126-
if A.(*lbSDK.PrivateNetwork).DHCPConfig != nil && B.(*lbSDK.PrivateNetwork).DHCPConfig != nil {
126+
if a.(*lbSDK.PrivateNetwork).DHCPConfig != nil && b.(*lbSDK.PrivateNetwork).DHCPConfig != nil {
127127
return true
128128
}
129129
// check static config
130-
aConfig := A.(*lbSDK.PrivateNetwork).StaticConfig
131-
bConfig := B.(*lbSDK.PrivateNetwork).StaticConfig
130+
aConfig := a.(*lbSDK.PrivateNetwork).StaticConfig
131+
bConfig := b.(*lbSDK.PrivateNetwork).StaticConfig
132132
if aConfig != nil && bConfig != nil {
133133
// check if static config is different
134134
return reflect.DeepEqual(aConfig.IPAddress, bConfig.IPAddress)
@@ -437,14 +437,14 @@ func expandLbPrivateNetworkDHCPConfig(raw interface{}) *lbSDK.PrivateNetworkDHCP
437437
return &lbSDK.PrivateNetworkDHCPConfig{}
438438
}
439439

440-
func waitForLB(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, LbID string, timeout time.Duration) (*lbSDK.LB, error) {
440+
func waitForLB(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) (*lbSDK.LB, error) {
441441
retryInterval := defaultWaitLBRetryInterval
442442
if DefaultWaitRetryInterval != nil {
443443
retryInterval = *DefaultWaitRetryInterval
444444
}
445445

446446
loadBalancer, err := lbAPI.WaitForLb(&lbSDK.ZonedAPIWaitForLBRequest{
447-
LBID: LbID,
447+
LBID: lbID,
448448
Zone: zone,
449449
Timeout: scw.TimeDurationPtr(timeout),
450450
RetryInterval: &retryInterval,
@@ -469,14 +469,14 @@ func waitForLbInstances(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zon
469469
return loadBalancer, err
470470
}
471471

472-
func waitForLBPN(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, LbID string, timeout time.Duration) ([]*lbSDK.PrivateNetwork, error) {
472+
func waitForLBPN(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) ([]*lbSDK.PrivateNetwork, error) {
473473
retryInterval := defaultWaitLBRetryInterval
474474
if DefaultWaitRetryInterval != nil {
475475
retryInterval = *DefaultWaitRetryInterval
476476
}
477477

478478
privateNetworks, err := lbAPI.WaitForLBPN(&lbSDK.ZonedAPIWaitForLBPNRequest{
479-
LBID: LbID,
479+
LBID: lbID,
480480
Zone: zone,
481481
Timeout: scw.TimeDurationPtr(timeout),
482482
RetryInterval: &retryInterval,

scaleway/helpers_rdb.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ func newEndPointPrivateNetworkDetails(id, ip, locality string) (*rdb.EndpointPri
169169
}, nil
170170
}
171171

172-
func isEndPointEqual(A, B interface{}) bool {
172+
func isEndPointEqual(a, b interface{}) bool {
173173
// Find out the diff Private Network or not
174-
if _, ok := A.(*rdb.EndpointPrivateNetworkDetails); ok {
175-
if _, ok := B.(*rdb.EndpointPrivateNetworkDetails); ok {
176-
detailsA := A.(*rdb.EndpointPrivateNetworkDetails)
177-
detailsB := B.(*rdb.EndpointPrivateNetworkDetails)
174+
if _, ok := a.(*rdb.EndpointPrivateNetworkDetails); ok {
175+
if _, ok := b.(*rdb.EndpointPrivateNetworkDetails); ok {
176+
detailsA := a.(*rdb.EndpointPrivateNetworkDetails)
177+
detailsB := b.(*rdb.EndpointPrivateNetworkDetails)
178178
return reflect.DeepEqual(detailsA, detailsB)
179179
}
180180
}

scaleway/provider_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func getTestFilePath(t *testing.T, suffix string) string {
3030
specialChars := regexp.MustCompile(`[\\?%*:|"<>. ]`)
3131

3232
// Replace nested tests separators.
33-
fileName := strings.Replace(t.Name(), "/", "-", -1)
33+
fileName := strings.ReplaceAll(t.Name(), "/", "-")
3434

3535
fileName = strcase.ToBashArg(fileName)
3636

scaleway/resource_domain_record.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func resourceScalewayDomainRecordRead(ctx context.Context, d *schema.ResourceDat
304304
currentData := d.Get("data")
305305
// check if this is an inline import. Like: "terraform import scaleway_domain_record.www subdomain.domain.tld/11111111-1111-1111-1111-111111111111"
306306
if strings.Contains(d.Id(), "/") {
307-
tab := strings.SplitN(d.Id(), "/", -1)
307+
tab := strings.Split(d.Id(), "/")
308308
if len(tab) != 2 {
309309
return diag.FromErr(fmt.Errorf("cant parse record id: %s", d.Id()))
310310
}

scaleway/resource_instance_server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,8 @@ func resourceScalewayInstanceServerRead(ctx context.Context, d *schema.ResourceD
602602
// if key != "cloud-init" {
603603
userData[key] = string(userDataValue)
604604
// } else {
605-
//_ = d.Set("cloud_init", string(userDataValue))
606-
//}
605+
// _ = d.Set("cloud_init", string(userDataValue))
606+
// }
607607
}
608608
if len(userData) > 0 {
609609
_ = d.Set("user_data", userData)

scaleway/resource_lb_frontend.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,12 @@ func resourceScalewayLbFrontendRead(ctx context.Context, d *schema.ResourceData,
269269
return nil
270270
}
271271

272-
func flattenLBACLs(ACLs []*lbSDK.ACL) interface{} {
273-
sort.Slice(ACLs, func(i, j int) bool {
274-
return ACLs[i].Index < ACLs[j].Index
272+
func flattenLBACLs(acls []*lbSDK.ACL) interface{} {
273+
sort.Slice(acls, func(i, j int) bool {
274+
return acls[i].Index < acls[j].Index
275275
})
276-
rawACLs := make([]interface{}, 0, len(ACLs))
277-
for _, apiACL := range ACLs {
276+
rawACLs := make([]interface{}, 0, len(acls))
277+
for _, apiACL := range acls {
278278
rawACLs = append(rawACLs, flattenLbACL(apiACL))
279279
}
280280
return rawACLs
@@ -403,7 +403,6 @@ func resourceScalewayLbFrontendUpdate(ctx context.Context, d *schema.ResourceDat
403403
return diag.FromErr(err)
404404
}
405405

406-
// update acl
407406
diagnostics := resourceScalewayLbFrontendUpdateACL(ctx, d, lbAPI, zone, ID)
408407
if diagnostics != nil {
409408
return diagnostics

0 commit comments

Comments
 (0)