diff --git a/.golangci.yml b/.golangci.yml index 5c58bc5d2..d8a9df58d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,6 +18,8 @@ linters: - gci # Gci controls golang package import order and makes it always deterministic. [fast: true, auto-fix: false] - gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false] - gochecksumtype # Run exhaustiveness checks on Go "sum types" [fast: false, auto-fix: false] + - goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false] + - gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: false] - 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] - gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true] - goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt. [fast: true, auto-fix: true] diff --git a/api/k8s/v1/k8s_helpers.go b/api/k8s/v1/k8s_helpers.go index 43e431dfd..c045e7868 100644 --- a/api/k8s/v1/k8s_helpers.go +++ b/api/k8s/v1/k8s_helpers.go @@ -189,7 +189,7 @@ func (s *API) WaitForClusterPool(req *WaitForClusterRequest, opts ...scw.Request PoolStatusWarning: {}, } - optsWithAllPages := append(opts, scw.WithAllPages()) + optsWithAllPages := append(opts, scw.WithAllPages()) //nolint:gocritic cluster, err := async.WaitSync(&async.WaitSyncConfig{ Get: func() (interface{}, bool, error) { diff --git a/namegenerator/name_generator.go b/namegenerator/name_generator.go index 213c34a21..b86b52c3b 100644 --- a/namegenerator/name_generator.go +++ b/namegenerator/name_generator.go @@ -853,7 +853,7 @@ var ( // formatted as "scw-adjective-surname". For example 'scw-focused-turing'. func GetRandomName(prefixes ...string) string { begin: - parts := append(prefixes, left[r.Intn(len(left))], right[r.Intn(len(right))]) + parts := append(prefixes, left[r.Intn(len(left))], right[r.Intn(len(right))]) //nolint:gocritic name := strings.Join(parts, "-") if strings.Contains(name, "boring-wozniak") /* Steve Wozniak is not boring */ { goto begin diff --git a/scw/client_option_test.go b/scw/client_option_test.go index 4fccc5df7..b16ef70dd 100644 --- a/scw/client_option_test.go +++ b/scw/client_option_test.go @@ -233,7 +233,7 @@ func TestCombinedClientOptions(t *testing.T) { t.Run(test.name, func(t *testing.T) { // set up env and config file(s) setEnv(t, test.env, test.files, dir) - test.expectedError = strings.Replace(test.expectedError, "{HOME}", dir, -1) + test.expectedError = strings.ReplaceAll(test.expectedError, "{HOME}", dir) // remove config file(s) defer cleanEnv(t, test.files, dir) diff --git a/scw/config_test.go b/scw/config_test.go index 7bc1b13a2..637d3bab6 100644 --- a/scw/config_test.go +++ b/scw/config_test.go @@ -419,7 +419,7 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { t.Run(test.name, func(t *testing.T) { // set up env and config file(s) setEnv(t, test.env, test.files, dir) - test.expectedError = strings.Replace(test.expectedError, "{HOME}", dir, -1) + test.expectedError = strings.ReplaceAll(test.expectedError, "{HOME}", dir) // remove config file(s) defer cleanEnv(t, test.files, dir) @@ -525,7 +525,7 @@ func cleanEnv(t *testing.T, files map[string]string, homeDir string) { func setEnv(t *testing.T, env, files map[string]string, homeDir string) { os.Clearenv() for key, value := range env { - value = strings.Replace(value, "{HOME}", homeDir, -1) + value = strings.ReplaceAll(value, "{HOME}", homeDir) testhelpers.AssertNoError(t, os.Setenv(key, value)) } diff --git a/scw/load_config_test.go b/scw/load_config_test.go index f5256f5b7..ac7e2cefa 100644 --- a/scw/load_config_test.go +++ b/scw/load_config_test.go @@ -119,7 +119,7 @@ func TestLoad(t *testing.T) { t.Run(test.name, func(t *testing.T) { // set up env and config file(s) setEnv(t, test.env, test.files, dir) - test.expectedError = strings.Replace(test.expectedError, "{HOME}", dir, -1) + test.expectedError = strings.ReplaceAll(test.expectedError, "{HOME}", dir) // remove config file(s) defer cleanEnv(t, test.files, dir) diff --git a/strcase/bash_arg.go b/strcase/bash_arg.go index 069647f25..f88438f0d 100644 --- a/strcase/bash_arg.go +++ b/strcase/bash_arg.go @@ -17,7 +17,7 @@ func ToBashArg(s string) string { } for _, initialism := range customInitialisms { // catch this kind of pattern: ExampleIDs ==> ExampleIds ==> example-ids - s = strings.Replace(s, initialism[0], strings.Title(strings.ToLower(initialism[0])), -1) + s = strings.ReplaceAll(s, initialism[0], strings.Title(strings.ToLower(initialism[0]))) } return toKebab(s) } @@ -41,18 +41,17 @@ func toDelimited(s string, del uint8) string { } } - if i > 0 && n[len(n)-1] != del && nextCaseIsChanged { - // add delimiter if next letter case type is changed + switch { + case i > 0 && n[len(n)-1] != del && nextCaseIsChanged: if isUpperLetter(v) { n += string(del) + string(v) } else if isLowerLetter(v) { n += string(v) + string(del) } - } else if v == ' ' || v == '-' || v == '_' { - // replace spaces and dashes with delimiter + case v == ' ' || v == '-' || v == '_': n += string(del) - } else { - n = n + string(v) + default: + n += string(v) } } n = strings.ToLower(n) diff --git a/strcase/goname.go b/strcase/goname.go index ecee32be4..d26710693 100644 --- a/strcase/goname.go +++ b/strcase/goname.go @@ -17,8 +17,8 @@ func ToPrivateGoName(s string) string { // toGoName returns a different name if it should be different. func toGoName(name string) (should string) { - name = strings.Replace(name, " ", "_", -1) - name = strings.Replace(name, "-", "_", -1) + name = strings.ReplaceAll(name, " ", "_") + name = strings.ReplaceAll(name, "-", "_") // Fast path for simple cases: "_" and all lowercase. if name == "_" { @@ -41,25 +41,21 @@ func toGoName(name string) (should string) { w, i := 0, 0 // index of start of word, scan for i+1 <= len(runes) { eow := false // whether we hit the end of a word - if i+1 == len(runes) { + switch { + case i+1 == len(runes): eow = true - } else if runes[i+1] == '_' { - // underscore; shift the remainder forward over any run of underscores + case runes[i+1] == '_': eow = true n := 1 for i+n+1 < len(runes) && runes[i+n+1] == '_' { n++ } - - // Leave at most one underscore if the underscore is between two digits if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) { n-- } - copy(runes[i+1:], runes[i+n+1:]) runes = runes[:len(runes)-n] - } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) { - // lower->non-lower + case unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]): eow = true } i++ diff --git a/strcase/kebab.go b/strcase/kebab.go index 8b7f7dfae..0ae07cc60 100644 --- a/strcase/kebab.go +++ b/strcase/kebab.go @@ -4,10 +4,10 @@ import "strings" // Converts a string to kebab-case func ToKebab(s string) string { - return strings.Replace(ToSnake(s), "_", "-", -1) + return strings.ReplaceAll(ToSnake(s), "_", "-") } // Converts a string to kebab-case func ToSpace(s string) string { - return strings.Replace(ToSnake(s), "_", " ", -1) + return strings.ReplaceAll(ToSnake(s), "_", " ") } diff --git a/strcase/snake.go b/strcase/snake.go index ba9f6c51a..80679d501 100644 --- a/strcase/snake.go +++ b/strcase/snake.go @@ -18,18 +18,17 @@ func ToSnake(s string) string { } } - if i > 0 && n[len(n)-1] != '_' && nextCaseIsChanged { - // add underscore if next letter case type is changed + switch { + case i > 0 && n[len(n)-1] != '_' && nextCaseIsChanged: if isUpperLetter(v) { n += "_" + string(v) } else if isLowerLetter(v) { n += string(v) + "_" } - } else if v == ' ' || v == '-' { - // replace spaces and dashes with underscores + case v == ' ' || v == '-': n += "_" - } else { - n = n + string(v) + default: + n += string(v) } } n = strings.ToLower(n)