Skip to content
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

Use Go 1.22+ int ranges #7328

Merged
merged 1 commit into from
Jan 30, 2025
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
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ linters:
- errcheck
- govet
- ineffassign
- intrange
- revive # replacement for golint
- gofmt
- goimports
Expand Down
10 changes: 5 additions & 5 deletions cmd/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func benchMain(args []string, params benchmarkCommandParams, w io.Writer, r benc
}

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

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

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

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

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

// Start the timer
b.StartTimer()
Expand Down
2 changes: 1 addition & 1 deletion cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func eval(args []string, params evalCommandParams, w io.Writer) (bool, error) {
profiles := make([][]profiler.ExprStats, ectx.params.count)
timers := make([]map[string]interface{}, ectx.params.count)

for i := 0; i < ectx.params.count; i++ {
for i := range ectx.params.count {
results[i] = evalOnce(ctx, ectx)
profiles[i] = results[i].Profile
if ts, ok := results[i].Metrics.(metrics.TimerMetrics); ok {
Expand Down
2 changes: 1 addition & 1 deletion cmd/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ func TestFmtMultipleWrongArityError(t *testing.T) {
"plus",
}
expectedErrs := ast.Errors(make([]*ast.Error, 3))
for i := 0; i < 3; i++ {
for i := range 3 {
loc := locations[i]
errExp := ast.NewError(ast.TypeErr, &loc, "%s: %s", operators[i], "arity mismatch")
errExp.Details = &format.ArityFormatErrDetail{
Expand Down
6 changes: 3 additions & 3 deletions cmd/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ p = 1
min = len(wantLines)
}

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

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

for i := 0; i < min; i++ {
for i := range min {
if gotLines[i] != wantLines[i] {
t.Fatalf("Expected line %d to be\n%v\n, got\n%v", i, wantLines[i], gotLines[i])
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func opaTest(args []string, testParams testCommandParams) (int, error) {
}

success := true
for i := 0; i < testParams.count; i++ {
for range testParams.count {
exitCode, err := runTests(ctx, txn, runner, reporter, testParams)
if exitCode != 0 {
success = false
Expand Down Expand Up @@ -322,7 +322,7 @@ func processWatcherUpdate(ctx context.Context, testParams testCommandParams, pat
return err
}

for i := 0; i < testParams.count; i++ {
for range testParams.count {
exitCode, err := runTests(ctx, txn, runner, reporter, testParams)
if exitCode != 0 {
return err
Expand Down
10 changes: 5 additions & 5 deletions internal/compiler/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _
// Initialize the locals that specify the path of the upsert operation.
lpath := make(map[int]uint32, len(path))

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

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

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

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

for i := 0; i < len(path)-1; i++ {
for i := range len(path) - 1 {
block = append(block, instruction.Block{Instrs: inner})
block = append(block, instruction.Call{Index: c.function(opaObject)})
block = append(block, instruction.SetLocal{Index: lval})
Expand Down
10 changes: 5 additions & 5 deletions internal/edittree/bitvector/bitvector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *BitVectorSuite) SetUpTest(_ *check.C) {
}

func (s *BitVectorSuite) TestElement(c *check.C) {
for i := 0; i < 4; i++ {
for i := range 4 {
c.Assert(s.vector.Element(i), check.Equals, byte(0))
}
for i := 4; i < 12; i++ {
Expand All @@ -32,14 +32,14 @@ func (s *BitVectorSuite) TestElement(c *check.C) {
}

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

func (s *BitVectorSuite) TestAppend(c *check.C) {
for i := 0; i < 4; i++ {
for i := range 4 {
if i%2 == 0 {
s.vector.Append(0)
} else {
Expand Down Expand Up @@ -68,14 +68,14 @@ func (s *BitVectorSuite) TestSetOneBit(c *check.C) {
}

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

func (s *BitVectorSuite) TestDeleteFirstIndex(c *check.C) {
for i := 0; i < 4; i++ {
for range 4 {
s.vector.Delete(0)
}
c.Assert(s.vector.Bytes(), check.DeepEquals, []byte{0xFF})
Expand Down
10 changes: 5 additions & 5 deletions internal/edittree/edittree.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,14 @@ func (e *EditTree) Delete(key *ast.Term) (*EditTree, error) {
}
// Do rewrites to clear out the newly-removed element.
e.deleteChildValue(idx)
for i := 0; i < len(rewritesScalars); i++ {
for i := range rewritesScalars {
originalIdx := rewritesScalars[i]
rewriteIdx := rewritesScalars[i] - 1
v := e.childScalarValues[originalIdx]
e.deleteChildValue(originalIdx)
e.setChildScalarValue(rewriteIdx, v)
}
for i := 0; i < len(rewritesComposites); i++ {
for i := range rewritesComposites {
originalIdx := rewritesComposites[i]
rewriteIdx := rewritesComposites[i] - 1
v := e.childCompositeValues[originalIdx]
Expand Down Expand Up @@ -591,7 +591,7 @@ func (e *EditTree) Delete(key *ast.Term) (*EditTree, error) {
//gcassert:inline
func sumZeroesBelowIndex(index int, bv *bitvector.BitVector) int {
zeroesSeen := 0
for i := 0; i < index; i++ {
for i := range index {
if bv.Element(i) == 0 {
zeroesSeen++
}
Expand All @@ -601,7 +601,7 @@ func sumZeroesBelowIndex(index int, bv *bitvector.BitVector) int {

func findIndexOfNthZero(n int, bv *bitvector.BitVector) (int, bool) {
zeroesSeen := 0
for i := 0; i < bv.Length(); i++ {
for i := range bv.Length() {
if bv.Element(i) == 0 {
zeroesSeen++
}
Expand Down Expand Up @@ -831,7 +831,7 @@ func (e *EditTree) Render() *ast.Term {
// original array. We build a new Array with modified/deleted keys.
out := make([]*ast.Term, 0, e.insertions.Length())
eIdx := 0
for i := 0; i < e.insertions.Length(); i++ {
for i := range e.insertions.Length() {
// If the index == 0, that indicates we should look up the next
// surviving original element.
// If the index == 1, that indicates we should look up that
Expand Down
8 changes: 4 additions & 4 deletions internal/gqlparser/ast/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func typeName(t reflect.Type) string {
func (d *dumper) dumpArray(v reflect.Value) {
d.WriteString("[" + typeName(v.Type().Elem()) + "]")

for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
d.nl()
d.WriteString("- ")
d.indent++
Expand All @@ -102,7 +102,7 @@ func (d *dumper) dumpStruct(v reflect.Value) {
d.indent++

typ := v.Type()
for i := 0; i < v.NumField(); i++ {
for i := range v.NumField() {
f := v.Field(i)
if typ.Field(i).Tag.Get("dump") == "-" {
continue
Expand Down Expand Up @@ -132,13 +132,13 @@ func isZero(v reflect.Value) bool {
return true
}
z := true
for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
z = z && isZero(v.Index(i))
}
return z
case reflect.Struct:
z := true
for i := 0; i < v.NumField(); i++ {
for i := range v.NumField() {
z = z && isZero(v.Field(i))
}
return z
Expand Down
2 changes: 1 addition & 1 deletion internal/gqlparser/validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
slc = reflect.Append(slc, val)
val = slc
}
for i := 0; i < val.Len(); i++ {
for i := range val.Len() {
resetPath()
v.path = append(v.path, ast.PathIndex(i))
field := val.Index(i)
Expand Down
2 changes: 1 addition & 1 deletion internal/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (o *Oracle) FindDefinition(q DefinitionQuery) (*DefinitionQueryResult, erro
top := stack[len(stack)-1]
if term, ok := top.(*ast.Term); ok {
if name, ok := term.Value.(ast.Var); ok {
for i := 0; i < len(stack); i++ {
for i := range stack {
switch node := stack[i].(type) {
case *ast.Rule:
if match := walkToFirstOccurrence(node.Head.Args, name); match != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/oracle/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ q = true`
t.Fatal("expected an exact set of location pointers but got different number:", len(result), "result:", result)
}

for i := 0; i < len(result); i++ {
for i := range result {
if result[i].Loc() != exp[i] {
t.Fatal("expected exact location pointers but found difference on i =", i, "result:", result)
}
Expand All @@ -474,7 +474,7 @@ q = true`
t.Fatal("expected an exact set of location pointers but got different number:", len(result), "result:", result)
}

for i := 0; i < len(result); i++ {
for i := range result {
if result[i].Loc() != exp[i] {
t.Fatal("expected exact location pointers but found difference on i =", i, "result:", result)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (p *Planner) planRules(rules []*ast.Rule) (string, error) {
}

// Initialize parameters for functions.
for i := 0; i < len(rules[0].Head.Args); i++ {
for range len(rules[0].Head.Args) {
fn.Params = append(fn.Params, p.newLocal())
}

Expand Down
4 changes: 2 additions & 2 deletions internal/planner/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (t *ruletrie) Rules() []*ast.Rule {

func (t *ruletrie) Push(key ast.Ref) {
node := t
for i := 0; i < len(key)-1; i++ {
for i := range len(key) - 1 {
node = node.Get(key[i].Value)
if node == nil {
return
Expand All @@ -123,7 +123,7 @@ func (t *ruletrie) Push(key ast.Ref) {

func (t *ruletrie) Pop(key ast.Ref) {
node := t
for i := 0; i < len(key)-1; i++ {
for i := range len(key) - 1 {
node = node.Get(key[i].Value)
if node == nil {
return
Expand Down
4 changes: 2 additions & 2 deletions internal/providers/aws/crypto/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func TestConstantTimeByteCompare(t *testing.T) {
func BenchmarkConstantTimeCompare(b *testing.B) {
x, y := big.NewInt(1023), big.NewInt(1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for range b.N {
_, _ = ConstantTimeByteCompare(x.Bytes(), y.Bytes())
}
}

func BenchmarkCompare(b *testing.B) {
x, y := big.NewInt(1023).Bytes(), big.NewInt(1024).Bytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for range b.N {
bytes.Compare(x, y)
}
}
2 changes: 1 addition & 1 deletion internal/providers/aws/signing_v4a.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (s *httpSigner) buildCanonicalHeaders(host string, rule v4Internal.Rule, he
var canonicalHeaders strings.Builder
n := len(headers)
const colon = ':'
for i := 0; i < n; i++ {
for i := range n {
if headers[i] == hostHeader {
canonicalHeaders.WriteString(hostHeader)
canonicalHeaders.WriteRune(colon)
Expand Down
4 changes: 2 additions & 2 deletions internal/providers/aws/v4/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestStripExcessHeaders(t *testing.T) {
"12 3 1abc123",
}

for i := 0; i < len(vals); i++ {
for i := range vals {
r := StripExcessSpaces(vals[i])
if e, a := expected[i], r; e != a {
t.Errorf("%d, expect %v, got %v", i, e, a)
Expand Down Expand Up @@ -67,7 +67,7 @@ var stripExcessSpaceCases = []string{
}

func BenchmarkStripExcessSpaces(b *testing.B) {
for i := 0; i < b.N; i++ {
for range b.N {
for _, v := range stripExcessSpaceCases {
StripExcessSpaces(v)
}
Expand Down
Loading