Skip to content

Commit 6f067a6

Browse files
committed
all: modernize code
Run modernize over the repository to update old Go idioms. Changes include: for i := 0; i < N; i++ -> for i := range N interface{} -> any for k, v := range x { y[k] = v } -> maps.Copy(y, x) HasPrefix+TrimPrefix -> CutPrefix Change generated by running: ``` go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... ```
1 parent df17445 commit 6f067a6

File tree

9 files changed

+16
-17
lines changed

9 files changed

+16
-17
lines changed

gomock/call.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (c *Call) DoAndReturn(f any) *Call {
129129
return nil
130130
}
131131
vArgs := make([]reflect.Value, len(args))
132-
for i := 0; i < len(args); i++ {
132+
for i := range args {
133133
if args[i] != nil {
134134
vArgs[i] = reflect.ValueOf(args[i])
135135
} else {
@@ -170,7 +170,7 @@ func (c *Call) Do(f any) *Call {
170170
return nil
171171
}
172172
vArgs := make([]reflect.Value, len(args))
173-
for i := 0; i < len(args); i++ {
173+
for i := range args {
174174
if args[i] != nil {
175175
vArgs[i] = reflect.ValueOf(args[i])
176176
} else {
@@ -437,7 +437,7 @@ func (c *Call) call() []func([]any) []any {
437437
// mock with an embedded *Call.
438438
func InOrder(args ...any) {
439439
calls := make([]*Call, 0, len(args))
440-
for i := 0; i < len(args); i++ {
440+
for i := range args {
441441
if call := getCall(args[i]); call != nil {
442442
calls = append(calls, call)
443443
continue

gomock/callset_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestCallSetAdd(t *testing.T) {
2929
cs := newCallSet()
3030

3131
numCalls := 10
32-
for i := 0; i < numCalls; i++ {
32+
for range numCalls {
3333
cs.Add(newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func)))
3434
}
3535

@@ -68,7 +68,7 @@ func TestCallSetRemove(t *testing.T) {
6868
ourCalls := []*Call{}
6969

7070
numCalls := 10
71-
for i := 0; i < numCalls; i++ {
71+
for i := range numCalls {
7272
// NOTE: abuse the `numCalls` value to convey initial ordering of mocked calls
7373
generatedCall := &Call{receiver: receiver, method: method, numCalls: i}
7474
cs.Add(generatedCall)

gomock/controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func TestAnyTimes(t *testing.T) {
393393
subject := new(Subject)
394394

395395
ctrl.RecordCall(subject, "FooMethod", "argument").AnyTimes()
396-
for i := 0; i < 100; i++ {
396+
for range 100 {
397397
ctrl.Call(subject, "FooMethod", "argument")
398398
}
399399
reporter.assertPass("After 100 method calls.")
@@ -419,7 +419,7 @@ func TestMinTimes1(t *testing.T) {
419419
_, ctrl = createFixtures(t)
420420
subject = new(Subject)
421421
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
422-
for i := 0; i < 100; i++ {
422+
for range 100 {
423423
ctrl.Call(subject, "FooMethod", "argument")
424424
}
425425
ctrl.Finish()
@@ -492,7 +492,7 @@ func TestMinMaxTimes(t *testing.T) {
492492
_, ctrl = createFixtures(t)
493493
subject = new(Subject)
494494
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1).MinTimes(2)
495-
for i := 0; i < 100; i++ {
495+
for range 100 {
496496
ctrl.Call(subject, "FooMethod", "argument")
497497
}
498498
ctrl.Finish()

mockgen/internal/tests/build_constraint/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package empty_interface
22

33
//go:generate mockgen -package empty_interface -destination mock.go -source input.go "-build_constraint=(linux && 386) || (darwin && !cgo) || usertag"
44

5-
type Empty interface{}
5+
type Empty any

mockgen/internal/tests/copyright_file/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package empty_interface
22

33
//go:generate mockgen -package empty_interface -destination mock.go -source input.go -copyright_file=mock_copyright_header
44

5-
type Empty interface{} // migrating interface{} -> any does not resolve to an interface type dropping test generation added in b391ab3
5+
type Empty any // migrating interface{} -> any does not resolve to an interface type dropping test generation added in b391ab3

mockgen/internal/tests/empty_interface/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package empty_interface
22

33
//go:generate mockgen -package empty_interface -destination mock.go -source input.go
44

5-
type Empty interface{} // migrating interface{} -> any does not resolve to an interface type.
5+
type Empty any // migrating interface{} -> any does not resolve to an interface type.

mockgen/internal/tests/package_comment/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package empty_interface
22

33
//go:generate mockgen -package empty_interface -destination mock.go -source input.go -write_package_comment=false
44

5-
type Empty interface{}
5+
type Empty any

mockgen/mockgen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,8 @@ func parsePackageImport(srcDir string) (string, error) {
937937
goPathList := strings.Split(goPaths, string(os.PathListSeparator))
938938
for _, goPath := range goPathList {
939939
sourceRoot := filepath.Join(goPath, "src") + string(os.PathSeparator)
940-
if strings.HasPrefix(srcDir, sourceRoot) {
941-
return filepath.ToSlash(strings.TrimPrefix(srcDir, sourceRoot)), nil
940+
if after, ok := strings.CutPrefix(srcDir, sourceRoot); ok {
941+
return filepath.ToSlash(after), nil
942942
}
943943
}
944944
return "", errOutsideGoPath

mockgen/parse.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"go/token"
2828
"go/types"
2929
"log"
30+
"maps"
3031
"os"
3132
"path"
3233
"path/filepath"
@@ -300,9 +301,7 @@ func (p *fileParser) parsePackage(path string) (*fileParser, error) {
300301
newP.importedInterfaces.Set(path, ni.name.Name, ni)
301302
}
302303
imports, _ := importsOfFile(file)
303-
for pkgName, pkgI := range imports {
304-
newP.imports[pkgName] = pkgI
305-
}
304+
maps.Copy(newP.imports, imports)
306305
}
307306
return newP, nil
308307
}

0 commit comments

Comments
 (0)