Skip to content

Commit 521911a

Browse files
committed
all: Run modernize -fix ./...
1 parent b7ae24b commit 521911a

File tree

141 files changed

+302
-354
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+302
-354
lines changed

cache/dynacache/dynacache_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,16 @@ func TestPanicInCreate(t *testing.T) {
191191
return err
192192
}
193193

194-
for i := 0; i < 3; i++ {
195-
for j := 0; j < 3; j++ {
194+
for i := range 3 {
195+
for range 3 {
196196
c.Assert(willPanic(i), qt.PanicMatches, fmt.Sprintf("panic-%d", i))
197197
c.Assert(willErr(i), qt.ErrorMatches, fmt.Sprintf("error-%d", i))
198198
}
199199
}
200200

201201
// Test the same keys again without the panic.
202-
for i := 0; i < 3; i++ {
203-
for j := 0; j < 3; j++ {
202+
for i := range 3 {
203+
for range 3 {
204204
v, err := p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
205205
return testItem{
206206
name: key,

cache/filecache/filecache_pruner_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ dir = ":resourceDir/_gen"
5959
caches, err := filecache.NewCaches(p)
6060
c.Assert(err, qt.IsNil)
6161
cache := caches[name]
62-
for i := 0; i < 10; i++ {
62+
for i := range 10 {
6363
id := fmt.Sprintf("i%d", i)
6464
cache.GetOrCreateBytes(id, func() ([]byte, error) {
6565
return []byte("abc"), nil
@@ -74,7 +74,7 @@ dir = ":resourceDir/_gen"
7474
c.Assert(err, qt.IsNil)
7575
c.Assert(count, qt.Equals, 5, msg)
7676

77-
for i := 0; i < 10; i++ {
77+
for i := range 10 {
7878
id := fmt.Sprintf("i%d", i)
7979
v := cache.GetString(id)
8080
if i < 5 {
@@ -97,7 +97,7 @@ dir = ":resourceDir/_gen"
9797
c.Assert(count, qt.Equals, 4)
9898

9999
// Now only the i5 should be left.
100-
for i := 0; i < 10; i++ {
100+
for i := range 10 {
101101
id := fmt.Sprintf("i%d", i)
102102
v := cache.GetString(id)
103103
if i != 5 {

cache/filecache/filecache_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ dir = ":cacheDir/c"
105105
}
106106

107107
for _, ca := range []*filecache.Cache{caches.ImageCache(), caches.AssetsCache(), caches.GetJSONCache(), caches.GetCSVCache()} {
108-
for i := 0; i < 2; i++ {
108+
for range 2 {
109109
info, r, err := ca.GetOrCreate("a", rf("abc"))
110110
c.Assert(err, qt.IsNil)
111111
c.Assert(r, qt.Not(qt.IsNil))
@@ -193,11 +193,11 @@ dir = "/cache/c"
193193

194194
var wg sync.WaitGroup
195195

196-
for i := 0; i < 50; i++ {
196+
for i := range 50 {
197197
wg.Add(1)
198198
go func(i int) {
199199
defer wg.Done()
200-
for j := 0; j < 20; j++ {
200+
for range 20 {
201201
ca := caches.Get(cacheName)
202202
c.Assert(ca, qt.Not(qt.IsNil))
203203
filename, data := filenameData(i)

codegen/methods.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"path/filepath"
2727
"reflect"
2828
"regexp"
29+
"slices"
2930
"sort"
3031
"strings"
3132
"sync"
@@ -102,7 +103,7 @@ func (c *Inspector) MethodsFromTypes(include []reflect.Type, exclude []reflect.T
102103
}
103104

104105
for _, t := range include {
105-
for i := 0; i < t.NumMethod(); i++ {
106+
for i := range t.NumMethod() {
106107

107108
m := t.Method(i)
108109
if excludes[m.Name] || seen[m.Name] {
@@ -122,7 +123,7 @@ func (c *Inspector) MethodsFromTypes(include []reflect.Type, exclude []reflect.T
122123

123124
method := Method{Owner: t, OwnerName: ownerName, Name: m.Name}
124125

125-
for i := 0; i < numIn; i++ {
126+
for i := range numIn {
126127
in := m.Type.In(i)
127128

128129
name, pkg := nameAndPackage(in)
@@ -137,7 +138,7 @@ func (c *Inspector) MethodsFromTypes(include []reflect.Type, exclude []reflect.T
137138
numOut := m.Type.NumOut()
138139

139140
if numOut > 0 {
140-
for i := 0; i < numOut; i++ {
141+
for i := range numOut {
141142
out := m.Type.Out(i)
142143
name, pkg := nameAndPackage(out)
143144

@@ -304,7 +305,7 @@ func (m Method) inOutStr() string {
304305
}
305306

306307
args := make([]string, len(m.In))
307-
for i := 0; i < len(args); i++ {
308+
for i := range args {
308309
args[i] = fmt.Sprintf("arg%d", i)
309310
}
310311
return "(" + strings.Join(args, ", ") + ")"
@@ -316,7 +317,7 @@ func (m Method) inStr() string {
316317
}
317318

318319
args := make([]string, len(m.In))
319-
for i := 0; i < len(args); i++ {
320+
for i := range args {
320321
args[i] = fmt.Sprintf("arg%d %s", i, m.In[i])
321322
}
322323
return "(" + strings.Join(args, ", ") + ")"
@@ -339,7 +340,7 @@ func (m Method) outStrNamed() string {
339340
}
340341

341342
outs := make([]string, len(m.Out))
342-
for i := 0; i < len(outs); i++ {
343+
for i := range outs {
343344
outs[i] = fmt.Sprintf("o%d %s", i, m.Out[i])
344345
}
345346

@@ -435,7 +436,7 @@ func (m Methods) ToMarshalJSON(receiver, pkgPath string, excludes ...string) (st
435436
// Exclude self
436437
for i, pkgImp := range pkgImports {
437438
if pkgImp == pkgPath {
438-
pkgImports = append(pkgImports[:i], pkgImports[i+1:]...)
439+
pkgImports = slices.Delete(pkgImports, i, i+1)
439440
}
440441
}
441442
}

commands/commandeer.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ type configKey struct {
101101

102102
// This is the root command.
103103
type rootCommand struct {
104-
Printf func(format string, v ...interface{})
105-
Println func(a ...interface{})
104+
Printf func(format string, v ...any)
105+
Println func(a ...any)
106106
StdOut io.Writer
107107
StdErr io.Writer
108108

@@ -431,12 +431,12 @@ func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
431431
// Used by mkcert (server).
432432
log.SetOutput(r.StdOut)
433433

434-
r.Printf = func(format string, v ...interface{}) {
434+
r.Printf = func(format string, v ...any) {
435435
if !r.quiet {
436436
fmt.Fprintf(r.StdOut, format, v...)
437437
}
438438
}
439-
r.Println = func(a ...interface{}) {
439+
r.Println = func(a ...any) {
440440
if !r.quiet {
441441
fmt.Fprintln(r.StdOut, a...)
442442
}

commands/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
9090
os.Stdout.Write(buf.Bytes())
9191
default:
9292
// Decode the JSON to a map[string]interface{} and then unmarshal it again to the correct format.
93-
var m map[string]interface{}
93+
var m map[string]any
9494
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
9595
return err
9696
}

commands/gen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ url: %s
222222
}
223223

224224
// Decode the JSON to a map[string]interface{} and then unmarshal it again to the correct format.
225-
var m map[string]interface{}
225+
var m map[string]any
226226
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
227227
return err
228228
}

commands/server.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import (
6565
"github.com/spf13/fsync"
6666
"golang.org/x/sync/errgroup"
6767
"golang.org/x/sync/semaphore"
68+
"maps"
6869
)
6970

7071
var (
@@ -195,9 +196,7 @@ func (f *fileChangeDetector) PrepareNew() {
195196
}
196197

197198
f.prev = make(map[string]uint64)
198-
for k, v := range f.current {
199-
f.prev[k] = v
200-
}
199+
maps.Copy(f.prev, f.current)
201200
f.current = make(map[string]uint64)
202201
}
203202

@@ -759,7 +758,7 @@ func (c *serverCommand) createServerPorts(cd *simplecobra.Commandeer) error {
759758
c.serverPorts = make([]serverPortListener, len(conf.configs.Languages))
760759
}
761760
currentServerPort := c.serverPort
762-
for i := 0; i < len(c.serverPorts); i++ {
761+
for i := range c.serverPorts {
763762
l, err := net.Listen("tcp", net.JoinHostPort(c.serverInterface, strconv.Itoa(currentServerPort)))
764763
if err == nil {
765764
c.serverPorts[i] = serverPortListener{ln: l, p: currentServerPort}

common/collections/append.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func appendToInterfaceSliceFromValues(slice1, slice2 reflect.Value) ([]any, erro
117117
tos = append(tos, nil)
118118
continue
119119
}
120-
for i := 0; i < slice.Len(); i++ {
120+
for i := range slice.Len() {
121121
tos = append(tos, slice.Index(i).Interface())
122122
}
123123
}
@@ -128,7 +128,7 @@ func appendToInterfaceSliceFromValues(slice1, slice2 reflect.Value) ([]any, erro
128128
func appendToInterfaceSlice(tov reflect.Value, from ...any) ([]any, error) {
129129
var tos []any
130130

131-
for i := 0; i < tov.Len(); i++ {
131+
for i := range tov.Len() {
132132
tos = append(tos, tov.Index(i).Interface())
133133
}
134134

common/collections/stack.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package collections
1515

16+
import "slices"
17+
1618
import "sync"
1719

1820
// Stack is a simple LIFO stack that is safe for concurrent use.
@@ -73,7 +75,7 @@ func (s *Stack[T]) DrainMatching(predicate func(T) bool) []T {
7375
for i := len(s.items) - 1; i >= 0; i-- {
7476
if predicate(s.items[i]) {
7577
items = append(items, s.items[i])
76-
s.items = append(s.items[:i], s.items[i+1:]...)
78+
s.items = slices.Delete(s.items, i, i+1)
7779
}
7880
}
7981
return items

common/hashing/hashing_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ func TestXxHashFromReaderPara(t *testing.T) {
3737
c := qt.New(t)
3838

3939
var wg sync.WaitGroup
40-
for i := 0; i < 10; i++ {
40+
for i := range 10 {
4141
i := i
4242
wg.Add(1)
4343
go func() {
4444
defer wg.Done()
45-
for j := 0; j < 100; j++ {
45+
for j := range 100 {
4646
s := strings.Repeat("Hello ", i+j+1*42)
4747
r := strings.NewReader(s)
4848
got, size, err := XXHashFromReader(r)
@@ -144,8 +144,8 @@ func BenchmarkHashString(b *testing.B) {
144144
}
145145

146146
func BenchmarkHashMap(b *testing.B) {
147-
m := map[string]interface{}{}
148-
for i := 0; i < 1000; i++ {
147+
m := map[string]any{}
148+
for i := range 1000 {
149149
m[fmt.Sprintf("key%d", i)] = i
150150
}
151151

common/herrors/error_locator.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,15 @@ func locateError(r io.Reader, le FileError, matches LineMatcherFn) *ErrorContext
152152
}
153153

154154
if ectx.Position.LineNumber > 0 {
155-
low := ectx.Position.LineNumber - 3
156-
if low < 0 {
157-
low = 0
158-
}
155+
low := max(ectx.Position.LineNumber-3, 0)
159156

160157
if ectx.Position.LineNumber > 2 {
161158
ectx.LinesPos = 2
162159
} else {
163160
ectx.LinesPos = ectx.Position.LineNumber - 1
164161
}
165162

166-
high := ectx.Position.LineNumber + 2
167-
if high > len(lines) {
168-
high = len(lines)
169-
}
163+
high := min(ectx.Position.LineNumber+2, len(lines))
170164

171165
ectx.Lines = lines[low:high]
172166

common/hreflect/helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func ToSliceAny(v any) ([]any, bool) {
245245
vvv := reflect.ValueOf(v)
246246
if vvv.Kind() == reflect.Slice {
247247
out := make([]any, vvv.Len())
248-
for i := 0; i < vvv.Len(); i++ {
248+
for i := range vvv.Len() {
249249
out[i] = vvv.Index(i).Interface()
250250
}
251251
return out, true

common/hstrings/strings.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"sync"
2121

2222
"github.com/gohugoio/hugo/compare"
23+
"slices"
2324
)
2425

2526
var _ compare.Eqer = StringEqualFold("")
@@ -50,12 +51,7 @@ func (s StringEqualFold) Eq(s2 any) bool {
5051

5152
// EqualAny returns whether a string is equal to any of the given strings.
5253
func EqualAny(a string, b ...string) bool {
53-
for _, s := range b {
54-
if a == s {
55-
return true
56-
}
57-
}
58-
return false
54+
return slices.Contains(b, a)
5955
}
6056

6157
// regexpCache represents a cache of regexp objects protected by a mutex.
@@ -103,12 +99,7 @@ func GetOrCompileRegexp(pattern string) (re *regexp.Regexp, err error) {
10399
// InSlice checks if a string is an element of a slice of strings
104100
// and returns a boolean value.
105101
func InSlice(arr []string, el string) bool {
106-
for _, v := range arr {
107-
if v == el {
108-
return true
109-
}
110-
}
111-
return false
102+
return slices.Contains(arr, el)
112103
}
113104

114105
// InSlicEqualFold checks if a string is an element of a slice of strings

common/hugio/hasBytesWriter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestHasBytesWriter(t *testing.T) {
4646
return strings.Repeat("ab cfo", r.Intn(33))
4747
}
4848

49-
for i := 0; i < 22; i++ {
49+
for range 22 {
5050
h, w := neww()
5151
fmt.Fprint(w, rndStr()+"abc __foobar"+rndStr())
5252
c.Assert(h.Patterns[0].Match, qt.Equals, true)

common/hugo/hugo.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,7 @@ func Deprecate(item, alternative string, version string) {
416416

417417
// DeprecateLevelMin informs about a deprecation starting at the given version, but with a minimum log level.
418418
func DeprecateLevelMin(item, alternative string, version string, minLevel logg.Level) {
419-
level := deprecationLogLevelFromVersion(version)
420-
if level < minLevel {
421-
level = minLevel
422-
}
419+
level := max(deprecationLogLevelFromVersion(version), minLevel)
423420
DeprecateLevel(item, alternative, version, level)
424421
}
425422

common/loggers/logger_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestLogDistinct(t *testing.T) {
3737

3838
l := loggers.New(opts)
3939

40-
for i := 0; i < 10; i++ {
40+
for range 10 {
4141
l.Errorln("error 1")
4242
l.Errorln("error 2")
4343
l.Warnln("warn 1")
@@ -137,7 +137,7 @@ func TestReset(t *testing.T) {
137137

138138
l := loggers.New(opts)
139139

140-
for i := 0; i < 3; i++ {
140+
for range 3 {
141141
l.Errorln("error 1")
142142
l.Errorln("error 2")
143143
l.Errorln("error 1")

0 commit comments

Comments
 (0)