all: Run modernize -fix ./...

This commit is contained in:
Bjørn Erik Pedersen
2025-02-26 10:15:04 +01:00
parent b7ae24b9c2
commit 521911a576
141 changed files with 302 additions and 354 deletions

View File

@@ -48,7 +48,7 @@ func (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any
switch seqv.Kind() {
case reflect.Array, reflect.Slice:
r := make([]any, seqv.Len())
for i := 0; i < seqv.Len(); i++ {
for i := range seqv.Len() {
vv := seqv.Index(i)
vvv, err := applyFnToThis(ctx, fnv, vv, args...)
@@ -91,7 +91,7 @@ func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (re
return reflect.ValueOf(nil), errors.New("Too many arguments")
}*/
for i := 0; i < num; i++ {
for i := range num {
// AssignableTo reports whether xt is assignable to type targ.
if xt, targ := n[i].Type(), fn.Type().In(i); !xt.AssignableTo(targ) {
return reflect.ValueOf(nil), errors.New("called apply using " + xt.String() + " as type " + targ.String())

View File

@@ -125,7 +125,7 @@ func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (stri
lv = reflect.ValueOf(sortSeq)
fallthrough
case reflect.Array, reflect.Slice, reflect.String:
for i := 0; i < lv.Len(); i++ {
for i := range lv.Len() {
val := lv.Index(i).Interface()
valStr, err := cast.ToStringE(val)
if err != nil {
@@ -165,7 +165,7 @@ func (ns *Namespace) Dictionary(values ...any) (map[string]any, error) {
case string:
key = v
case []string:
for i := 0; i < len(v)-1; i++ {
for i := range len(v) - 1 {
key = v[i]
var m map[string]any
v, found := dict[key]
@@ -235,7 +235,7 @@ func (ns *Namespace) In(l any, v any) (bool, error) {
switch lv.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < lv.Len(); i++ {
for i := range lv.Len() {
lvv, isNil := indirectInterface(lv.Index(i))
if isNil {
continue
@@ -277,13 +277,13 @@ func (ns *Namespace) Intersect(l1, l2 any) (any, error) {
ins = &intersector{r: reflect.MakeSlice(l1v.Type(), 0, 0), seen: make(map[any]bool)}
switch l2v.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < l1v.Len(); i++ {
for i := range l1v.Len() {
l1vv := l1v.Index(i)
if !l1vv.Type().Comparable() {
return make([]any, 0), errors.New("intersect does not support slices or arrays of uncomparable types")
}
for j := 0; j < l2v.Len(); j++ {
for j := range l2v.Len() {
l2vv := l2v.Index(j)
if !l2vv.Type().Comparable() {
return make([]any, 0), errors.New("intersect does not support slices or arrays of uncomparable types")
@@ -590,7 +590,7 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) {
isNil bool
)
for i := 0; i < l1v.Len(); i++ {
for i := range l1v.Len() {
l1vv, isNil = indirectInterface(l1v.Index(i))
if !l1vv.Type().Comparable() {
@@ -610,7 +610,7 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) {
}
}
for j := 0; j < l2v.Len(); j++ {
for j := range l2v.Len() {
l2vv := l2v.Index(j)
switch kind := l1vv.Kind(); {
@@ -661,7 +661,7 @@ func (ns *Namespace) Uniq(l any) (any, error) {
seen := make(map[any]bool)
for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
ev, _ := indirectInterface(v.Index(i))
key := normalize(ev)

View File

@@ -52,7 +52,7 @@ Desc: {{ sort (sort $values "b" "desc") "a" "desc" }}
`
for i := 0; i < 4; i++ {
for range 4 {
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
@@ -122,7 +122,7 @@ func TestAppendNilsToSliceWithNils(t *testing.T) {
`
for i := 0; i < 4; i++ {
for range 4 {
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{

View File

@@ -856,7 +856,7 @@ func ToTstXIs(slice any) []TstXI {
}
tis := make([]TstXI, s.Len())
for i := 0; i < s.Len(); i++ {
for i := range s.Len() {
tsti, ok := s.Index(i).Interface().(TstXI)
if !ok {
return nil

View File

@@ -44,7 +44,7 @@ func (ns *Namespace) Complement(ls ...any) (any, error) {
switch v.Kind() {
case reflect.Array, reflect.Slice:
sl := reflect.MakeSlice(v.Type(), 0, 0)
for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
ev, _ := indirectInterface(v.Index(i))
if _, found := aset[normalize(ev)]; !found {
sl = reflect.Append(sl, ev)

View File

@@ -52,7 +52,7 @@ func (ns *Namespace) doIndex(item any, args ...any) (any, error) {
if len(args) == 1 {
v := reflect.ValueOf(args[0])
if v.Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
indices = append(indices, v.Index(i).Interface())
}
} else {

View File

@@ -74,7 +74,7 @@ func collectIdentities(seqs ...any) (map[any]bool, error) {
v := reflect.ValueOf(seq)
switch v.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
ev, _ := indirectInterface(v.Index(i))
if !ev.Type().Comparable() {

View File

@@ -73,7 +73,7 @@ func (ns *Namespace) Sort(ctx context.Context, l any, args ...any) (any, error)
switch seqv.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < seqv.Len(); i++ {
for i := range seqv.Len() {
p.Pairs[i].Value = seqv.Index(i)
if sortByField == "" || sortByField == "value" {
p.Pairs[i].Key = p.Pairs[i].Value

View File

@@ -44,7 +44,7 @@ func (ns *Namespace) SymDiff(s2, s1 any) (any, error) {
slice = reflect.MakeSlice(sliceType, 0, 0)
}
for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
ev, _ := indirectInterface(v.Index(i))
key := normalize(ev)

View File

@@ -148,7 +148,7 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
iv := v.Int()
ivp = &iv
for i := 0; i < mv.Len(); i++ {
for i := range mv.Len() {
if anInt, err := toInt(mv.Index(i)); err == nil {
ima = append(ima, anInt)
}
@@ -156,7 +156,7 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
case reflect.String:
sv := v.String()
svp = &sv
for i := 0; i < mv.Len(); i++ {
for i := range mv.Len() {
if aString, err := toString(mv.Index(i)); err == nil {
sma = append(sma, aString)
}
@@ -164,7 +164,7 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
case reflect.Float64:
fv := v.Float()
fvp = &fv
for i := 0; i < mv.Len(); i++ {
for i := range mv.Len() {
if aFloat, err := toFloat(mv.Index(i)); err == nil {
fma = append(fma, aFloat)
}
@@ -173,7 +173,7 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
if hreflect.IsTime(v.Type()) {
iv := ns.toTimeUnix(v)
ivp = &iv
for i := 0; i < mv.Len(); i++ {
for i := range mv.Len() {
ima = append(ima, ns.toTimeUnix(mv.Index(i)))
}
}
@@ -397,7 +397,7 @@ func parseWhereArgs(args ...any) (mv reflect.Value, op string, err error) {
func (ns *Namespace) checkWhereArray(ctxv, seqv, kv, mv reflect.Value, path []string, op string) (any, error) {
rv := reflect.MakeSlice(seqv.Type(), 0, 0)
for i := 0; i < seqv.Len(); i++ {
for i := range seqv.Len() {
var vvv reflect.Value
rvv := seqv.Index(i)

View File

@@ -865,10 +865,10 @@ func BenchmarkWhereOps(b *testing.B) {
ns := newNs()
var seq []map[string]string
ctx := context.Background()
for i := 0; i < 500; i++ {
for range 500 {
seq = append(seq, map[string]string{"foo": "bar"})
}
for i := 0; i < 500; i++ {
for range 500 {
seq = append(seq, map[string]string{"foo": "baz"})
}
// Shuffle the sequence.
@@ -907,7 +907,7 @@ func BenchmarkWhereMap(b *testing.B) {
ns := newNs()
seq := map[string]string{}
for i := 0; i < 1000; i++ {
for i := range 1000 {
seq[fmt.Sprintf("key%d", i)] = "value"
}

View File

@@ -36,6 +36,7 @@ import (
"github.com/spf13/cast"
"github.com/gohugoio/hugo/deps"
"slices"
)
// New returns a new instance of the data-namespaced template functions.
@@ -170,12 +171,7 @@ func hasHeaderValue(m http.Header, key, value string) bool {
return false
}
for _, v := range s {
if v == value {
return true
}
}
return false
return slices.Contains(s, value)
}
func hasHeaderKey(m http.Header, key string) bool {

View File

@@ -155,11 +155,11 @@ func TestScpGetRemoteParallel(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 1; i++ {
for i := range 1 {
wg.Add(1)
go func(gor int) {
defer wg.Done()
for j := 0; j < 10; j++ {
for range 10 {
var cb []byte
f := func(b []byte) (bool, error) {
cb = b

View File

@@ -213,7 +213,7 @@ func (t *TemplateFuncsNamespace) toJSON(ctx context.Context) ([]byte, error) {
return nil, nil
}
ctxType := reflect.TypeOf(tctx)
for i := 0; i < ctxType.NumMethod(); i++ {
for i := range ctxType.NumMethod() {
method := ctxType.Method(i)
if ignoreFuncs[method.Name] {
continue

View File

@@ -314,7 +314,7 @@ func (ns *Namespace) toFloatsE(v any) ([]float64, bool, error) {
switch vv.Kind() {
case reflect.Slice, reflect.Array:
var floats []float64
for i := 0; i < vv.Len(); i++ {
for i := range vv.Len() {
f, err := cast.ToFloat64E(vv.Index(i).Interface())
if err != nil {
return nil, true, err

View File

@@ -31,7 +31,7 @@ func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ns := &internal.TemplateFuncsNamespace{
Name: name,
Context: func(ctx context.Context, args ...interface{}) (interface{}, error) {
Context: func(ctx context.Context, args ...any) (any, error) {
v := tpl.Context.Page.Get(ctx)
if v == nil {
// The multilingual sitemap does not have a page as its context.

View File

@@ -87,7 +87,7 @@ func TestDeferRepeatedBuildsEditOutside(t *testing.T) {
b := hugolib.TestRunning(t, deferFilesCommon)
for i := 0; i < 5; i++ {
for i := range 5 {
old := fmt.Sprintf("EDIT_COUNTER_OUTSIDE_%d", i)
new := fmt.Sprintf("EDIT_COUNTER_OUTSIDE_%d", i+1)
b.EditFileReplaceAll("layouts/index.html", old, new).Build()
@@ -100,7 +100,7 @@ func TestDeferRepeatedBuildsEditDefer(t *testing.T) {
b := hugolib.TestRunning(t, deferFilesCommon)
for i := 0; i < 8; i++ {
for i := range 8 {
old := fmt.Sprintf("EDIT_COUNTER_DEFER_%d", i)
new := fmt.Sprintf("EDIT_COUNTER_DEFER_%d", i+1)
b.EditFileReplaceAll("layouts/index.html", old, new).Build()

View File

@@ -431,7 +431,7 @@ func (t *templateHandler) LookupVariants(name string) []tpl.Template {
}
variants := make([]tpl.Template, len(s.variants))
for i := 0; i < len(variants); i++ {
for i := range variants {
variants[i] = s.variants[i].ts
}
@@ -599,7 +599,7 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error
func (t *templateHandler) extractIdentifiers(line string) []string {
m := identifiersRe.FindAllStringSubmatch(line, -1)
identifiers := make([]string, len(m))
for i := 0; i < len(m); i++ {
for i := range m {
identifiers[i] = m[i][1]
}
return identifiers

View File

@@ -27,6 +27,7 @@ import (
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/tpl"
"github.com/mitchellh/mapstructure"
"slices"
)
type templateType int
@@ -187,7 +188,7 @@ func (c *templateContext) applyTransformations(n parse.Node) (bool, error) {
for i, cmd := range x.Cmds {
keep, _ := c.applyTransformations(cmd)
if !keep {
x.Cmds = append(x.Cmds[:i], x.Cmds[i+1:]...)
x.Cmds = slices.Delete(x.Cmds, i, i+1)
}
}
@@ -271,12 +272,7 @@ func (c *templateContext) applyTransformationsToNodes(nodes ...parse.Node) {
}
func (c *templateContext) hasIdent(idents []string, ident string) bool {
for _, id := range idents {
if id == ident {
return true
}
}
return false
return slices.Contains(idents, ident)
}
// collectConfig collects and parses any leading template config variable declaration.

View File

@@ -64,6 +64,7 @@ import (
_ "github.com/gohugoio/hugo/tpl/time"
_ "github.com/gohugoio/hugo/tpl/transform"
_ "github.com/gohugoio/hugo/tpl/urls"
maps0 "maps"
)
var (
@@ -290,9 +291,7 @@ func createFuncMap(d *deps.Deps) map[string]any {
}
if d.OverloadedTemplateFuncs != nil {
for k, v := range d.OverloadedTemplateFuncs {
funcMap[k] = v
}
maps0.Copy(funcMap, d.OverloadedTemplateFuncs)
}
d.TmplFuncMap = funcMap

View File

@@ -192,7 +192,7 @@ func BenchmarkUnmarshalString(b *testing.B) {
const numJsons = 100
var jsons [numJsons]string
for i := 0; i < numJsons; i++ {
for i := range numJsons {
jsons[i] = strings.Replace(testJSON, "ROOT_KEY", fmt.Sprintf("root%d", i), 1)
}
@@ -220,7 +220,7 @@ func BenchmarkUnmarshalResource(b *testing.B) {
const numJsons = 100
var jsons [numJsons]testContentResource
for i := 0; i < numJsons; i++ {
for i := range numJsons {
key := fmt.Sprintf("root%d", i)
jsons[i] = testContentResource{key: key, content: strings.Replace(testJSON, "ROOT_KEY", key, 1), mime: media.Builtin.JSONType}
}