Skip to content

chore: add support for gocritic and goconst #2180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion api/k8s/v1/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion namegenerator/name_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scw/client_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions scw/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}

Expand Down
2 changes: 1 addition & 1 deletion scw/load_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions strcase/bash_arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
16 changes: 6 additions & 10 deletions strcase/goname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "_" {
Expand All @@ -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++
Expand Down
4 changes: 2 additions & 2 deletions strcase/kebab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), "_", " ")
}
11 changes: 5 additions & 6 deletions strcase/snake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down