Skip to content

Commit ae13910

Browse files
authored
ci: add new linters to golangci-lint (#2916)
* ci: add new linters to golangci-lint * add nlreturn
1 parent c4b4c7e commit ae13910

File tree

254 files changed

+842
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

254 files changed

+842
-1
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ linters:
1818
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
1919
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
2020
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false]
21+
- exptostd # Detects functions from golang.org/x/exp/ that can be replaced by std functions. [auto-fix]
2122
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
2223
- gci # Gci controls golang package import order and makes it always deterministic. [fast: true, auto-fix: false]
2324
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false]
@@ -48,6 +49,7 @@ linters:
4849
- musttag # enforce field tags in (un)marshaled structs [fast: false, auto-fix: false]
4950
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
5051
- nilerr # Finds the code that returns nil even if it checks that the error is not nil. [fast: false, auto-fix: false]
52+
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity [fast: true, auto-fix: true]
5153
- noctx # noctx finds sending http request without context.Context [fast: false, auto-fix: false]
5254
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
5355
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false]
@@ -74,6 +76,7 @@ linters:
7476
- unconvert # Remove unnecessary type conversions [fast: false, auto-fix: false]
7577
- unused #(megacheck): Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
7678
- usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. [fast: true, auto-fix: false]
79+
- usetesting # Reports uses of functions with replacement inside the testing package. [auto-fix]
7780
- wastedassign # wastedassign finds wasted assignment statements. [fast: false, auto-fix: false]
7881
- whitespace # Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true]
7982
- zerologlint # Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg` [fast: false, auto-fix: false]
@@ -97,7 +100,6 @@ linters:
97100
- mnd # An analyzer to detect magic numbers. [fast: true, auto-fix: false]
98101
- nestif # Reports deeply nested if statements [fast: true, auto-fix: false]
99102
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value. [fast: false, auto-fix: false]
100-
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity [fast: true, auto-fix: false]
101103
- varnamelen # checks that the length of a variable's name matches its scope [fast: false, auto-fix: false]
102104
- wsl # Whitespace Linter - Forces you to use empty lines! [fast: true, auto-fix: false]
103105

internal/acctest/acctest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func extractGeneratedNamePrefix(name string) string {
9898
// ^
9999
dashIndex = strings.LastIndex(name, "-")
100100
name = name[:dashIndex]
101+
101102
return name
102103
}
103104

@@ -155,6 +156,7 @@ func compareJSONBodies(expected, actual map[string]interface{}) bool {
155156
return false
156157
}
157158
}
159+
158160
return true
159161
}
160162

internal/acctest/checks.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func CheckResourceIDChanged(resourceName string, resourceID *string) resource.Te
2626
return errors.New("resource ID persisted when it should have changed")
2727
}
2828
*resourceID = rs.Primary.ID
29+
2930
return nil
3031
}
3132
}
@@ -42,6 +43,7 @@ func CheckResourceIDPersisted(resourceName string, resourceID *string) resource.
4243
return errors.New("resource ID changed when it should have persisted")
4344
}
4445
*resourceID = rs.Primary.ID
46+
4547
return nil
4648
}
4749
}
@@ -90,6 +92,7 @@ func CheckResourceAttrFunc(name string, key string, test func(string) error) res
9092
if err != nil {
9193
return fmt.Errorf("test for %s %s did not pass test: %s", name, key, err)
9294
}
95+
9396
return nil
9497
}
9598
}
@@ -100,6 +103,7 @@ func CheckResourceAttrIPv4(name string, key string) resource.TestCheckFunc {
100103
if ip.To4() == nil {
101104
return fmt.Errorf("%s is not a valid IPv4", value)
102105
}
106+
103107
return nil
104108
})
105109
}
@@ -110,6 +114,7 @@ func CheckResourceAttrIPv6(name string, key string) resource.TestCheckFunc {
110114
if ip.To16() == nil {
111115
return fmt.Errorf("%s is not a valid IPv6", value)
112116
}
117+
113118
return nil
114119
})
115120
}
@@ -120,6 +125,7 @@ func CheckResourceAttrIP(name string, key string) resource.TestCheckFunc {
120125
if ip == nil {
121126
return fmt.Errorf("%s is not a valid IP", value)
122127
}
128+
123129
return nil
124130
})
125131
}

internal/acctest/domain.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ func init() {
3636
for _, reservedDomain := range reservedDomains {
3737
if reservedDomain.MatchString(TestDomain) {
3838
isReserved = true
39+
3940
break
4041
}
4142
}
4243

4344
if isReserved {
4445
logging.L.Warningf("TF_TEST_DOMAIN cannot be a Scaleway required domain. Please use another one.")
46+
4547
return
4648
}
4749

internal/acctest/sweepers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func Sweep(f func(scwClient *scw.Client) error) error {
1616
if err != nil {
1717
return err
1818
}
19+
1920
return f(m.ScwClient())
2021
}
2122

@@ -30,6 +31,7 @@ func SweepZones(zones []scw.Zone, f func(scwClient *scw.Client, zone scw.Zone) e
3031
logging.L.Warningf("error running sweepZones, ignoring: %s", err)
3132
}
3233
}
34+
3335
return nil
3436
}
3537

