Skip to content

Commit a5d4feb

Browse files
fix: for loop can be modernized using range over int
1 parent c3d1092 commit a5d4feb

File tree

11 files changed

+19
-19
lines changed

11 files changed

+19
-19
lines changed

benchmarks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func runRequest(B *testing.B, r *Engine, method, path string) {
154154
w := newMockWriter()
155155
B.ReportAllocs()
156156
B.ResetTimer()
157-
for i := 0; i < B.N; i++ {
157+
for B.Loop() {
158158
r.ServeHTTP(w, req)
159159
}
160160
}

binding/default_validator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (err SliceValidationError) Error() string {
2727
}
2828

2929
var b strings.Builder
30-
for i := 0; i < len(err); i++ {
30+
for i := range len(err) {
3131
if err[i] != nil {
3232
if b.Len() > 0 {
3333
b.WriteString("\n")
@@ -58,7 +58,7 @@ func (v *defaultValidator) ValidateStruct(obj any) error {
5858
case reflect.Slice, reflect.Array:
5959
count := value.Len()
6060
validateRet := make(SliceValidationError, 0)
61-
for i := 0; i < count; i++ {
61+
for i := range count {
6262
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
6363
validateRet = append(validateRet, err)
6464
}

binding/default_validator_benchmark_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func BenchmarkSliceValidationError(b *testing.B) {
2020
b.ReportAllocs()
2121
b.ResetTimer()
2222

23-
for i := 0; i < b.N; i++ {
23+
for b.Loop() {
2424
if len(e.Error()) == 0 {
2525
b.Errorf("error")
2626
}

binding/form_mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func mapping(value reflect.Value, field reflect.StructField, setter setter, tag
118118
tValue := value.Type()
119119

120120
var isSet bool
121-
for i := 0; i < value.NumField(); i++ {
121+
for i := range value.NumField() {
122122
sf := tValue.Field(i)
123123
if sf.PkgPath != "" && !sf.Anonymous { // unexported
124124
continue

binding/form_mapping_benchmark_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type structFull struct {
3131

3232
func BenchmarkMapFormFull(b *testing.B) {
3333
var s structFull
34-
for i := 0; i < b.N; i++ {
34+
for b.Loop() {
3535
err := mapForm(&s, form)
3636
if err != nil {
3737
b.Fatalf("Error on a form mapping")
@@ -54,7 +54,7 @@ type structName struct {
5454

5555
func BenchmarkMapFormName(b *testing.B) {
5656
var s structName
57-
for i := 0; i < b.N; i++ {
57+
for b.Loop() {
5858
err := mapForm(&s, form)
5959
if err != nil {
6060
b.Fatalf("Error on a form mapping")

context_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,22 +3581,22 @@ func BenchmarkGetMapFromFormData(b *testing.B) {
35813581

35823582
// Test case 3: Large dataset with many bracket keys
35833583
largeData := make(map[string][]string)
3584-
for i := 0; i < 100; i++ {
3584+
for i := range 100 {
35853585
key := fmt.Sprintf("ids[%d]", i)
35863586
largeData[key] = []string{fmt.Sprintf("value%d", i)}
35873587
}
3588-
for i := 0; i < 50; i++ {
3588+
for i := range 50 {
35893589
key := fmt.Sprintf("names[%d]", i)
35903590
largeData[key] = []string{fmt.Sprintf("name%d", i)}
35913591
}
3592-
for i := 0; i < 25; i++ {
3592+
for i := range 25 {
35933593
key := fmt.Sprintf("other[key%d]", i)
35943594
largeData[key] = []string{fmt.Sprintf("other%d", i)}
35953595
}
35963596

35973597
// Test case 4: Dataset with many non-matching keys (worst case)
35983598
worstCaseData := make(map[string][]string)
3599-
for i := 0; i < 100; i++ {
3599+
for i := range 100 {
36003600
key := fmt.Sprintf("nonmatching%d", i)
36013601
worstCaseData[key] = []string{fmt.Sprintf("value%d", i)}
36023602
}
@@ -3632,7 +3632,7 @@ func BenchmarkGetMapFromFormData(b *testing.B) {
36323632
for _, bm := range benchmarks {
36333633
b.Run(bm.name, func(b *testing.B) {
36343634
b.ReportAllocs()
3635-
for i := 0; i < b.N; i++ {
3635+
for b.Loop() {
36363636
_, _ = getMapFromFormData(bm.data, bm.key)
36373637
}
36383638
})

gin_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func TestConcurrentHandleContext(t *testing.T) {
393393
var wg sync.WaitGroup
394394
iterations := 200
395395
wg.Add(iterations)
396-
for i := 0; i < iterations; i++ {
396+
for range iterations {
397397
go func() {
398398
req, err := http.NewRequest(http.MethodGet, "/", nil)
399399
assert.NoError(t, err)

internal/bytesconv/bytesconv_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func rawStrToBytes(s string) []byte {
3030

3131
func TestBytesToString(t *testing.T) {
3232
data := make([]byte, 1024)
33-
for i := 0; i < 100; i++ {
33+
for range 100 {
3434
_, err := cRand.Read(data)
3535
if err != nil {
3636
t.Fatal(err)
@@ -70,7 +70,7 @@ func RandStringBytesMaskImprSrcSB(n int) string {
7070
}
7171

7272
func TestStringToBytes(t *testing.T) {
73-
for i := 0; i < 100; i++ {
73+
for range 100 {
7474
s := RandStringBytesMaskImprSrcSB(64)
7575
if !bytes.Equal(rawStrToBytes(s), StringToBytes(s)) {
7676
t.Fatal("don't match")

path_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestPathCleanMallocs(t *testing.T) {
9494
func BenchmarkPathClean(b *testing.B) {
9595
b.ReportAllocs()
9696

97-
for i := 0; i < b.N; i++ {
97+
for b.Loop() {
9898
for _, test := range cleanTests {
9999
cleanPath(test.path)
100100
}
@@ -137,7 +137,7 @@ func BenchmarkPathCleanLong(b *testing.B) {
137137
b.ResetTimer()
138138
b.ReportAllocs()
139139

140-
for i := 0; i < b.N; i++ {
140+
for b.Loop() {
141141
for _, test := range cleanTests {
142142
cleanPath(test.path)
143143
}

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func resolveAddress(addr []string) string {
155155

156156
// https://stackoverflow.com/questions/53069040/checking-a-string-contains-only-ascii-characters
157157
func isASCII(s string) bool {
158-
for i := 0; i < len(s); i++ {
158+
for i := range len(s) {
159159
if s[i] > unicode.MaxASCII {
160160
return false
161161
}

0 commit comments

Comments
 (0)