Skip to content

Commit b942136

Browse files
authored
Use Go 1.22+ int ranges (open-policy-agent#7328)
With "some" help from `golangci-lint run --fix ./...` Signed-off-by: Anders Eknert <[email protected]>
1 parent 616aac4 commit b942136

File tree

118 files changed

+412
-407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+412
-407
lines changed

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ linters:
151151
- errcheck
152152
- govet
153153
- ineffassign
154+
- intrange
154155
- revive # replacement for golint
155156
- gofmt
156157
- goimports

cmd/bench.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func benchMain(args []string, params benchmarkCommandParams, w io.Writer, r benc
214214
}
215215

216216
// Run the benchmark as many times as specified, re-use the prepared objects for each
217-
for i := 0; i < params.count; i++ {
217+
for range params.count {
218218
br, err := r.run(ctx, ectx, params, benchFunc)
219219
if err != nil {
220220
errRender := renderBenchmarkError(params, err, w)
@@ -252,7 +252,7 @@ func (r *goBenchRunner) run(ctx context.Context, ectx *evalContext, params bench
252252
hist.Clear()
253253

254254
b.ResetTimer()
255-
for i := 0; i < b.N; i++ {
255+
for range b.N {
256256

257257
// Start the timer (might already be started, but that's ok)
258258
b.StartTimer()
@@ -350,7 +350,7 @@ func benchE2E(ctx context.Context, args []string, params benchmarkCommandParams,
350350
baseDelay := time.Duration(100) * time.Millisecond
351351
maxDelay := time.Duration(60) * time.Second
352352
retries := 3 // Max of around 1 minute total wait time.
353-
for i := 0; i < retries; i++ {
353+
for i := range retries {
354354
if len(rt.Addrs()) == 0 {
355355
delay := util.DefaultBackoff(float64(baseDelay), float64(maxDelay), i)
356356
time.Sleep(delay)
@@ -415,7 +415,7 @@ func benchE2E(ctx context.Context, args []string, params benchmarkCommandParams,
415415
url += "?metrics=true"
416416
}
417417

418-
for i := 0; i < params.count; i++ {
418+
for range params.count {
419419
br, err := runE2E(params, url, body)
420420
if err != nil {
421421
return err
@@ -441,7 +441,7 @@ func runE2E(params benchmarkCommandParams, url string, input map[string]interfac
441441
hist.Clear()
442442

443443
b.ResetTimer()
444-
for i := 0; i < b.N; i++ {
444+
for range b.N {
445445

446446
// Start the timer
447447
b.StartTimer()

cmd/eval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func eval(args []string, params evalCommandParams, w io.Writer) (bool, error) {
394394
profiles := make([][]profiler.ExprStats, ectx.params.count)
395395
timers := make([]map[string]interface{}, ectx.params.count)
396396

397-
for i := 0; i < ectx.params.count; i++ {
397+
for i := range ectx.params.count {
398398
results[i] = evalOnce(ctx, ectx)
399399
profiles[i] = results[i].Profile
400400
if ts, ok := results[i].Metrics.(metrics.TimerMetrics); ok {

cmd/fmt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ func TestFmtMultipleWrongArityError(t *testing.T) {
727727
"plus",
728728
}
729729
expectedErrs := ast.Errors(make([]*ast.Error, 3))
730-
for i := 0; i < 3; i++ {
730+
for i := range 3 {
731731
loc := locations[i]
732732
errExp := ast.NewError(ast.TypeErr, &loc, "%s: %s", operators[i], "arity mismatch")
733733
errExp.Details = &format.ArityFormatErrDetail{

cmd/parse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ p = 1
247247
min = len(wantLines)
248248
}
249249

250-
for i := 0; i < min; i++ {
250+
for i := range min {
251251
if gotLines[i] != wantLines[i] {
252252
t.Fatalf("Expected line %d to be\n%v\n, got\n%v", i, wantLines[i], gotLines[i])
253253
}
@@ -473,7 +473,7 @@ a.b.c := true
473473
min = len(wantLines)
474474
}
475475

476-
for i := 0; i < min; i++ {
476+
for i := range min {
477477
if gotLines[i] != wantLines[i] {
478478
t.Fatalf("Expected line %d to be\n%v\n, got\n%v", i, wantLines[i], gotLines[i])
479479
}
@@ -934,7 +934,7 @@ allow = true if {
934934
min = len(wantLines)
935935
}
936936

937-
for i := 0; i < min; i++ {
937+
for i := range min {
938938
if gotLines[i] != wantLines[i] {
939939
t.Fatalf("Expected line %d to be\n%v\n, got\n%v", i, wantLines[i], gotLines[i])
940940
}

cmd/test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func opaTest(args []string, testParams testCommandParams) (int, error) {
148148
}
149149

150150
success := true
151-
for i := 0; i < testParams.count; i++ {
151+
for range testParams.count {
152152
exitCode, err := runTests(ctx, txn, runner, reporter, testParams)
153153
if exitCode != 0 {
154154
success = false
@@ -322,7 +322,7 @@ func processWatcherUpdate(ctx context.Context, testParams testCommandParams, pat
322322
return err
323323
}
324324

325-
for i := 0; i < testParams.count; i++ {
325+
for range testParams.count {
326326
exitCode, err := runTests(ctx, txn, runner, reporter, testParams)
327327
if exitCode != 0 {
328328
return err

internal/compiler/wasm/wasm.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _
13601360
// Initialize the locals that specify the path of the upsert operation.
13611361
lpath := make(map[int]uint32, len(path))
13621362

1363-
for i := 0; i < len(path); i++ {
1363+
for i := range path {
13641364
lpath[i] = c.genLocal()
13651365
instrs = append(instrs, instruction.I32Const{Value: c.opaStringAddr(path[i])})
13661366
instrs = append(instrs, instruction.SetLocal{Index: lpath[i]})
@@ -1369,10 +1369,10 @@ func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _
13691369
// Generate a block that traverses the path of the upsert operation,
13701370
// shallowing copying values at each step as needed. Stop before the final
13711371
// segment that will only be inserted.
1372-
var inner []instruction.Instruction
1372+
inner := make([]instruction.Instruction, 0, len(path)*21+1)
13731373
ltemp := c.genLocal()
13741374

1375-
for i := 0; i < len(path)-1; i++ {
1375+
for i := range len(path) - 1 {
13761376

13771377
// Lookup the next part of the path.
13781378
inner = append(inner, instruction.GetLocal{Index: lcopy})
@@ -1408,10 +1408,10 @@ func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _
14081408
inner = append(inner, instruction.Br{Index: uint32(len(path) - 1)})
14091409

14101410
// Generate blocks that handle missing nodes during traversal.
1411-
var block []instruction.Instruction
1411+
block := make([]instruction.Instruction, 0, len(path)*10)
14121412
lval := c.genLocal()
14131413

1414-
for i := 0; i < len(path)-1; i++ {
1414+
for i := range len(path) - 1 {
14151415
block = append(block, instruction.Block{Instrs: inner})
14161416
block = append(block, instruction.Call{Index: c.function(opaObject)})
14171417
block = append(block, instruction.SetLocal{Index: lval})

internal/edittree/bitvector/bitvector_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (s *BitVectorSuite) SetUpTest(_ *check.C) {
2323
}
2424

2525
func (s *BitVectorSuite) TestElement(c *check.C) {
26-
for i := 0; i < 4; i++ {
26+
for i := range 4 {
2727
c.Assert(s.vector.Element(i), check.Equals, byte(0))
2828
}
2929
for i := 4; i < 12; i++ {
@@ -32,14 +32,14 @@ func (s *BitVectorSuite) TestElement(c *check.C) {
3232
}
3333

3434
func (s *BitVectorSuite) TestInsert(c *check.C) {
35-
for i := 0; i < 4; i++ {
35+
for range 4 {
3636
s.vector.Insert(0, 8)
3737
}
3838
c.Assert(s.vector.Bytes(), check.DeepEquals, []byte{0xF0, 0xF0})
3939
}
4040

4141
func (s *BitVectorSuite) TestAppend(c *check.C) {
42-
for i := 0; i < 4; i++ {
42+
for i := range 4 {
4343
if i%2 == 0 {
4444
s.vector.Append(0)
4545
} else {
@@ -68,14 +68,14 @@ func (s *BitVectorSuite) TestSetOneBit(c *check.C) {
6868
}
6969

7070
func (s *BitVectorSuite) TestDelete(c *check.C) {
71-
for i := 0; i < 4; i++ {
71+
for range 4 {
7272
s.vector.Delete(8)
7373
}
7474
c.Assert(s.vector.Bytes(), check.DeepEquals, []byte{0xF0})
7575
}
7676

7777
func (s *BitVectorSuite) TestDeleteFirstIndex(c *check.C) {
78-
for i := 0; i < 4; i++ {
78+
for range 4 {
7979
s.vector.Delete(0)
8080
}
8181
c.Assert(s.vector.Bytes(), check.DeepEquals, []byte{0xFF})

internal/edittree/edittree.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,14 @@ func (e *EditTree) Delete(key *ast.Term) (*EditTree, error) {
552552
}
553553
// Do rewrites to clear out the newly-removed element.
554554
e.deleteChildValue(idx)
555-
for i := 0; i < len(rewritesScalars); i++ {
555+
for i := range rewritesScalars {
556556
originalIdx := rewritesScalars[i]
557557
rewriteIdx := rewritesScalars[i] - 1
558558
v := e.childScalarValues[originalIdx]
559559
e.deleteChildValue(originalIdx)
560560
e.setChildScalarValue(rewriteIdx, v)
561561
}
562-
for i := 0; i < len(rewritesComposites); i++ {
562+
for i := range rewritesComposites {
563563
originalIdx := rewritesComposites[i]
564564
rewriteIdx := rewritesComposites[i] - 1
565565
v := e.childCompositeValues[originalIdx]
@@ -591,7 +591,7 @@ func (e *EditTree) Delete(key *ast.Term) (*EditTree, error) {
591591
//gcassert:inline
592592
func sumZeroesBelowIndex(index int, bv *bitvector.BitVector) int {
593593
zeroesSeen := 0
594-
for i := 0; i < index; i++ {
594+
for i := range index {
595595
if bv.Element(i) == 0 {
596596
zeroesSeen++
597597
}
@@ -601,7 +601,7 @@ func sumZeroesBelowIndex(index int, bv *bitvector.BitVector) int {
601601

602602
func findIndexOfNthZero(n int, bv *bitvector.BitVector) (int, bool) {
603603
zeroesSeen := 0
604-
for i := 0; i < bv.Length(); i++ {
604+
for i := range bv.Length() {
605605
if bv.Element(i) == 0 {
606606
zeroesSeen++
607607
}
@@ -831,7 +831,7 @@ func (e *EditTree) Render() *ast.Term {
831831
// original array. We build a new Array with modified/deleted keys.
832832
out := make([]*ast.Term, 0, e.insertions.Length())
833833
eIdx := 0
834-
for i := 0; i < e.insertions.Length(); i++ {
834+
for i := range e.insertions.Length() {
835835
// If the index == 0, that indicates we should look up the next
836836
// surviving original element.
837837
// If the index == 1, that indicates we should look up that

internal/gqlparser/ast/dumper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func typeName(t reflect.Type) string {
8888
func (d *dumper) dumpArray(v reflect.Value) {
8989
d.WriteString("[" + typeName(v.Type().Elem()) + "]")
9090

91-
for i := 0; i < v.Len(); i++ {
91+
for i := range v.Len() {
9292
d.nl()
9393
d.WriteString("- ")
9494
d.indent++
@@ -102,7 +102,7 @@ func (d *dumper) dumpStruct(v reflect.Value) {
102102
d.indent++
103103

104104
typ := v.Type()
105-
for i := 0; i < v.NumField(); i++ {
105+
for i := range v.NumField() {
106106
f := v.Field(i)
107107
if typ.Field(i).Tag.Get("dump") == "-" {
108108
continue
@@ -132,13 +132,13 @@ func isZero(v reflect.Value) bool {
132132
return true
133133
}
134134
z := true
135-
for i := 0; i < v.Len(); i++ {
135+
for i := range v.Len() {
136136
z = z && isZero(v.Index(i))
137137
}
138138
return z
139139
case reflect.Struct:
140140
z := true
141-
for i := 0; i < v.NumField(); i++ {
141+
for i := range v.NumField() {
142142
z = z && isZero(v.Field(i))
143143
}
144144
return z

0 commit comments

Comments
 (0)