@@ -41,6 +43,7 @@ func SweepRegions(regions []scw.Region, f func(scwClient *scw.Client, region scw
4143

4244
return SweepZones(zones, func(scwClient *scw.Client, zone scw.Zone) error {
4345
r, _ := zone.Region()
46+
4447
return f(scwClient, r)
4548
})
4649
}
@@ -56,5 +59,6 @@ func sharedClientForZone(zone scw.Zone) (*scw.Client, error) {
5659
if err != nil {
5760
return nil, err
5861
}
62+
5963
return m.ScwClient(), nil
6064
}

internal/acctest/validate_cassettes_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func getTestFiles() (map[string]struct{}, error) {
4040
if isCassette && !isException {
4141
filesMap[fileNameWithoutExtSuffix(path)] = struct{}{}
4242
}
43+
4344
return nil
4445
})
4546
if err != nil {
@@ -87,8 +88,10 @@ func checkErrCodeExcept(i *cassette.Interaction, c *cassette.Cassette, codes ...
8788
return true
8889
}
8990
}
91+
9092
return false
9193
}
94+
9295
return true
9396
}
9497

internal/acctest/vcr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func compareJSONFields(expected, actualI interface{}) bool {
7070
if _, isString := expected.(string); !isString {
7171
return false
7272
}
73+
7374
return compareJSONFieldsStrings(expected.(string), actual)
7475
default:
7576
// Consider equality when not handled
@@ -116,6 +117,7 @@ func cassetteBodyMatcher(actualRequest *http.Request, cassetteRequest cassette.R
116117
} else if _, isFile := actualRequest.Body.(*os.File); isFile {
117118
return true // Body match if request is sending a file, maybe do more check here
118119
}
120+
119121
return false
120122
}
121123

@@ -243,6 +245,7 @@ func cassetteSensitiveFieldsAnonymizer(i *cassette.Interaction) error {
243245
return fmt.Errorf("failed to marshal anonymized body: %w", err)
244246
}
245247
i.Response.Body = string(anonymizedBody)
248+
246249
return nil
247250
}
248251

@@ -290,6 +293,7 @@ func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.C
290293
delete(i.Request.Headers, "x-auth-token")
291294
delete(i.Request.Headers, "X-Auth-Token")
292295
delete(i.Request.Headers, "Authorization")
296+
293297
return nil
294298
}, recorder.BeforeSaveHook)
295299

internal/cdf/locality.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func getLocality(diff *schema.ResourceDiff, m interface{}) string {
5757
region, _ := meta.ExtractRegion(diff, m)
5858
loc = region.String()
5959
}
60+
6061
return loc
6162
}
6263

@@ -91,6 +92,7 @@ func LocalityCheck(keys ...string) schema.CustomizeDiffFunc {
9192
}
9293
}
9394
}
95+
9496
return nil
9597
}
9698
}

internal/datasource/schemas.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func SchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*schema.S
4949
switch v.Type {
5050
case schema.TypeSet:
5151
dv.Set = v.Set
52+
5253
fallthrough
5354
case schema.TypeList:
5455
// List & Set types are generally used for 2 cases:
@@ -70,6 +71,7 @@ func SchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*schema.S
7071
}
7172
ds[k] = dv
7273
}
74+
7375
return ds
7476
}
7577

