start using some Go 1.22 features

We no longer need to worry about the scope of range variables,
we can iterate over integers directly, and we can use cmp.Or too.

I haven't paid close attention to using these everywhere.
This is mainly testing out the new features where I saw some benefit.
pull/834/head
Daniel Martí 3 months ago committed by pagran
parent ad2ecc7f2f
commit 69bc62c56c

@ -70,7 +70,7 @@ func BenchmarkBuild(b *testing.B) {
b.ResetTimer()
b.StopTimer()
for i := 0; i < b.N; i++ {
for i := range b.N {
// First we do a fresh build, using empty cache directories,
// and the second does an incremental rebuild reusing the same cache directories.
goCache := filepath.Join(tdir, "go-cache")

@ -7,6 +7,7 @@ package main
import (
"bufio"
"bytes"
"cmp"
cryptorand "crypto/rand"
"encoding/base64"
"encoding/binary"
@ -287,10 +288,7 @@ func goVersionOK() bool {
// Ensure that the version of Go that built the garble binary is equal or
// newer than cache.GoVersionSemver.
builtVersionFull := os.Getenv("GARBLE_TEST_GOVERSION")
if builtVersionFull == "" {
builtVersionFull = runtime.Version()
}
builtVersionFull := cmp.Or(os.Getenv("GARBLE_TEST_GOVERSION"), runtime.Version())
builtVersion := rxVersion.FindString(builtVersionFull)
if builtVersion == "" {
// If garble built itself, we don't know what Go version was used.
@ -1727,7 +1725,7 @@ func recordType(used, origin types.Type, done map[*types.Named]bool, fieldToStru
recordType(used.Underlying(), used.Origin().Underlying(), done, fieldToStruct)
case *types.Struct:
origin := origin.(*types.Struct)
for i := 0; i < used.NumFields(); i++ {
for i := range used.NumFields() {
field := used.Field(i)
fieldToStruct[field] = origin
@ -2383,9 +2381,6 @@ To install Go, see: https://go.dev/doc/install
if err := json.Unmarshal(out, &sharedCache.GoEnv); err != nil {
return fmt.Errorf(`cannot unmarshal from "go env -json": %w`, err)
}
sharedCache.GOGARBLE = os.Getenv("GOGARBLE")
if sharedCache.GOGARBLE == "" {
sharedCache.GOGARBLE = "*" // we default to obfuscating everything
}
sharedCache.GOGARBLE = cmp.Or(os.Getenv("GOGARBLE"), "*") // we default to obfuscating everything
return nil
}

@ -295,7 +295,7 @@ func generateLiterals(ts *testscript.TestScript, neg bool, args []string) {
var statements []ast.Stmt
// Assignments which append 100 random small literals to x: `x += "the_small_random_literal"`
for i := 0; i < 100; i++ {
for range 100 {
statements = append(
statements,
&ast.AssignStmt{
@ -310,7 +310,7 @@ func generateLiterals(ts *testscript.TestScript, neg bool, args []string) {
// We add huge literals to make sure we obfuscate them fast.
// 5 * 128KiB is large enough that it would take a very, very long time
// to obfuscate those literals if too complex obfuscators are used.
for i := 0; i < 5; i++ {
for range 5 {
statements = append(
statements,
&ast.AssignStmt{
@ -421,7 +421,6 @@ func TestSplitFlagsFromArgs(t *testing.T) {
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
flags, args := splitFlagsFromArgs(test.args)
@ -459,7 +458,6 @@ func TestFilterForwardBuildFlags(t *testing.T) {
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got, _ := filterForwardBuildFlags(test.flags)
@ -488,7 +486,6 @@ func TestFlagValue(t *testing.T) {
{"StrEmpty", []string{"-buildid="}, "-buildid", ""},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := flagValue(test.flags, test.flagName)

@ -69,7 +69,7 @@ func (ri *reflectInspector) ignoreReflectedTypes(ssaPkg *ssa.Package) {
// so some logic is required to find the methods a type has
method := func(mset *types.MethodSet) {
for i, n := 0, mset.Len(); i < n; i++ {
for i := range mset.Len() {
at := mset.At(i)
if m := ssaPkg.Prog.MethodValue(at); m != nil {
@ -112,7 +112,7 @@ func (ri *reflectInspector) checkMethodSignature(reflectParams map[int]bool, sig
}
params := sig.Params()
for i := 0; i < params.Len(); i++ {
for i := range params.Len() {
if reflectParams[i] {
continue
}
@ -404,7 +404,7 @@ func (ri *reflectInspector) recursivelyRecordUsedForReflect(t types.Type) {
ri.recursivelyRecordUsedForReflect(t.Underlying())
case *types.Struct:
for i := 0; i < t.NumFields(); i++ {
for i := range t.NumFields() {
field := t.Field(i)
// This check is similar to the one in *types.Named.

Loading…
Cancel
Save