From f73dcd97f52585c5100e3c36f3717ea56f30a56b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 1 Feb 2024 15:22:21 +0100 Subject: [PATCH] chore(lint): add support for prealloc and asasalint --- .golangci.yml | 2 ++ scaleway/helpers.go | 20 ++++++++++---------- scaleway/helpers_baremetal.go | 2 +- scaleway/helpers_flexible_ip.go | 2 +- scaleway/helpers_instance.go | 10 ++++++---- scaleway/helpers_k8s.go | 2 +- scaleway/helpers_mnq_queue.go | 2 +- scaleway/helpers_rdb.go | 4 ++-- scaleway/helpers_redis.go | 2 +- scaleway/resource_object_bucket_acl.go | 4 ++-- scaleway/resource_rdb_acl.go | 4 ++-- 11 files changed, 29 insertions(+), 25 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 673dfdf33..b89af4937 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,6 +3,7 @@ linters: # Run golangci-lint linters to see the list of all linters # Please keep them sorted alphabetically enable: + - asasalint # check for pass []any as any in variadic func(...any) [fast: false, auto-fix: false] - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers [fast: true, auto-fix: false] - bidichk # Checks for dangerous unicode character sequences [fast: true, auto-fix: false] - bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false] @@ -44,6 +45,7 @@ linters: - noctx # noctx finds sending http request without context.Context [fast: false, auto-fix: false] - nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false] - nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false] + - prealloc # Finds slice declarations that could potentially be pre-allocated [fast: true, auto-fix: false] - predeclared # find code that shadows one of Go's predeclared identifiers [fast: true, auto-fix: false] - promlinter # Check Prometheus metrics naming via promlint [fast: true, auto-fix: false] - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. [fast: false, auto-fix: false] diff --git a/scaleway/helpers.go b/scaleway/helpers.go index 9b2945265..55f7a771f 100644 --- a/scaleway/helpers.go +++ b/scaleway/helpers.go @@ -377,21 +377,21 @@ func zoneSchema() *schema.Schema { } func allZones() []string { - var allZones []string + zones := make([]string, 0, len(scw.AllZones)) for _, z := range scw.AllZones { - allZones = append(allZones, z.String()) + zones = append(zones, z.String()) } - return allZones + return zones } func allRegions() []string { - var allRegions []string + regions := make([]string, 0, len(scw.AllRegions)) for _, z := range scw.AllRegions { - allRegions = append(allRegions, z.String()) + regions = append(regions, z.String()) } - return allRegions + return regions } // regionSchema returns a standard schema for a zone @@ -487,7 +487,7 @@ func expandStringWithDefault(data interface{}, defaultValue string) string { } func expandStrings(data interface{}) []string { - var stringSlice []string + stringSlice := make([]string, 0, len(data.([]interface{}))) for _, s := range data.([]interface{}) { // zero-value is nil, ["foo", ""] if s == nil { @@ -499,7 +499,7 @@ func expandStrings(data interface{}) []string { } func expandStringsPtr(data interface{}) *[]string { - var stringSlice []string + stringSlice := make([]string, 0, len(data.([]interface{}))) if _, ok := data.([]interface{}); !ok || data == nil { return nil } @@ -534,7 +534,7 @@ func expandUpdatedStringsPtr(data interface{}) *[]string { } func expandSliceIDsPtr(rawIDs interface{}) *[]string { - var stringSlice []string + stringSlice := make([]string, 0, len(rawIDs.([]interface{}))) if _, ok := rawIDs.([]interface{}); !ok || rawIDs == nil { return &stringSlice } @@ -545,7 +545,7 @@ func expandSliceIDsPtr(rawIDs interface{}) *[]string { } func expandStringsOrEmpty(data interface{}) []string { - var stringSlice []string + stringSlice := make([]string, 0, len(data.([]interface{}))) if _, ok := data.([]interface{}); !ok || data == nil { return stringSlice } diff --git a/scaleway/helpers_baremetal.go b/scaleway/helpers_baremetal.go index 9b96b963b..d3e982c7a 100644 --- a/scaleway/helpers_baremetal.go +++ b/scaleway/helpers_baremetal.go @@ -88,7 +88,7 @@ func expandBaremetalOptions(i interface{}) ([]*baremetal.ServerOption, error) { } func expandBaremetalPrivateNetworks(pn interface{}) []string { - var privateNetworkIDs []string + privateNetworkIDs := make([]string, 0, len(pn.(*schema.Set).List())) for _, op := range pn.(*schema.Set).List() { rawPN := op.(map[string]interface{}) diff --git a/scaleway/helpers_flexible_ip.go b/scaleway/helpers_flexible_ip.go index 510b7abf3..9f3cc453e 100644 --- a/scaleway/helpers_flexible_ip.go +++ b/scaleway/helpers_flexible_ip.go @@ -70,7 +70,7 @@ func flattenFlexibleIPMacAddress(mac *flexibleip.MACAddress) interface{} { } func expandServerIDs(data interface{}) []string { - var expandedIDs []string + expandedIDs := make([]string, 0, len(data.([]interface{}))) for _, s := range data.([]interface{}) { if s == nil { s = "" diff --git a/scaleway/helpers_instance.go b/scaleway/helpers_instance.go index b80489fbe..d5211eee9 100644 --- a/scaleway/helpers_instance.go +++ b/scaleway/helpers_instance.go @@ -82,12 +82,13 @@ func instanceAPIWithZoneAndNestedID(m interface{}, zonedNestedID string) (*insta // orderVolumes return an ordered slice based on the volume map key "0", "1", "2",... func orderVolumes(v map[string]*instance.Volume) []*instance.Volume { - var indexes []string + indexes := make([]string, 0, len(v)) for index := range v { indexes = append(indexes, index) } sort.Strings(indexes) - var orderedVolumes []*instance.Volume + + orderedVolumes := make([]*instance.Volume, 0, len(indexes)) for _, index := range indexes { orderedVolumes = append(orderedVolumes, v[index]) } @@ -96,12 +97,13 @@ func orderVolumes(v map[string]*instance.Volume) []*instance.Volume { // sortVolumeServer return an ordered slice based on the volume map key "0", "1", "2",... func sortVolumeServer(v map[string]*instance.VolumeServer) []*instance.VolumeServer { - var indexes []string + indexes := make([]string, 0, len(v)) for index := range v { indexes = append(indexes, index) } sort.Strings(indexes) - var sortedVolumes []*instance.VolumeServer + + sortedVolumes := make([]*instance.VolumeServer, 0, len(indexes)) for _, index := range indexes { sortedVolumes = append(sortedVolumes, v[index]) } diff --git a/scaleway/helpers_k8s.go b/scaleway/helpers_k8s.go index 572e29370..864ee97d0 100644 --- a/scaleway/helpers_k8s.go +++ b/scaleway/helpers_k8s.go @@ -152,7 +152,7 @@ func waitK8SPoolReady(ctx context.Context, k8sAPI *k8s.API, region scw.Region, p // convert a list of nodes to a list of map func convertNodes(res *k8s.ListNodesResponse) []map[string]interface{} { - var result []map[string]interface{} + result := make([]map[string]interface{}, 0, len(res.Nodes)) for _, node := range res.Nodes { n := make(map[string]interface{}) n["name"] = node.Name diff --git a/scaleway/helpers_mnq_queue.go b/scaleway/helpers_mnq_queue.go index ab730f6e3..386b50d87 100644 --- a/scaleway/helpers_mnq_queue.go +++ b/scaleway/helpers_mnq_queue.go @@ -134,7 +134,7 @@ var SQSAttributesToResourceMap = map[string]string{ // Returns all managed SQS attribute names func getSQSAttributeNames() []*string { - var attributeNames []*string + attributeNames := make([]*string, 0, len(SQSAttributesToResourceMap)) for attribute := range SQSAttributesToResourceMap { attributeNames = append(attributeNames, aws.String(attribute)) diff --git a/scaleway/helpers_rdb.go b/scaleway/helpers_rdb.go index 6cf6e8756..b0e7de87f 100644 --- a/scaleway/helpers_rdb.go +++ b/scaleway/helpers_rdb.go @@ -56,7 +56,7 @@ func flattenInstanceSettings(settings []*rdb.InstanceSetting) interface{} { func expandInstanceSettings(i interface{}) []*rdb.InstanceSetting { rawRule := i.(map[string]interface{}) - var res []*rdb.InstanceSetting + res := make([]*rdb.InstanceSetting, 0, len(rawRule)) for key, value := range rawRule { res = append(res, &rdb.InstanceSetting{ Name: key, @@ -114,7 +114,7 @@ func expandPrivateNetwork(data interface{}, exist bool, enableIpam bool) ([]*rdb return nil, nil } - var res []*rdb.EndpointSpec + res := make([]*rdb.EndpointSpec, 0, len(data.([]interface{}))) for _, pn := range data.([]interface{}) { r := pn.(map[string]interface{}) spec := &rdb.EndpointSpec{ diff --git a/scaleway/helpers_redis.go b/scaleway/helpers_redis.go index c14d719b4..2e9170b06 100644 --- a/scaleway/helpers_redis.go +++ b/scaleway/helpers_redis.go @@ -62,7 +62,7 @@ func expandRedisPrivateNetwork(data []interface{}) ([]*redis.EndpointSpec, error if data == nil { return nil, nil } - var epSpecs []*redis.EndpointSpec + epSpecs := make([]*redis.EndpointSpec, 0, len(data)) for _, rawPN := range data { pn := rawPN.(map[string]interface{}) diff --git a/scaleway/resource_object_bucket_acl.go b/scaleway/resource_object_bucket_acl.go index adecd173e..1a1b591a3 100644 --- a/scaleway/resource_object_bucket_acl.go +++ b/scaleway/resource_object_bucket_acl.go @@ -208,7 +208,7 @@ func expandBucketACLAccessControlPolicy(l []interface{}) *s3.AccessControlPolicy } func expandBucketACLAccessControlPolicyGrants(l []interface{}) []*s3.Grant { - var grants []*s3.Grant + grants := make([]*s3.Grant, 0, len(l)) for _, tfMapRaw := range l { tfMap, ok := tfMapRaw.(map[string]interface{}) @@ -297,7 +297,7 @@ func flattenBucketACLAccessControlPolicy(output *s3.GetBucketAclOutput) []interf } func flattenBucketACLAccessControlPolicyGrants(grants []*s3.Grant) []interface{} { - var results []interface{} + results := make([]interface{}, 0, len(grants)) for _, grant := range grants { if grant == nil { diff --git a/scaleway/resource_rdb_acl.go b/scaleway/resource_rdb_acl.go index 447f567b4..6f1d61d06 100644 --- a/scaleway/resource_rdb_acl.go +++ b/scaleway/resource_rdb_acl.go @@ -249,7 +249,7 @@ func rdbACLExpand(data []interface{}) ([]*rdb.ACLRuleRequest, error) { } func rdbACLRulesFlattenFromSchema(rules []*rdb.ACLRule, dataFromSchema []interface{}) ([]map[string]interface{}, []error) { - var res []map[string]interface{} + res := make([]map[string]interface{}, 0, len(dataFromSchema)) var errors []error ruleMap := make(map[string]*rdb.ACLRule) for _, rule := range rules { @@ -300,7 +300,7 @@ func mergeDiffToSchema(rulesFromSchema map[string]struct{}, ruleMap map[string]* } func rdbACLRulesFlatten(rules []*rdb.ACLRule) []map[string]interface{} { - var res []map[string]interface{} + res := make([]map[string]interface{}, 0, len(rules)) for _, rule := range rules { r := map[string]interface{}{ "ip": rule.IP.String(),