Skip to content

Commit 4267c04

Browse files
committed
move to range int loops
1 parent 0e3d5bc commit 4267c04

File tree

15 files changed

+44
-53
lines changed

15 files changed

+44
-53
lines changed

console_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func BenchmarkConsolePrintw(b *testing.B) {
117117
w := NewConsoleWriter(io.Discard, LdetFlags)
118118
l := New(w)
119119

120-
for i := 0; i < b.N; i++ {
120+
for i := range b.N {
121121
l.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
122122
}
123123
}
@@ -128,7 +128,7 @@ func BenchmarkConsoleStartPrintwFinish(b *testing.B) {
128128
w := NewConsoleWriter(io.Discard, LdetFlags)
129129
l := New(w)
130130

131-
for i := 0; i < b.N; i++ {
131+
for i := range b.N {
132132
tr := l.Start("span_name")
133133
tr.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
134134
tr.Finish()

convert/json_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@ func TestJSONLogger(t *testing.T) {
6161

6262
exps := strings.Split(exp, "\n")
6363
ls := strings.Split(string(b), "\n")
64-
for i := 0; i < len(exps); i++ {
65-
re := regexp.MustCompile("^" + exps[i] + "$")
64+
65+
for i, e := range exps {
66+
re := regexp.MustCompile("^" + e + "$")
6667

6768
var have string
6869
if i < len(ls) {
6970
have = ls[i]
7071
}
7172

72-
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", exps[i], have)
73+
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", e, have)
7374
}
7475

7576
for i := len(exps); i < len(ls); i++ {
@@ -112,15 +113,16 @@ func TestJSONRename(t *testing.T) {
112113

113114
exps := strings.Split(exp, "\n")
114115
ls := strings.Split(string(b), "\n")
115-
for i := 0; i < len(exps); i++ {
116-
re := regexp.MustCompile("^" + exps[i] + "$")
116+
117+
for i, e := range exps {
118+
re := regexp.MustCompile("^" + e + "$")
117119

118120
var have string
119121
if i < len(ls) {
120122
have = ls[i]
121123
}
122124

123-
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", exps[i], have)
125+
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", e, have)
124126
}
125127

126128
for i := len(exps); i < len(ls); i++ {
@@ -158,7 +160,7 @@ func BenchmarkJSONConvert(b *testing.B) {
158160
b.ResetTimer()
159161
b.ReportAllocs()
160162

161-
for i := 0; i < b.N; i++ {
163+
for range b.N {
162164
_, _ = w.Write(buf[st:])
163165
}
164166
}
@@ -174,7 +176,7 @@ func BenchmarkJSONPrintw(b *testing.B) {
174176
b.ResetTimer()
175177
b.ReportAllocs()
176178

177-
for i := 0; i < b.N; i++ {
179+
for i := range b.N {
178180
l.Printw("message", "a", i+1000, "b", i+1000)
179181
}
180182
}

convert/logfmt_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,16 @@ func testLogfmtObj(t *testing.T, flat bool) {
9393

9494
exps := strings.Split(exp, "\n")
9595
ls := strings.Split(string(b), "\n")
96-
for i := 0; i < len(exps); i++ {
97-
re := regexp.MustCompile("^" + exps[i] + "$")
96+
97+
for i, e := range exps {
98+
re := regexp.MustCompile("^" + e + "$")
9899

99100
var have string
100101
if i < len(ls) {
101102
have = ls[i]
102103
}
103104

104-
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", exps[i], have)
105+
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", e, have)
105106
}
106107

107108
for i := len(exps); i < len(ls); i++ {
@@ -144,15 +145,16 @@ func TestLogfmtRename(t *testing.T) {
144145

145146
exps := strings.Split(exp, "\n")
146147
ls := strings.Split(string(b), "\n")
147-
for i := 0; i < len(exps); i++ {
148-
re := regexp.MustCompile("^" + exps[i] + "$")
148+
149+
for i, e := range exps {
150+
re := regexp.MustCompile("^" + e + "$")
149151

150152
var have string
151153
if i < len(ls) {
152154
have = ls[i]
153155
}
154156

155-
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", exps[i], have)
157+
assert.True(t, re.MatchString(have), "expected\n%s\ngot\n%s", e, have)
156158
}
157159

158160
for i := len(exps); i < len(ls); i++ {

convert/rewriter.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (w *KeyRenamer) cmp(x, y []tlog.RawMessage) (r int) {
237237
// defer func() {
238238
// fmt.Printf("cmp %q %q -> %d from %v\n", x, y, r, loc.Caller(1))
239239
// }()
240-
for i := 0; i < min(len(x), len(y)); i++ {
240+
for i := range min(len(x), len(y)) {
241241
r = bytes.Compare(x[i], y[i])
242242
if r != 0 {
243243
return r
@@ -254,11 +254,3 @@ func (w *KeyRenamer) cmp(x, y []tlog.RawMessage) (r int) {
254254

255255
return 0
256256
}
257-
258-
func min(a, b int) int {
259-
if a < b {
260-
return a
261-
}
262-
263-
return b
264-
}

ext/tlslog/slog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (l *Handler) Handle(ctx context.Context, r slog.Record) error { //nolint:go
6565

6666
r.Attrs(l.attr)
6767

68-
for i := 0; i < l.depth; i++ {
68+
for range l.depth {
6969
l.b = l.AppendBreak(l.b)
7070
}
7171

id.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func IDFromBytes(b []byte) (id ID, err error) {
6363
func IDFromString(s string) (id ID, err error) {
6464
var j int
6565

66-
for i := 0; i < len(s); i++ {
66+
for i := range len(s) {
6767
var c byte
6868

6969
switch {
@@ -246,8 +246,8 @@ func (id *ID) UnmarshalJSON(b []byte) error {
246246
}
247247

248248
func MathRandID() (id ID) {
249-
lo := rand.Uint64()
250-
hi := rand.Uint64()
249+
lo := rand.Uint64() //nolint:gosec
250+
hi := rand.Uint64() //nolint:gosec
251251

252252
binary.BigEndian.PutUint64(id[:8], hi)
253253
binary.BigEndian.PutUint64(id[8:], lo)

id_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func BenchmarkIDStringUUID(b *testing.B) {
6262

6363
id := ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
6464

65-
for i := 0; i < b.N; i++ {
65+
for range b.N {
6666
_ = id.StringUUID()
6767
}
6868
}
@@ -72,7 +72,7 @@ func BenchmarkIDFormat(b *testing.B) {
7272

7373
id := ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
7474

75-
for i := 0; i < b.N; i++ {
75+
for range b.N {
7676
fmt.Fprintf(io.Discard, "%+x", id)
7777
}
7878
}
@@ -83,7 +83,7 @@ func BenchmarkIDFormatTo(b *testing.B) {
8383
var buf [40]byte
8484
id := ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
8585

86-
for i := 0; i < b.N; i++ {
86+
for i := range b.N {
8787
if i&0xf == 0 {
8888
ID{}.FormatTo(buf[:], 0, 'v')
8989
} else {

low/runtime.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ type (
77
t unsafe.Pointer
88
p unsafe.Pointer
99
}
10-
11-
sh struct {
12-
p unsafe.Pointer
13-
l int
14-
}
1510
)
1611

1712
func UnpackInterface(v interface{}) (t, p unsafe.Pointer) {

tlio/writers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func BenchmarkReWriter(b *testing.B) {
179179
"c", 4,
180180
"d", "")
181181

182-
for i := 0; i < b.N; i++ {
182+
for i := range b.N {
183183
l.Printw("message", "a", i+1000, "b", i+1001)
184184
}
185185
}

tlog_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestLoggerSmokeConcurrent(t *testing.T) {
4646
go func() {
4747
defer wg.Done()
4848

49-
for i := 0; i < N; i++ {
49+
for i := range N {
5050
l.Printf("printf %v %v", i+1000, i+1001)
5151
}
5252
}()
@@ -55,7 +55,7 @@ func TestLoggerSmokeConcurrent(t *testing.T) {
5555
go func() {
5656
defer wg.Done()
5757

58-
for i := 0; i < N; i++ {
58+
for i := range N {
5959
l.Printw("printw", "i0", i+1000, "i1", i+1001)
6060
}
6161
}()
@@ -64,7 +64,7 @@ func TestLoggerSmokeConcurrent(t *testing.T) {
6464
go func() {
6565
defer wg.Done()
6666

67-
for i := 0; i < N; i++ {
67+
for i := range N {
6868
tr := l.Start("span")
6969
tr.Printw("span.printw", "i0", i+1000, "i1", i+1001)
7070
tr.Finish()
@@ -75,7 +75,7 @@ func TestLoggerSmokeConcurrent(t *testing.T) {
7575
go func() {
7676
defer wg.Done()
7777

78-
for i := 0; i < N; i++ {
78+
for i := range N {
7979
tr := l.Start("span_observer")
8080
_ = tr.Event("value", i+1000)
8181
tr.Finish()
@@ -188,7 +188,7 @@ func BenchmarkLoggerPrintw(b *testing.B) {
188188

189189
l := New(io.Discard)
190190

191-
for i := 0; i < b.N; i++ {
191+
for i := range b.N {
192192
l.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
193193
}
194194
}
@@ -198,7 +198,7 @@ func BenchmarkLoggerPrintf(b *testing.B) {
198198

199199
l := New(io.Discard)
200200

201-
for i := 0; i < b.N; i++ {
201+
for i := range b.N {
202202
l.Printf("message a %v b %v c %v", i+1000, i+1000, "str")
203203
}
204204
}
@@ -208,7 +208,7 @@ func BenchmarkLoggerStartPrintwFinish(b *testing.B) {
208208

209209
l := New(io.Discard)
210210

211-
for i := 0; i < b.N; i++ {
211+
for i := range b.N {
212212
tr := l.Start("span_name")
213213
tr.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
214214
tr.Finish()

0 commit comments

Comments
 (0)