Skip to content

Commit

Permalink
all: modernize -fix ./...
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan committed Feb 24, 2025
1 parent dd6dbb2 commit eb18234
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 93 deletions.
4 changes: 2 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Open(dir string) (*Cache, error) {
if !info.IsDir() {
return nil, &fs.PathError{Op: "open", Path: dir, Err: fmt.Errorf("not a directory")}
}
for i := 0; i < 256; i++ {
for i := range 256 {
name := filepath.Join(dir, fmt.Sprintf("%02x", i))
if err := os.MkdirAll(name, 0777); err != nil {
return nil, err
Expand Down Expand Up @@ -332,7 +332,7 @@ func (c *Cache) Trim() error {
// We subtract an additional mtimeInterval
// to account for the imprecision of our "last used" mtimes.
cutoff := now.Add(-trimLimit - mtimeInterval)
for i := 0; i < 256; i++ {
for i := range 256 {
subdir := filepath.Join(c.dir, fmt.Sprintf("%02x", i))
c.trimSubdir(subdir, cutoff)
}
Expand Down
4 changes: 2 additions & 2 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestGrowth(t *testing.T) {
n = 10
}

for i := 0; i < n; i++ {
for i := range n {
if err := c.putIndexEntry(dummyID(i), dummyID(i*99), int64(i)*101, true); err != nil {
t.Fatalf("addIndexEntry: %v", err)
}
Expand All @@ -93,7 +93,7 @@ func TestGrowth(t *testing.T) {
t.Errorf("Get(%x) = %x, %d, want %x, %d", id, entry.OutputID, entry.Size, dummyID(i*99), int64(i)*101)
}
}
for i := 0; i < n; i++ {
for i := range n {
id := ActionID(dummyID(i))
entry, err := c.Get(id)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/testscript/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ type runT struct {
failed atomic.Bool
}

func (r *runT) Skip(is ...interface{}) {
func (r *runT) Skip(is ...any) {
panic(skipRun)
}

func (r *runT) Fatal(is ...interface{}) {
func (r *runT) Fatal(is ...any) {
r.Log(is...)
r.FailNow()
}
Expand All @@ -197,7 +197,7 @@ func (r *runT) Parallel() {
// TODO run tests in parallel.
}

func (r *runT) Log(is ...interface{}) {
func (r *runT) Log(is ...any) {
msg := fmt.Sprint(is...)
if r.stdinTempFile != "" {
msg = strings.ReplaceAll(msg, r.stdinTempFile, "<stdin>")
Expand Down
4 changes: 2 additions & 2 deletions cmd/txtar-addmod/addmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ It is acceptable to edit the archive afterward to remove or shorten files.

var tmpdir string

func fatalf(format string, args ...interface{}) {
func fatalf(format string, args ...any) {
os.RemoveAll(tmpdir)
log.Fatalf(format, args...)
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func main() {
filePrefix = ".gomodproxy/" + modDir + "/"
} else {
// No comment if we're writing to stdout.
a.Comment = []byte(fmt.Sprintf("module %s\n\n", title))
a.Comment = fmt.Appendf(nil, "module %s\n\n", title)
}
a.Files = []txtar.File{
{Name: filePrefix + ".mod", Data: mod},
Expand Down
7 changes: 2 additions & 5 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ func Diff(oldName string, old []byte, newName string, new []byte) []byte {

// End chunk with common lines for context.
if len(ctext) > 0 {
n := end.x - start.x
if n > C {
n = C
}
n := min(end.x-start.x, C)
for _, s := range x[start.x : start.x+n] {
ctext = append(ctext, " "+s)
count.x++
Expand Down Expand Up @@ -234,7 +231,7 @@ func tgs(x, y []string) []pair {
for i := range T {
T[i] = n + 1
}
for i := 0; i < n; i++ {
for i := range n {
k := sort.Search(n, func(k int) bool {
return T[k] >= J[i]
})
Expand Down
4 changes: 2 additions & 2 deletions fmtsort/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ func compare(aVal, bVal reflect.Value) int {
return 0
}
case reflect.Struct:
for i := 0; i < aVal.NumField(); i++ {
for i := range aVal.NumField() {
if c := compare(aVal.Field(i), bVal.Field(i)); c != 0 {
return c
}
}
return 0
case reflect.Array:
for i := 0; i < aVal.Len(); i++ {
for i := range aVal.Len() {
if c := compare(aVal.Index(i), bVal.Index(i)); c != 0 {
return c
}
Expand Down
16 changes: 8 additions & 8 deletions fmtsort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ var compareTests = [][]reflect.Value{
ct(reflect.TypeOf(chans[0]), chans[0], chans[1], chans[2]),
ct(reflect.TypeOf(toy{}), toy{0, 1}, toy{0, 2}, toy{1, -1}, toy{1, 1}),
ct(reflect.TypeOf([2]int{}), [2]int{1, 1}, [2]int{1, 2}, [2]int{2, 0}),
ct(reflect.TypeOf(interface{}(interface{}(0))), iFace, 1, 2, 3),
ct(reflect.TypeOf(any(any(0))), iFace, 1, 2, 3),
}

var iFace interface{}
var iFace any

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

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

var sortTests = []sortTest{
Expand Down Expand Up @@ -132,7 +132,7 @@ var sortTests = []sortTest{
},
}

func sprint(data interface{}) string {
func sprint(data any) string {
om := fmtsort.Sort(reflect.ValueOf(data))
if om == nil {
return "nil"
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestInterface(t *testing.T) {
// A map containing multiple concrete types should be sorted by type,
// then value. However, the relative ordering of types is unspecified,
// so test this by checking the presence of sorted subgroups.
m := map[interface{}]string{
m := map[any]string{
[2]int{1, 0}: "",
[2]int{0, 1}: "",
true: "",
Expand Down
2 changes: 1 addition & 1 deletion goproxytest/allhex.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package goproxytest

// allHex reports whether the revision rev is entirely lower-case hexadecimal digits.
func allHex(rev string) bool {
for i := 0; i < len(rev); i++ {
for i := range len(rev) {
c := rev[i]
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' {
continue
Expand Down
4 changes: 2 additions & 2 deletions goproxytest/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
zip []byte
err error
}
c := srv.zipCache.Do(a, func() interface{} {
c := srv.zipCache.Do(a, func() any {
var buf bytes.Buffer
z := zip.NewWriter(&buf)
for _, f := range a.Files {
Expand Down Expand Up @@ -305,7 +305,7 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
name := filepath.Join(srv.dir, prefix+"_"+encVers)
txtName := name + ".txt"
txtarName := name + ".txtar"
a := srv.archiveCache.Do(name, func() interface{} {
a := srv.archiveCache.Do(name, func() any {
a, err := txtar.ParseFile(txtarName)
if os.IsNotExist(err) {
// fall back to trying with the .txt extension
Expand Down
2 changes: 1 addition & 1 deletion imports/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *importReader) nextByte(skipSpace bool) byte {
// If the keyword is not present, readKeyword records a syntax error.
func (r *importReader) readKeyword(kw string) {
r.peekByte(true)
for i := 0; i < len(kw); i++ {
for i := range len(kw) {
if r.nextByte(false) != kw[i] {
r.syntaxError()
return
Expand Down
26 changes: 13 additions & 13 deletions par/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (
// Work manages a set of work items to be executed in parallel, at most once each.
// The items in the set must all be valid map keys.
type Work struct {
f func(interface{}) // function to run for each item
running int // total number of runners
f func(any) // function to run for each item
running int // total number of runners

mu sync.Mutex
added map[interface{}]bool // items added to set
todo []interface{} // items yet to be run
wait sync.Cond // wait when todo is empty
waiting int // number of runners waiting for todo
added map[any]bool // items added to set
todo []any // items yet to be run
wait sync.Cond // wait when todo is empty
waiting int // number of runners waiting for todo
}

func (w *Work) init() {
if w.added == nil {
w.added = make(map[interface{}]bool)
w.added = make(map[any]bool)
}
}

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

for i := 0; i < n-1; i++ {
for range n - 1 {
go w.runner()
}
w.runner()
Expand Down Expand Up @@ -110,13 +110,13 @@ type Cache struct {
type cacheEntry struct {
done uint32
mu sync.Mutex
result interface{}
result any
}

// Do calls the function f if and only if Do is being called for the first time with this key.
// No call to Do with a given key returns until the one call to f returns.
// Do returns the value returned by the one call to f.
func (c *Cache) Do(key interface{}, f func() interface{}) interface{} {
func (c *Cache) Do(key any, f func() any) any {
entryIface, ok := c.m.Load(key)
if !ok {
entryIface, _ = c.m.LoadOrStore(key, new(cacheEntry))
Expand All @@ -136,7 +136,7 @@ func (c *Cache) Do(key interface{}, f func() interface{}) interface{} {
// Get returns the cached result associated with key.
// It returns nil if there is no such result.
// If the result for key is being computed, Get does not wait for the computation to finish.
func (c *Cache) Get(key interface{}) interface{} {
func (c *Cache) Get(key any) any {
entryIface, ok := c.m.Load(key)
if !ok {
return nil
Expand Down
16 changes: 8 additions & 8 deletions par/work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestWork(t *testing.T) {
const N = 10000
n := int32(0)
w.Add(N)
w.Do(100, func(x interface{}) {
w.Do(100, func(x any) {
atomic.AddInt32(&n, 1)
i := x.(int)
if i >= 2 {
Expand All @@ -32,15 +32,15 @@ func TestWork(t *testing.T) {
}

func TestWorkParallel(t *testing.T) {
for tries := 0; tries < 10; tries++ {
for range 10 {
var w Work
const N = 100
for i := 0; i < N; i++ {
for i := range N {
w.Add(i)
}
start := time.Now()
var n int32
w.Do(N, func(x interface{}) {
w.Do(N, func(x any) {
time.Sleep(1 * time.Millisecond)
atomic.AddInt32(&n, +1)
})
Expand All @@ -58,19 +58,19 @@ func TestCache(t *testing.T) {
var cache Cache

n := 1
v := cache.Do(1, func() interface{} { n++; return n })
v := cache.Do(1, func() any { n++; return n })
if v != 2 {
t.Fatalf("cache.Do(1) did not run f")
}
v = cache.Do(1, func() interface{} { n++; return n })
v = cache.Do(1, func() any { n++; return n })
if v != 2 {
t.Fatalf("cache.Do(1) ran f again!")
}
v = cache.Do(2, func() interface{} { n++; return n })
v = cache.Do(2, func() any { n++; return n })
if v != 3 {
t.Fatalf("cache.Do(2) did not run f")
}
v = cache.Do(1, func() interface{} { n++; return n })
v = cache.Do(1, func() any { n++; return n })
if v != 2 {
t.Fatalf("cache.Do(1) did not returned saved value from original cache.Do(1)")
}
Expand Down
3 changes: 2 additions & 1 deletion testscript/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -592,7 +593,7 @@ func (ts *TestScript) waitBackgroundOne(bgName string) {
// Remove this process from the list of running background processes.
for i := range ts.background {
if bg == &ts.background[i] {
ts.background = append(ts.background[:i], ts.background[i+1:]...)
ts.background = slices.Delete(ts.background, i, i+1)
break
}
}
Expand Down
Loading

0 comments on commit eb18234

Please sign in to comment.