internal/datasource/search.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func FindExact[T any](slice []T, finder func(T) bool, searchName string) (T, err
1818
if foundFlag {
1919
// More than one element found with the same search name
2020
var zero T
21+
2122
return zero, fmt.Errorf("multiple elements found with the name %s", searchName)
2223
}
2324
found = elem
@@ -27,6 +28,7 @@ func FindExact[T any](slice []T, finder func(T) bool, searchName string) (T, err
2728

2829
if !foundFlag {
2930
var zero T
31+
3032
return zero, fmt.Errorf("no element found with the name %s", searchName)
3133
}
3234

@@ -51,6 +53,7 @@ func SingularDataSourceFindError(resourceType string, err error) error {
5153
// retry.NotFoundError.
5254
func notFound(err error) bool {
5355
var e *retry.NotFoundError // nosemgrep:ci.is-not-found-error
56+
5457
return errors.As(err, &e)
5558
}
5659

@@ -65,6 +68,7 @@ func (e *TooManyResultsError) Error() string {
6568

6669
func (e *TooManyResultsError) Is(err error) bool {
6770
_, ok := err.(*TooManyResultsError) //nolint:errorlint // Explicitly does *not* match down the error tree
71+
6872
return ok
6973
}
7074

internal/dsf/time.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func Duration(_, oldValue, newValue string, _ *schema.ResourceData) bool {
1515
if err1 != nil || err2 != nil {
1616
return false
1717
}
18+
1819
return d1 == d2
1920
}
2021

@@ -27,5 +28,6 @@ func TimeRFC3339(_, oldValue, newValue string, _ *schema.ResourceData) bool {
2728
if err1 != nil || err2 != nil {
2829
return false
2930
}
31+
3032
return t1.Equal(t2)
3133
}

internal/httperrors/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,35 @@ func IsHTTPCodeError(err error, statusCode int) bool {
1717
if errors.As(err, &responseError) && responseError.StatusCode == statusCode {
1818
return true
1919
}
20+
2021
return false
2122
}
2223

2324
// Is404 returns true if err is an HTTP 404 error
2425
func Is404(err error) bool {
2526
notFoundError := &scw.ResourceNotFoundError{}
27+
2628
return IsHTTPCodeError(err, http.StatusNotFound) || errors.As(err, &notFoundError)
2729
}
2830

2931
func Is412(err error) bool {
3032
preConditionFailedError := &scw.PreconditionFailedError{}
33+
3134
return IsHTTPCodeError(err, http.StatusPreconditionFailed) || errors.As(err, &preConditionFailedError)
3235
}
3336

3437
// Is403 returns true if err is an HTTP 403 error
3538
func Is403(err error) bool {
3639
permissionsDeniedError := &scw.PermissionsDeniedError{}
40+
3741
return IsHTTPCodeError(err, http.StatusForbidden) || errors.As(err, &permissionsDeniedError)
3842
}
3943

4044
// Is409 return true is err is an HTTP 409 error
4145
func Is409(err error) bool {
4246
// check transient error
4347
transientStateError := &scw.TransientStateError{}
48+
4449
return IsHTTPCodeError(err, http.StatusConflict) || errors.As(err, &transientStateError)
4550
}
4651

internal/locality/ids.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ func ExpandID(id interface{}) string {
66
if err != nil {
77
return id.(string)
88
}
9+
910
return ID
1011
}
1112

@@ -18,5 +19,6 @@ func ExpandIDs(data interface{}) []string {
1819
expandedID := ExpandID(s.(string))
1920
expandedIDs = append(expandedIDs, expandedID)
2021
}
22+
2123
return expandedIDs
2224
}

internal/locality/parsing.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func ParseLocalizedID(localizedID string) (locality, id string, err error) {
1111
if len(tab) != 2 {
1212
return "", localizedID, fmt.Errorf("cant parse localized id: %s", localizedID)
1313
}
14+
1415
return tab[0], tab[1], nil
1516
}
1617

@@ -20,6 +21,7 @@ func ParseLocalizedNestedID(localizedID string) (locality string, innerID, outer
2021
if len(tab) < 3 {
2122
return "", "", localizedID, fmt.Errorf("cant parse localized id: %s", localizedID)
2223
}
24+
2325
return tab[0], tab[1], strings.Join(tab[2:], "/"), nil
2426
}
2527

@@ -53,5 +55,6 @@ func CompareLocalities(loc1, loc2 string) bool {
5355
if strings.HasPrefix(loc1, loc2) || strings.HasPrefix(loc2, loc1) {
5456
return true
5557
}
58+
5659
return false
5760
}

internal/locality/regional/ids.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func ParseNestedID(regionalNestedID string) (region scw.Region, outerID, innerID
5252
}
5353

5454
region, err = scw.ParseRegion(loc)
55+
5556
return
5657
}
5758

@@ -63,6 +64,7 @@ func ParseID(regionalID string) (region scw.Region, id string, err error) {
6364
}
6465

6566
region, err = scw.ParseRegion(loc)
67+
6668
return
6769
}
6870

@@ -74,5 +76,6 @@ func NewRegionalIDs(region scw.Region, ids []string) []string {
7476
for i, id := range ids {
7577
flattenedIDs[i] = NewIDString(region, id)
7678
}
79+
7780
return flattenedIDs
7881
}

internal/locality/validation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func ValidateStringInSliceWithWarning(correctValues []string, field string) sche
1919
AttributePath: path,
2020
})
2121
}
22+
2223
return res
2324
}
2425
}

internal/locality/zonal/ids.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func ParseID(zonedID string) (zone scw.Zone, id string, err error) {
5757
}
5858

5959
zone, err = scw.ParseZone(rawZone)
60+
6061
return
6162
}
6263

@@ -68,5 +69,6 @@ func ParseNestedID(zonedNestedID string) (zone scw.Zone, outerID, innerID string
6869
}
6970

7071
zone, err = scw.ParseZone(rawZone)
72+
7173
return
7274
}

0 commit comments

Comments
 (0)