Skip to content

Commit

Permalink
all: Run modernize -fix ./...
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 26, 2025
1 parent b7ae24b commit 521911a
Show file tree
Hide file tree
Showing 141 changed files with 302 additions and 354 deletions.
8 changes: 4 additions & 4 deletions cache/dynacache/dynacache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,16 @@ func TestPanicInCreate(t *testing.T) {
return err
}

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

// Test the same keys again without the panic.
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
for i := range 3 {
for range 3 {
v, err := p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
return testItem{
name: key,
Expand Down
6 changes: 3 additions & 3 deletions cache/filecache/filecache_pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dir = ":resourceDir/_gen"
caches, err := filecache.NewCaches(p)
c.Assert(err, qt.IsNil)
cache := caches[name]
for i := 0; i < 10; i++ {
for i := range 10 {
id := fmt.Sprintf("i%d", i)
cache.GetOrCreateBytes(id, func() ([]byte, error) {
return []byte("abc"), nil
Expand All @@ -74,7 +74,7 @@ dir = ":resourceDir/_gen"
c.Assert(err, qt.IsNil)
c.Assert(count, qt.Equals, 5, msg)

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

// Now only the i5 should be left.
for i := 0; i < 10; i++ {
for i := range 10 {
id := fmt.Sprintf("i%d", i)
v := cache.GetString(id)
if i != 5 {
Expand Down
6 changes: 3 additions & 3 deletions cache/filecache/filecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ dir = ":cacheDir/c"
}

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

var wg sync.WaitGroup

for i := 0; i < 50; i++ {
for i := range 50 {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := 0; j < 20; j++ {
for range 20 {
ca := caches.Get(cacheName)
c.Assert(ca, qt.Not(qt.IsNil))
filename, data := filenameData(i)
Expand Down
15 changes: 8 additions & 7 deletions codegen/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"slices"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -102,7 +103,7 @@ func (c *Inspector) MethodsFromTypes(include []reflect.Type, exclude []reflect.T
}

for _, t := range include {
for i := 0; i < t.NumMethod(); i++ {
for i := range t.NumMethod() {

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

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

for i := 0; i < numIn; i++ {
for i := range numIn {
in := m.Type.In(i)

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

if numOut > 0 {
for i := 0; i < numOut; i++ {
for i := range numOut {
out := m.Type.Out(i)
name, pkg := nameAndPackage(out)

Expand Down Expand Up @@ -304,7 +305,7 @@ func (m Method) inOutStr() string {
}

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

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

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

Expand Down Expand Up @@ -435,7 +436,7 @@ func (m Methods) ToMarshalJSON(receiver, pkgPath string, excludes ...string) (st
// Exclude self
for i, pkgImp := range pkgImports {
if pkgImp == pkgPath {
pkgImports = append(pkgImports[:i], pkgImports[i+1:]...)
pkgImports = slices.Delete(pkgImports, i, i+1)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ type configKey struct {

// This is the root command.
type rootCommand struct {
Printf func(format string, v ...interface{})
Println func(a ...interface{})
Printf func(format string, v ...any)
Println func(a ...any)
StdOut io.Writer
StdErr io.Writer

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

r.Printf = func(format string, v ...interface{}) {
r.Printf = func(format string, v ...any) {
if !r.quiet {
fmt.Fprintf(r.StdOut, format, v...)
}
}
r.Println = func(a ...interface{}) {
r.Println = func(a ...any) {
if !r.quiet {
fmt.Fprintln(r.StdOut, a...)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
os.Stdout.Write(buf.Bytes())
default:
// Decode the JSON to a map[string]interface{} and then unmarshal it again to the correct format.
var m map[string]interface{}
var m map[string]any
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ url: %s
}

// Decode the JSON to a map[string]interface{} and then unmarshal it again to the correct format.
var m map[string]interface{}
var m map[string]any
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
"github.com/spf13/fsync"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
"maps"
)

var (
Expand Down Expand Up @@ -195,9 +196,7 @@ func (f *fileChangeDetector) PrepareNew() {
}

f.prev = make(map[string]uint64)
for k, v := range f.current {
f.prev[k] = v
}
maps.Copy(f.prev, f.current)
f.current = make(map[string]uint64)
}

Expand Down Expand Up @@ -759,7 +758,7 @@ func (c *serverCommand) createServerPorts(cd *simplecobra.Commandeer) error {
c.serverPorts = make([]serverPortListener, len(conf.configs.Languages))
}
currentServerPort := c.serverPort
for i := 0; i < len(c.serverPorts); i++ {
for i := range c.serverPorts {
l, err := net.Listen("tcp", net.JoinHostPort(c.serverInterface, strconv.Itoa(currentServerPort)))
if err == nil {
c.serverPorts[i] = serverPortListener{ln: l, p: currentServerPort}
Expand Down
4 changes: 2 additions & 2 deletions common/collections/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func appendToInterfaceSliceFromValues(slice1, slice2 reflect.Value) ([]any, erro
tos = append(tos, nil)
continue
}
for i := 0; i < slice.Len(); i++ {
for i := range slice.Len() {
tos = append(tos, slice.Index(i).Interface())
}
}
Expand All @@ -128,7 +128,7 @@ func appendToInterfaceSliceFromValues(slice1, slice2 reflect.Value) ([]any, erro
func appendToInterfaceSlice(tov reflect.Value, from ...any) ([]any, error) {
var tos []any

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

Expand Down
4 changes: 3 additions & 1 deletion common/collections/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package collections

import "slices"

import "sync"

// Stack is a simple LIFO stack that is safe for concurrent use.
Expand Down Expand Up @@ -73,7 +75,7 @@ func (s *Stack[T]) DrainMatching(predicate func(T) bool) []T {
for i := len(s.items) - 1; i >= 0; i-- {
if predicate(s.items[i]) {
items = append(items, s.items[i])
s.items = append(s.items[:i], s.items[i+1:]...)
s.items = slices.Delete(s.items, i, i+1)
}
}
return items
Expand Down
8 changes: 4 additions & 4 deletions common/hashing/hashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func TestXxHashFromReaderPara(t *testing.T) {
c := qt.New(t)

var wg sync.WaitGroup
for i := 0; i < 10; i++ {
for i := range 10 {
i := i
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 100; j++ {
for j := range 100 {
s := strings.Repeat("Hello ", i+j+1*42)
r := strings.NewReader(s)
got, size, err := XXHashFromReader(r)
Expand Down Expand Up @@ -144,8 +144,8 @@ func BenchmarkHashString(b *testing.B) {
}

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

Expand Down
10 changes: 2 additions & 8 deletions common/herrors/error_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,15 @@ func locateError(r io.Reader, le FileError, matches LineMatcherFn) *ErrorContext
}

if ectx.Position.LineNumber > 0 {
low := ectx.Position.LineNumber - 3
if low < 0 {
low = 0
}
low := max(ectx.Position.LineNumber-3, 0)

if ectx.Position.LineNumber > 2 {
ectx.LinesPos = 2
} else {
ectx.LinesPos = ectx.Position.LineNumber - 1
}

high := ectx.Position.LineNumber + 2
if high > len(lines) {
high = len(lines)
}
high := min(ectx.Position.LineNumber+2, len(lines))

ectx.Lines = lines[low:high]

Expand Down
2 changes: 1 addition & 1 deletion common/hreflect/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func ToSliceAny(v any) ([]any, bool) {
vvv := reflect.ValueOf(v)
if vvv.Kind() == reflect.Slice {
out := make([]any, vvv.Len())
for i := 0; i < vvv.Len(); i++ {
for i := range vvv.Len() {
out[i] = vvv.Index(i).Interface()
}
return out, true
Expand Down
15 changes: 3 additions & 12 deletions common/hstrings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"

"github.com/gohugoio/hugo/compare"
"slices"
)

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

// EqualAny returns whether a string is equal to any of the given strings.
func EqualAny(a string, b ...string) bool {
for _, s := range b {
if a == s {
return true
}
}
return false
return slices.Contains(b, a)
}

// regexpCache represents a cache of regexp objects protected by a mutex.
Expand Down Expand Up @@ -103,12 +99,7 @@ func GetOrCompileRegexp(pattern string) (re *regexp.Regexp, err error) {
// InSlice checks if a string is an element of a slice of strings
// and returns a boolean value.
func InSlice(arr []string, el string) bool {
for _, v := range arr {
if v == el {
return true
}
}
return false
return slices.Contains(arr, el)
}

// InSlicEqualFold checks if a string is an element of a slice of strings
Expand Down
2 changes: 1 addition & 1 deletion common/hugio/hasBytesWriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestHasBytesWriter(t *testing.T) {
return strings.Repeat("ab cfo", r.Intn(33))
}

for i := 0; i < 22; i++ {
for range 22 {
h, w := neww()
fmt.Fprint(w, rndStr()+"abc __foobar"+rndStr())
c.Assert(h.Patterns[0].Match, qt.Equals, true)
Expand Down
5 changes: 1 addition & 4 deletions common/hugo/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,7 @@ func Deprecate(item, alternative string, version string) {

// DeprecateLevelMin informs about a deprecation starting at the given version, but with a minimum log level.
func DeprecateLevelMin(item, alternative string, version string, minLevel logg.Level) {
level := deprecationLogLevelFromVersion(version)
if level < minLevel {
level = minLevel
}
level := max(deprecationLogLevelFromVersion(version), minLevel)
DeprecateLevel(item, alternative, version, level)
}

Expand Down
4 changes: 2 additions & 2 deletions common/loggers/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestLogDistinct(t *testing.T) {

l := loggers.New(opts)

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

l := loggers.New(opts)

for i := 0; i < 3; i++ {
for range 3 {
l.Errorln("error 1")
l.Errorln("error 2")
l.Errorln("error 1")
Expand Down
Loading

0 comments on commit 521911a

Please sign in to comment.