Skip to content

Commit

Permalink
start using some Go 1.22 features
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvdan authored and pagran committed Feb 12, 2024
1 parent ad2ecc7 commit 69bc62c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 4 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"bufio"
"bytes"
"cmp"
cryptorand "crypto/rand"
"encoding/base64"
"encoding/binary"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
7 changes: 2 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 69bc62c

Please sign in to comment.