Skip to content

Commit eb18234

Browse files
committed
all: modernize -fix ./...
1 parent dd6dbb2 commit eb18234

File tree

15 files changed

+89
-93
lines changed

15 files changed

+89
-93
lines changed

cache/cache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func Open(dir string) (*Cache, error) {
5555
if !info.IsDir() {
5656
return nil, &fs.PathError{Op: "open", Path: dir, Err: fmt.Errorf("not a directory")}
5757
}
58-
for i := 0; i < 256; i++ {
58+
for i := range 256 {
5959
name := filepath.Join(dir, fmt.Sprintf("%02x", i))
6060
if err := os.MkdirAll(name, 0777); err != nil {
6161
return nil, err
@@ -332,7 +332,7 @@ func (c *Cache) Trim() error {
332332
// We subtract an additional mtimeInterval
333333
// to account for the imprecision of our "last used" mtimes.
334334
cutoff := now.Add(-trimLimit - mtimeInterval)
335-
for i := 0; i < 256; i++ {
335+
for i := range 256 {
336336
subdir := filepath.Join(c.dir, fmt.Sprintf("%02x", i))
337337
c.trimSubdir(subdir, cutoff)
338338
}

cache/cache_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestGrowth(t *testing.T) {
8080
n = 10
8181
}
8282

83-
for i := 0; i < n; i++ {
83+
for i := range n {
8484
if err := c.putIndexEntry(dummyID(i), dummyID(i*99), int64(i)*101, true); err != nil {
8585
t.Fatalf("addIndexEntry: %v", err)
8686
}
@@ -93,7 +93,7 @@ func TestGrowth(t *testing.T) {
9393
t.Errorf("Get(%x) = %x, %d, want %x, %d", id, entry.OutputID, entry.Size, dummyID(i*99), int64(i)*101)
9494
}
9595
}
96-
for i := 0; i < n; i++ {
96+
for i := range n {
9797
id := ActionID(dummyID(i))
9898
entry, err := c.Get(id)
9999
if err != nil {

cmd/testscript/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ type runT struct {
184184
failed atomic.Bool
185185
}
186186

187-
func (r *runT) Skip(is ...interface{}) {
187+
func (r *runT) Skip(is ...any) {
188188
panic(skipRun)
189189
}
190190

191-
func (r *runT) Fatal(is ...interface{}) {
191+
func (r *runT) Fatal(is ...any) {
192192
r.Log(is...)
193193
r.FailNow()
194194
}
@@ -197,7 +197,7 @@ func (r *runT) Parallel() {
197197
// TODO run tests in parallel.
198198
}
199199

200-
func (r *runT) Log(is ...interface{}) {
200+
func (r *runT) Log(is ...any) {
201201
msg := fmt.Sprint(is...)
202202
if r.stdinTempFile != "" {
203203
msg = strings.ReplaceAll(msg, r.stdinTempFile, "<stdin>")

cmd/txtar-addmod/addmod.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ It is acceptable to edit the archive afterward to remove or shorten files.
5555

5656
var tmpdir string
5757

58-
func fatalf(format string, args ...interface{}) {
58+
func fatalf(format string, args ...any) {
5959
os.RemoveAll(tmpdir)
6060
log.Fatalf(format, args...)
6161
}
@@ -150,7 +150,7 @@ func main() {
150150
filePrefix = ".gomodproxy/" + modDir + "/"
151151
} else {
152152
// No comment if we're writing to stdout.
153-
a.Comment = []byte(fmt.Sprintf("module %s\n\n", title))
153+
a.Comment = fmt.Appendf(nil, "module %s\n\n", title)
154154
}
155155
a.Files = []txtar.File{
156156
{Name: filePrefix + ".mod", Data: mod},

diff/diff.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ func Diff(oldName string, old []byte, newName string, new []byte) []byte {
116116

117117
// End chunk with common lines for context.
118118
if len(ctext) > 0 {
119-
n := end.x - start.x
120-
if n > C {
121-
n = C
122-
}
119+
n := min(end.x-start.x, C)
123120
for _, s := range x[start.x : start.x+n] {
124121
ctext = append(ctext, " "+s)
125122
count.x++
@@ -234,7 +231,7 @@ func tgs(x, y []string) []pair {
234231
for i := range T {
235232
T[i] = n + 1
236233
}
237-
for i := 0; i < n; i++ {
234+
for i := range n {
238235
k := sort.Search(n, func(k int) bool {
239236
return T[k] >= J[i]
240237
})

fmtsort/sort.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ func compare(aVal, bVal reflect.Value) int {
143143
return 0
144144
}
145145
case reflect.Struct:
146-
for i := 0; i < aVal.NumField(); i++ {
146+
for i := range aVal.NumField() {
147147
if c := compare(aVal.Field(i), bVal.Field(i)); c != 0 {
148148
return c
149149
}
150150
}
151151
return 0
152152
case reflect.Array:
153-
for i := 0; i < aVal.Len(); i++ {
153+
for i := range aVal.Len() {
154154
if c := compare(aVal.Index(i), bVal.Index(i)); c != 0 {
155155
return c
156156
}

fmtsort/sort_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ var compareTests = [][]reflect.Value{
3636
ct(reflect.TypeOf(chans[0]), chans[0], chans[1], chans[2]),
3737
ct(reflect.TypeOf(toy{}), toy{0, 1}, toy{0, 2}, toy{1, -1}, toy{1, 1}),
3838
ct(reflect.TypeOf([2]int{}), [2]int{1, 1}, [2]int{1, 2}, [2]int{2, 0}),
39-
ct(reflect.TypeOf(interface{}(interface{}(0))), iFace, 1, 2, 3),
39+
ct(reflect.TypeOf(any(any(0))), iFace, 1, 2, 3),
4040
}
4141

42-
var iFace interface{}
42+
var iFace any
4343

44-
func ct(typ reflect.Type, args ...interface{}) []reflect.Value {
44+
func ct(typ reflect.Type, args ...any) []reflect.Value {
4545
value := make([]reflect.Value, len(args))
4646
for i, v := range args {
4747
x := reflect.ValueOf(v)
@@ -82,9 +82,9 @@ func TestCompare(t *testing.T) {
8282
}
8383

8484
type sortTest struct {
85-
data interface{} // Always a map.
86-
print string // Printed result using our custom printer.
87-
printBrokenNaNs string // Printed result when NaN support is broken (pre Go1.12).
85+
data any // Always a map.
86+
print string // Printed result using our custom printer.
87+
printBrokenNaNs string // Printed result when NaN support is broken (pre Go1.12).
8888
}
8989

9090
var sortTests = []sortTest{
@@ -132,7 +132,7 @@ var sortTests = []sortTest{
132132
},
133133
}
134134

135-
func sprint(data interface{}) string {
135+
func sprint(data any) string {
136136
om := fmtsort.Sort(reflect.ValueOf(data))
137137
if om == nil {
138138
return "nil"
@@ -219,7 +219,7 @@ func TestInterface(t *testing.T) {
219219
// A map containing multiple concrete types should be sorted by type,
220220
// then value. However, the relative ordering of types is unspecified,
221221
// so test this by checking the presence of sorted subgroups.
222-
m := map[interface{}]string{
222+
m := map[any]string{
223223
[2]int{1, 0}: "",
224224
[2]int{0, 1}: "",
225225
true: "",

goproxytest/allhex.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package goproxytest
88

99
// allHex reports whether the revision rev is entirely lower-case hexadecimal digits.
1010
func allHex(rev string) bool {
11-
for i := 0; i < len(rev); i++ {
11+
for i := range len(rev) {
1212
c := rev[i]
1313
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' {
1414
continue

goproxytest/proxy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
239239
zip []byte
240240
err error
241241
}
242-
c := srv.zipCache.Do(a, func() interface{} {
242+
c := srv.zipCache.Do(a, func() any {
243243
var buf bytes.Buffer
244244
z := zip.NewWriter(&buf)
245245
for _, f := range a.Files {
@@ -305,7 +305,7 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
305305
name := filepath.Join(srv.dir, prefix+"_"+encVers)
306306
txtName := name + ".txt"
307307
txtarName := name + ".txtar"
308-
a := srv.archiveCache.Do(name, func() interface{} {
308+
a := srv.archiveCache.Do(name, func() any {
309309
a, err := txtar.ParseFile(txtarName)
310310
if os.IsNotExist(err) {
311311
// fall back to trying with the .txt extension

imports/read.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (r *importReader) nextByte(skipSpace bool) byte {
123123
// If the keyword is not present, readKeyword records a syntax error.
124124
func (r *importReader) readKeyword(kw string) {
125125
r.peekByte(true)
126-
for i := 0; i < len(kw); i++ {
126+
for i := range len(kw) {
127127
if r.nextByte(false) != kw[i] {
128128
r.syntaxError()
129129
return

par/work.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ import (
1414
// Work manages a set of work items to be executed in parallel, at most once each.
1515
// The items in the set must all be valid map keys.
1616
type Work struct {
17-
f func(interface{}) // function to run for each item
18-
running int // total number of runners
17+
f func(any) // function to run for each item
18+
running int // total number of runners
1919

2020
mu sync.Mutex
21-
added map[interface{}]bool // items added to set
22-
todo []interface{} // items yet to be run
23-
wait sync.Cond // wait when todo is empty
24-
waiting int // number of runners waiting for todo
21+
added map[any]bool // items added to set
22+
todo []any // items yet to be run
23+
wait sync.Cond // wait when todo is empty
24+
waiting int // number of runners waiting for todo
2525
}
2626

2727
func (w *Work) init() {
2828
if w.added == nil {
29-
w.added = make(map[interface{}]bool)
29+
w.added = make(map[any]bool)
3030
}
3131
}
3232

3333
// Add adds item to the work set, if it hasn't already been added.
34-
func (w *Work) Add(item interface{}) {
34+
func (w *Work) Add(item any) {
3535
w.mu.Lock()
3636
w.init()
3737
if !w.added[item] {
@@ -51,7 +51,7 @@ func (w *Work) Add(item interface{}) {
5151
// before calling Do (or else Do returns immediately),
5252
// but it is allowed for f(item) to add new items to the set.
5353
// Do should only be used once on a given Work.
54-
func (w *Work) Do(n int, f func(item interface{})) {
54+
func (w *Work) Do(n int, f func(item any)) {
5555
if n < 1 {
5656
panic("par.Work.Do: n < 1")
5757
}
@@ -63,7 +63,7 @@ func (w *Work) Do(n int, f func(item interface{})) {
6363
w.f = f
6464
w.wait.L = &w.mu
6565

66-
for i := 0; i < n-1; i++ {
66+
for range n - 1 {
6767
go w.runner()
6868
}
6969
w.runner()
@@ -110,13 +110,13 @@ type Cache struct {
110110
type cacheEntry struct {
111111
done uint32
112112
mu sync.Mutex
113-
result interface{}
113+
result any
114114
}
115115

116116
// Do calls the function f if and only if Do is being called for the first time with this key.
117117
// No call to Do with a given key returns until the one call to f returns.
118118
// Do returns the value returned by the one call to f.
119-
func (c *Cache) Do(key interface{}, f func() interface{}) interface{} {
119+
func (c *Cache) Do(key any, f func() any) any {
120120
entryIface, ok := c.m.Load(key)
121121
if !ok {
122122
entryIface, _ = c.m.LoadOrStore(key, new(cacheEntry))
@@ -136,7 +136,7 @@ func (c *Cache) Do(key interface{}, f func() interface{}) interface{} {
136136
// Get returns the cached result associated with key.
137137
// It returns nil if there is no such result.
138138
// If the result for key is being computed, Get does not wait for the computation to finish.
139-
func (c *Cache) Get(key interface{}) interface{} {
139+
func (c *Cache) Get(key any) any {
140140
entryIface, ok := c.m.Load(key)
141141
if !ok {
142142
return nil

par/work_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestWork(t *testing.T) {
1616
const N = 10000
1717
n := int32(0)
1818
w.Add(N)
19-
w.Do(100, func(x interface{}) {
19+
w.Do(100, func(x any) {
2020
atomic.AddInt32(&n, 1)
2121
i := x.(int)
2222
if i >= 2 {
@@ -32,15 +32,15 @@ func TestWork(t *testing.T) {
3232
}
3333

3434
func TestWorkParallel(t *testing.T) {
35-
for tries := 0; tries < 10; tries++ {
35+
for range 10 {
3636
var w Work
3737
const N = 100
38-
for i := 0; i < N; i++ {
38+
for i := range N {
3939
w.Add(i)
4040
}
4141
start := time.Now()
4242
var n int32
43-
w.Do(N, func(x interface{}) {
43+
w.Do(N, func(x any) {
4444
time.Sleep(1 * time.Millisecond)
4545
atomic.AddInt32(&n, +1)
4646
})
@@ -58,19 +58,19 @@ func TestCache(t *testing.T) {
5858
var cache Cache
5959

6060
n := 1
61-
v := cache.Do(1, func() interface{} { n++; return n })
61+
v := cache.Do(1, func() any { n++; return n })
6262
if v != 2 {
6363
t.Fatalf("cache.Do(1) did not run f")
6464
}
65-
v = cache.Do(1, func() interface{} { n++; return n })
65+
v = cache.Do(1, func() any { n++; return n })
6666
if v != 2 {
6767
t.Fatalf("cache.Do(1) ran f again!")
6868
}
69-
v = cache.Do(2, func() interface{} { n++; return n })
69+
v = cache.Do(2, func() any { n++; return n })
7070
if v != 3 {
7171
t.Fatalf("cache.Do(2) did not run f")
7272
}
73-
v = cache.Do(1, func() interface{} { n++; return n })
73+
v = cache.Do(1, func() any { n++; return n })
7474
if v != 2 {
7575
t.Fatalf("cache.Do(1) did not returned saved value from original cache.Do(1)")
7676
}

testscript/cmd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"regexp"
1515
"runtime"
16+
"slices"
1617
"strconv"
1718
"strings"
1819

@@ -592,7 +593,7 @@ func (ts *TestScript) waitBackgroundOne(bgName string) {
592593
// Remove this process from the list of running background processes.
593594
for i := range ts.background {
594595
if bg == &ts.background[i] {
595-
ts.background = append(ts.background[:i], ts.background[i+1:]...)
596+
ts.background = slices.Delete(ts.background, i, i+1)
596597
break
597598
}
598599
}

0 commit comments

Comments
 (0)