Skip to content

Commit 61443b1

Browse files
modernize for loops
1 parent a51cd00 commit 61443b1

File tree

11 files changed

+23
-30
lines changed

11 files changed

+23
-30
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ jobs:
3333
- name: golangci-lint
3434
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # tag=v8.0.0
3535
with:
36-
version: v2.5.0
36+
version: v2.6.0
3737
args: --output.text.print-linter-name=true --output.text.colors=true --timeout 10m
3838
working-directory: ${{matrix.working-directory}}

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ linters:
3232
- iotamixing
3333
- makezero
3434
- misspell
35+
- modernize
3536
- nakedret
3637
- nilerr
3738
- nolintlint

pkg/crd/known_types.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package crd
1818

1919
import (
20+
"maps"
2021
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2122
"k8s.io/utils/ptr"
2223
"sigs.k8s.io/controller-tools/pkg/loader"
@@ -156,13 +157,9 @@ func AddKnownTypes(parser *Parser) {
156157
// ensure everything is there before adding to PackageOverrides
157158
// TODO(directxman12): this is a bit of a hack, maybe just use constructors?
158159
parser.init()
159-
for pkgName, override := range KnownPackages {
160-
parser.PackageOverrides[pkgName] = override
161-
}
160+
maps.Copy(parser.PackageOverrides, KnownPackages)
162161
// if we want to generate the embedded ObjectMeta in the CRD we need to add the ObjectMetaPackages
163162
if parser.GenerateEmbeddedObjectMeta {
164-
for pkgName, override := range ObjectMetaPackages {
165-
parser.PackageOverrides[pkgName] = override
166-
}
163+
maps.Copy(parser.PackageOverrides, ObjectMetaPackages)
167164
}
168165
}

pkg/deepcopy/traverse.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@ func (c *copyMethodMaker) genSliceDeepCopy(actualName *namingInfo, sliceType *ty
447447
func (c *copyMethodMaker) genStructDeepCopy(_ *namingInfo, structType *types.Struct) {
448448
c.Line("*out = *in")
449449

450-
for i := 0; i < structType.NumFields(); i++ {
451-
field := structType.Field(i)
450+
for field := range structType.Fields() {
452451

453452
// if we have a manual deepcopy, use that
454453
hasDeepCopy, copyOnPtr := hasDeepCopyMethod(c.pkg, field.Type())
@@ -769,8 +768,7 @@ func fineToShallowCopy(typeInfo types.Type) bool {
769768
return fineToShallowCopy(typeInfo.Underlying())
770769
case *types.Struct:
771770
// structs are fine to shallow-copy if they have all shallow-copyable fields
772-
for i := 0; i < typeInfo.NumFields(); i++ {
773-
field := typeInfo.Field(i)
771+
for field := range typeInfo.Fields() {
774772
if !fineToShallowCopy(field.Type()) {
775773
return false
776774
}

pkg/genall/help/pretty/print.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func writePadding(out io.Writer, typ []byte, amt int) error {
291291

292292
num := amt / len(typ)
293293
rem := amt % len(typ)
294-
for i := 0; i < num; i++ {
294+
for range num {
295295
if _, err := out.Write(typ); err != nil {
296296
return err
297297
}

pkg/genall/options.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package genall
1818

1919
import (
2020
"fmt"
21+
"maps"
2122
"strings"
2223

2324
"golang.org/x/tools/go/packages"
@@ -100,9 +101,7 @@ func FromOptionsWithConfig(cfg *packages.Config, optionsRegistry *markers.Regist
100101
}
101102

102103
outRules := DirectoryPerGenerator("config", protoRt.GeneratorsByName)
103-
for gen, rule := range protoRt.OutputRules.ByGenerator {
104-
outRules.ByGenerator[gen] = rule
105-
}
104+
maps.Copy(outRules.ByGenerator, protoRt.OutputRules.ByGenerator)
106105

107106
genRuntime.OutputRules = outRules
108107
return genRuntime, nil

pkg/markers/collect.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ type Collector struct {
3636
}
3737

3838
// MarkerValues are all the values for some set of markers.
39-
type MarkerValues map[string][]interface{}
39+
type MarkerValues map[string][]any
4040

4141
// Get fetches the first value that for the given marker, returning
4242
// nil if no values are available.
43-
func (v MarkerValues) Get(name string) interface{} {
43+
func (v MarkerValues) Get(name string) any {
4444
vals := v[name]
4545
if len(vals) == 0 {
4646
return nil
@@ -109,7 +109,7 @@ func (c *Collector) parseMarkersInPackage(nodeMarkersRaw map[ast.Node][]markerCo
109109
default:
110110
target = DescribesType
111111
}
112-
markerVals := make(map[string][]interface{})
112+
markerVals := make(map[string][]any)
113113
for _, markerRaw := range markersRaw {
114114
markerText := markerRaw.Text()
115115
def := c.Registry.Lookup(markerText, target)

pkg/markers/parse.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func peekNoSpace(scanner *sc.Scanner) rune {
5050

5151
var (
5252
// interfaceType is a pre-computed reflect.Type representing the empty interface.
53-
interfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
53+
interfaceType = reflect.TypeOf((*any)(nil)).Elem()
5454
rawArgsType = reflect.TypeOf((*RawArguments)(nil)).Elem()
5555
)
5656

@@ -183,13 +183,13 @@ func makeSliceType(itemType Argument) (reflect.Type, error) {
183183
var itemReflectedType reflect.Type
184184
switch itemType.Type {
185185
case IntType:
186-
itemReflectedType = reflect.TypeOf(int(0))
186+
itemReflectedType = reflect.TypeFor[int]()
187187
case NumberType:
188-
itemReflectedType = reflect.TypeOf(float64(0))
188+
itemReflectedType = reflect.TypeFor[float64]()
189189
case StringType:
190-
itemReflectedType = reflect.TypeOf("")
190+
itemReflectedType = reflect.TypeFor[string]()
191191
case BoolType:
192-
itemReflectedType = reflect.TypeOf(false)
192+
itemReflectedType = reflect.TypeFor[bool]()
193193
case SliceType:
194194
subItemType, err := makeSliceType(*itemType.ItemType)
195195
if err != nil {

pkg/rbac/parser.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package rbac
2424

2525
import (
2626
"fmt"
27+
"slices"
2728
"sort"
2829
"strings"
2930

@@ -308,11 +309,8 @@ func GenerateRoles(ctx *genall.GenerationContext, roleName string) ([]interface{
308309

309310
// Normalize rule verbs to "*" if any verb in the rule is an asterisk
310311
for _, rule := range ruleMap {
311-
for _, verb := range rule.Verbs {
312-
if verb == "*" {
313-
rule.Verbs = []string{"*"}
314-
break
315-
}
312+
if slices.Contains(rule.Verbs, "*") {
313+
rule.Verbs = []string{"*"}
316314
}
317315
}
318316
var policyRules []rbacv1.PolicyRule

pkg/rbac/parser_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
var _ = Describe("ClusterRole generated by the RBAC Generator", func() {
2020
// run this test multiple times to make sure the Rule order is stable.
2121
const stableTestCount = 5
22-
for i := 0; i < stableTestCount; i++ {
22+
for range stableTestCount {
2323
It("should match the expected result", func() {
2424
By("switching into testdata to appease go modules")
2525
cwd, err := os.Getwd()

0 commit comments

Comments
 (0)