Skip to content

Commit

Permalink
Merge branch 'master' into gocritic
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Aug 22, 2024
2 parents fd159c4 + 151e9de commit 914e1e5
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ linters:
- govet #(vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
- importas # Enforces consistent import aliases [fast: false, auto-fix: false]
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
- intrange # intrange is a linter to find places where for loops could make use of an integer range. [fast: true, auto-fix: false]
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
Expand All @@ -45,6 +46,7 @@ linters:
- unconvert # Remove unnecessary type conversions [fast: false, auto-fix: false]
- unparam # Reports unused function parameters [fast: false, auto-fix: false]
- unused #(megacheck): Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
- usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. [fast: true, auto-fix: false]
- whitespace # Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true]
- zerologlint # Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg` [fast: false, auto-fix: false]

Expand Down
4 changes: 2 additions & 2 deletions internal/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func GetArgType(argType reflect.Type, name string) (reflect.Type, error) {
// We construct two caches:
anonymousFieldIndexes := []int(nil)
fieldIndexByName := map[string]int{}
for i := 0; i < argType.NumField(); i++ {
for i := range argType.NumField() {
field := argType.Field(i)
if field.Anonymous {
anonymousFieldIndexes = append(anonymousFieldIndexes, i)
Expand Down Expand Up @@ -283,7 +283,7 @@ func listArgTypeFields(base string, argType reflect.Type) []string {
case reflect.Struct:
fields := []string(nil)

for i := 0; i < argType.NumField(); i++ {
for i := range argType.NumField() {
field := argType.Field(i)
fieldBase := base

Expand Down
4 changes: 2 additions & 2 deletions internal/args/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func marshal(src reflect.Value, keys []string) (args []string, err error) {

// We loop through all items and marshal them with key = key.0, key.1, ....
args := []string(nil)
for i := 0; i < src.Len(); i++ {
for i := range src.Len() {
subArgs, err := marshal(src.Index(i), append(keys, strconv.Itoa(i)))
if err != nil {
return nil, err
Expand Down Expand Up @@ -178,7 +178,7 @@ func marshal(src reflect.Value, keys []string) (args []string, err error) {
// If type is a struct
// We loop through all struct field
args := []string(nil)
for i := 0; i < src.NumField(); i++ {
for i := range src.NumField() {
fieldValue := src.Field(i)
key := strcase.ToBashArg(src.Type().Field(i).Name)
newArgs, err := marshal(fieldValue, append(keys, key))
Expand Down
2 changes: 1 addition & 1 deletion internal/args/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func set(dest reflect.Value, argNameWords []string, value string) error {
// We construct two caches:
anonymousFieldIndexes := []int(nil)
fieldIndexByName := map[string]int{}
for i := 0; i < dest.Type().NumField(); i++ {
for i := range dest.Type().NumField() {
field := dest.Type().Field(i)
if field.Anonymous {
anonymousFieldIndexes = append(anonymousFieldIndexes, i)
Expand Down
2 changes: 1 addition & 1 deletion internal/core/autocomplete_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func AutocompleteGetArg(ctx context.Context, cmd *Command, argSpec *ArgSpec, com
}
values := []string(nil)
// Let's iterate over the struct in the response slice and get the searched field
for i := 0; i < resources.Len(); i++ {
for i := range resources.Len() {
resource := resources.Index(i)
if resource.Kind() == reflect.Ptr {
resource = resource.Elem()
Expand Down
2 changes: 1 addition & 1 deletion internal/core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (p *Printer) printTemplate(data interface{}) error {
switch dataValue.Type().Kind() {
// If we have a slice of value, we apply the template for each item
case reflect.Slice:
for i := 0; i < dataValue.Len(); i++ {
for i := range dataValue.Len() {
elemValue := dataValue.Index(i)
err := p.template.Execute(writer, elemValue)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/core/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
// becomes struct{FieldName string `json:"field_name"`}
func newObjectWithForcedJSONTags(t reflect.Type) interface{} {
structFieldsCopy := []reflect.StructField(nil)
for i := 0; i < t.NumField(); i++ {
for i := range t.NumField() {
fieldCopy := t.Field(i)
if fieldCopy.Anonymous {
anonymousType := fieldCopy.Type
if anonymousType.Kind() == reflect.Ptr {
anonymousType = anonymousType.Elem()
}
for i := 0; i < anonymousType.NumField(); i++ {
for i := range anonymousType.NumField() {
fieldCopy := anonymousType.Field(i)
fieldCopy.Tag = reflect.StructTag(`json:"` + strings.ReplaceAll(strcase.ToBashArg(fieldCopy.Name), "-", "_") + `"`)
structFieldsCopy = append(structFieldsCopy, fieldCopy)
Expand All @@ -49,7 +49,7 @@ func GetValuesForFieldByName(value reflect.Value, parts []string) (values []refl
values := []reflect.Value(nil)
errs := []error(nil)

for i := 0; i < value.Len(); i++ {
for i := range value.Len() {
newValues, err := GetValuesForFieldByName(value.Index(i), parts[1:])
if err != nil {
errs = append(errs, err)
Expand Down Expand Up @@ -90,7 +90,7 @@ func GetValuesForFieldByName(value reflect.Value, parts []string) (values []refl
anonymousFieldIndexes := []int(nil)
fieldIndexByName := map[string]int{}

for i := 0; i < value.NumField(); i++ {
for i := range value.NumField() {
field := value.Type().Field(i)
if field.Anonymous {
anonymousFieldIndexes = append(anonymousFieldIndexes, i)
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/sdk_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestSdkStandardErrors(t *testing.T) {
UseE2EClient: true,
DisableParallel: true, // because e2e client is used
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
for i := 0; i < 10; i++ {
for range 10 {
ctx.ExecuteCmd([]string{"scw", "test", "human", "create"})
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/editor/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func hasTag(tags []string, actualTag string) bool {
func valueMapperWithoutOpt(dest reflect.Value, src reflect.Value, includeFields []string, excludeFields []string) {
switch dest.Kind() {
case reflect.Struct:
for i := 0; i < dest.NumField(); i++ {
for i := range dest.NumField() {
destField := dest.Field(i)
fieldType := dest.Type().Field(i)
srcField := src.FieldByName(fieldType.Name)
Expand Down Expand Up @@ -82,7 +82,7 @@ func valueMapperWithoutOpt(dest reflect.Value, src reflect.Value, includeFields
// If destination is a slice, allocate the slice and map each value
srcLen := src.Len()
dest.Set(reflect.MakeSlice(dest.Type(), srcLen, srcLen))
for i := 0; i < srcLen; i++ {
for i := range srcLen {
valueMapperWithoutOpt(dest.Index(i), src.Index(i), includeFields, excludeFields)
}
default:
Expand Down Expand Up @@ -140,7 +140,7 @@ func DeleteRecursive(elem interface{}, keys ...string) {
case reflect.Map:
deleteRecursiveMap(elem.(map[string]interface{}), keys...)
case reflect.Slice:
for i := 0; i < value.Len(); i++ {
for i := range value.Len() {
DeleteRecursive(value.Index(i).Interface(), keys...)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/gofields/gofields.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func listFields(t reflect.Type, parents []string, filter ListFieldFilter) []stri
return listFields(t.Elem(), append(parents, "<key>"), filter)
case reflect.Struct:
res := []string(nil)
for i := 0; i < t.NumField(); i++ {
for i := range t.NumField() {
field := t.Field(i)

if !isFieldPublic(field) {
Expand Down
10 changes: 5 additions & 5 deletions internal/human/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func marshalStruct(value reflect.Value, opt *MarshalOpt) (string, error) {
// If type is a slice:
// We loop through all items and marshal them with key = key.0, key.1, ....
data := [][]string(nil)
for i := 0; i < value.Len(); i++ {
for i := range value.Len() {
subData, err := marshal(value.Index(i), append(keys, strconv.Itoa(i)))
if err != nil {
return nil, err
Expand Down Expand Up @@ -233,7 +233,7 @@ func GetStructFieldsIndex(v reflect.Type) [][]int {
v = v.Elem()
}

for i := 0; i < v.NumField(); i++ {
for i := range v.NumField() {
field := v.Field(i)
// If a field is anonymous we start recursive call
if field.Anonymous {
Expand Down Expand Up @@ -307,7 +307,7 @@ func marshalSlice(slice reflect.Value, opt *MarshalOpt) (string, error) {
grid = append(grid, headerRow)

// For each item in the slice
for i := 0; i < slice.Len(); i++ {
for i := range slice.Len() {
item := slice.Index(i)
row := []string(nil)
for _, fieldSpec := range opt.Fields {
Expand Down Expand Up @@ -422,7 +422,7 @@ func computeMaxCols(grid [][]string) int {
return maxCols
}
colMaxSize := make([]int, len(grid[0]))
for i := 0; i < len(grid); i++ {
for i := range len(grid) {
lineSize := 0
for j := 0; j < maxCols; j++ {
size := len(grid[i][j]) + colPadding
Expand All @@ -443,7 +443,7 @@ func computeMaxCols(grid [][]string) int {
func getDefaultFieldsOpt(t reflect.Type) []*MarshalFieldOpt {
results := []*MarshalFieldOpt(nil)
// Loop through all struct field
for fieldIdx := 0; fieldIdx < t.NumField(); fieldIdx++ {
for fieldIdx := range t.NumField() {
field := t.Field(fieldIdx)
fieldType := field.Type

Expand Down
2 changes: 1 addition & 1 deletion internal/interactive/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func RemoveIndent(str string) string {

func makeStr(char string, length int) string {
str := ""
for i := 0; i < length; i++ {
for range length {
str += char
}
return str
Expand Down
4 changes: 2 additions & 2 deletions internal/namespaces/config/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ The only allowed attributes are access_key, secret_key, default_organization_id,

argValue := reflect.ValueOf(args).Elem()
profileValue := reflect.ValueOf(profile).Elem()
for i := 0; i < argValue.NumField(); i++ {
for i := range argValue.NumField() {
field := argValue.Field(i)
if !field.IsNil() {
profileValue.Field(i).Set(field)
Expand Down Expand Up @@ -772,7 +772,7 @@ func getProfileField(profile *scw.Profile, key string) (reflect.Value, error) {
func getProfileKeys() []string {
t := reflect.TypeOf(scw.Profile{})
keys := []string{}
for i := 0; i < t.NumField(); i++ {
for i := range t.NumField() {
field := t.Field(i)
switch field.Name {
case "APIURL":
Expand Down
10 changes: 5 additions & 5 deletions internal/passwordgenerator/password_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ func GeneratePassword(length, minNumbers, minLower, minUpper, minSymbol int) (st

var password strings.Builder

for i := 0; i < minNumbers; i++ {
for range minNumbers {
random, err := randInt(len(numbers))
if err != nil {
return "", err
}
password.WriteString(string(numbers[random]))
}

for i := 0; i < minUpper; i++ {
for range minUpper {
random, err := randInt(len(upperLetters))
if err != nil {
return "", err
}
password.WriteString(string(upperLetters[random]))
}

for i := 0; i < minLower; i++ {
for range minLower {
random, err := randInt(len(lowerLetters))
if err != nil {
return "", err
}
password.WriteString(string(lowerLetters[random]))
}

for i := 0; i < minSymbol; i++ {
for range minSymbol {
random, err := randInt(len(SpecialSymbols))
if err != nil {
return "", err
Expand All @@ -55,7 +55,7 @@ func GeneratePassword(length, minNumbers, minLower, minUpper, minSymbol int) (st
}

remainingLength := length - minNumbers - minLower - minUpper - minSymbol
for i := 0; i < remainingLength; i++ {
for range remainingLength {
random, err := randInt(len(allSet))
if err != nil {
return "", err
Expand Down
12 changes: 6 additions & 6 deletions internal/tabwriter/tabwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (b *buffer) Write(buf []byte) (written int, err error) {
m := len(buf)
if n+m <= cap(b.a) {
b.a = b.a[0 : n+m]
for i := 0; i < m; i++ {
for i := range m {
b.a[n+i] = buf[i]
}
} else {
Expand Down Expand Up @@ -75,7 +75,7 @@ func check(t *testing.T, testname string, minwidth, tabwidth, padding int, padch
// write byte-by-byte
title = testname + " (written byte-by-byte)"
b.clear()
for i := 0; i < len(src); i++ {
for i := range len(src) {
write(t, title, &w, src[i:i+1])
}
verify(t, title, &w, &b, src, expected)
Expand Down Expand Up @@ -698,7 +698,7 @@ func BenchmarkTable(b *testing.B) {
for i := 0; i < b.N; i++ {
w := tabwriter.NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
// Write the line h times.
for j := 0; j < h; j++ {
for range h {
w.Write(line)
}
w.Flush()
Expand All @@ -710,7 +710,7 @@ func BenchmarkTable(b *testing.B) {
w := tabwriter.NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
for i := 0; i < b.N; i++ {
// Write the line h times.
for j := 0; j < h; j++ {
for range h {
w.Write(line)
}
w.Flush()
Expand All @@ -730,7 +730,7 @@ func BenchmarkPyramid(b *testing.B) {
for i := 0; i < b.N; i++ {
w := tabwriter.NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
// Write increasing prefixes of that line.
for j := 0; j < x; j++ {
for j := range x {
w.Write(line[:j*2])
w.Write([]byte{'\n'})
}
Expand All @@ -752,7 +752,7 @@ func BenchmarkRagged(b *testing.B) {
for i := 0; i < b.N; i++ {
w := tabwriter.NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
// Write the lines in turn h times.
for j := 0; j < h; j++ {
for j := range h {
w.Write(lines[j%len(lines)])
w.Write([]byte{'\n'})
}
Expand Down

0 comments on commit 914e1e5

Please sign in to comment.