Skip to content

Commit 6453fa4

Browse files
committed
chore: use new language elements
1 parent 46d610a commit 6453fa4

File tree

11 files changed

+56
-92
lines changed

11 files changed

+56
-92
lines changed

pkg/commands/internal/migrate/versionone/output.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ type OutputFormat struct {
2424
type OutputFormats []OutputFormat
2525

2626
func (p *OutputFormats) UnmarshalText(text []byte) error {
27-
formats := strings.Split(string(text), ",")
28-
29-
for _, item := range formats {
27+
for item := range strings.SplitSeq(string(text), ",") {
3028
format, path, _ := strings.Cut(item, ":")
3129

3230
*p = append(*p, OutputFormat{

pkg/goformatters/gci/internal/section/standard_list.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/dogsled/dogsled.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@ func run(pass *analysis.Pass, maxBlanks int) (any, error) {
3030
return nil, nil
3131
}
3232

33-
nodeFilter := []ast.Node{
34-
(*ast.FuncDecl)(nil),
35-
}
36-
37-
insp.Preorder(nodeFilter, func(node ast.Node) {
33+
for node := range insp.PreorderSeq((*ast.FuncDecl)(nil)) {
3834
funcDecl, ok := node.(*ast.FuncDecl)
3935
if !ok {
40-
return
36+
continue
4137
}
4238

4339
if funcDecl.Body == nil {
44-
return
40+
continue
4541
}
4642

4743
for _, expr := range funcDecl.Body.List {
@@ -65,7 +61,7 @@ func run(pass *analysis.Pass, maxBlanks int) (any, error) {
6561
pass.Reportf(assgnStmt.Pos(), "declaration has %v blank identifiers", numBlank)
6662
}
6763
}
68-
})
64+
}
6965

7066
return nil, nil
7167
}

pkg/golinters/gochecknoinits/gochecknoinits.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,17 @@ func run(pass *analysis.Pass) (any, error) {
2828
return nil, nil
2929
}
3030

31-
nodeFilter := []ast.Node{
32-
(*ast.FuncDecl)(nil),
33-
}
34-
35-
insp.Preorder(nodeFilter, func(decl ast.Node) {
36-
funcDecl, ok := decl.(*ast.FuncDecl)
31+
for node := range insp.PreorderSeq((*ast.FuncDecl)(nil)) {
32+
funcDecl, ok := node.(*ast.FuncDecl)
3733
if !ok {
38-
return
34+
continue
3935
}
4036

4137
fnName := funcDecl.Name.Name
4238
if fnName == "init" && funcDecl.Recv.NumFields() == 0 {
4339
pass.Reportf(funcDecl.Pos(), "don't use %s function", internal.FormatCode(fnName))
4440
}
45-
})
41+
}
4642

4743
return nil, nil
4844
}

pkg/goutil/version.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ func CleanRuntimeVersion() (string, error) {
6060
}
6161

6262
func cleanRuntimeVersion(rv string) (string, error) {
63-
parts := strings.Fields(rv)
64-
65-
for _, part := range parts {
63+
for part := range strings.FieldsSeq(rv) {
6664
// Allow to handle:
6765
// - GOEXPERIMENT -> "go1.23.0 X:boringcrypto"
6866
// - devel -> "devel go1.24-e705a2d Wed Aug 7 01:16:42 2024 +0000 linux/amd64"

pkg/logutils/logutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func getEnabledDebugs() map[string]bool {
9494
return ret
9595
}
9696

97-
for _, tag := range strings.Split(debugVar, ",") {
97+
for tag := range strings.SplitSeq(debugVar, ",") {
9898
ret[tag] = true
9999
}
100100

pkg/result/processors/nolint_filter.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,12 @@ func (p *NolintFilter) extractInlineRangeFromComment(text string, g ast.Node, fs
227227
return buildRange(nil) // ignore all linters
228228
}
229229

230+
text, _, _ = strings.Cut(text, "//") // allow another comment after this comment
231+
230232
// ignore specific linters
231233
var linters []string
232-
text = strings.Split(text, "//")[0] // allow another comment after this comment
233-
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
234-
for _, item := range linterItems {
234+
235+
for item := range strings.SplitSeq(strings.TrimPrefix(text, "nolint:"), ",") {
235236
linterName := strings.ToLower(strings.TrimSpace(item))
236237
if linterName == "all" {
237238
p.unknownLintersSet = map[string]bool{}

scripts/gen_github_action_config/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,9 @@ func parseVersion(s string) (*version, error) {
219219
return nil, fmt.Errorf("version %q should start with %q", s, vPrefix)
220220
}
221221

222-
parts := strings.Split(strings.TrimPrefix(s, vPrefix), ".")
223-
224222
var nums []int
225-
for _, part := range parts {
223+
224+
for part := range strings.SplitSeq(strings.TrimPrefix(s, vPrefix), ".") {
226225
num, err := strconv.Atoi(part)
227226
if err != nil {
228227
return nil, fmt.Errorf("failed to parse version %q: %w", s, err)

scripts/website/expand_templates/changelog.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bufio"
54
"bytes"
65
"os"
76
"path/filepath"
@@ -24,13 +23,7 @@ func copyChangelog(dir string) error {
2423

2524
var write bool
2625

27-
// TODO(ldez): use bytes.Lines when min go1.24 (and remove the new line)
28-
scanner := bufio.NewScanner(bytes.NewBuffer(bytes.ReplaceAll(in, []byte("### "), []byte("## "))))
29-
scanner.Split(bufio.ScanLines)
30-
31-
for scanner.Scan() {
32-
line := scanner.Bytes()
33-
26+
for line := range bytes.Lines(bytes.ReplaceAll(in, []byte("### "), []byte("## "))) {
3427
if bytes.Equal(bytes.TrimSpace(line), []byte(marker)) {
3528
write = true
3629
continue
@@ -40,8 +33,6 @@ func copyChangelog(dir string) error {
4033
continue
4134
}
4235

43-
line = append(line, '\n')
44-
4536
_, err = out.Write(line)
4637
if err != nil {
4738
return err

test/bench/bench_test.go

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ type metrics struct {
3636
}
3737

3838
func Benchmark_linters(b *testing.B) {
39-
savedWD, err := os.Getwd()
40-
require.NoError(b, err)
41-
42-
b.Cleanup(func() {
43-
// Restore WD to avoid side effects when during all the benchmarks.
44-
err = os.Chdir(savedWD)
45-
require.NoError(b, err)
46-
})
47-
4839
installGolangCILint(b)
4940

5041
repos := getAllRepositories(b)
@@ -67,8 +58,7 @@ func Benchmark_linters(b *testing.B) {
6758
// TODO(ldez): clean inside go1.25 PR
6859
_ = exec.CommandContext(context.Background(), binName, "cache", "clean").Run()
6960

70-
err = os.Chdir(repo.dir)
71-
require.NoErrorf(b, err, "can't chdir to %s", repo.dir)
61+
b.Chdir(repo.dir)
7262

7363
lc := countGoLines(b)
7464

@@ -85,15 +75,6 @@ func Benchmark_linters(b *testing.B) {
8575
}
8676

8777
func Benchmark_golangciLint(b *testing.B) {
88-
savedWD, err := os.Getwd()
89-
require.NoError(b, err)
90-
91-
b.Cleanup(func() {
92-
// Restore WD to avoid side effects when during all the benchmarks.
93-
err = os.Chdir(savedWD)
94-
require.NoError(b, err)
95-
})
96-
9778
installGolangCILint(b)
9879

9980
// TODO(ldez): clean inside go1.25 PR
@@ -117,8 +98,7 @@ func Benchmark_golangciLint(b *testing.B) {
11798

11899
for _, c := range cases {
119100
b.Run(c.name, func(b *testing.B) {
120-
err = os.Chdir(c.dir)
121-
require.NoErrorf(b, err, "can't chdir to %s", c.dir)
101+
b.Chdir(c.dir)
122102

123103
lc := countGoLines(b)
124104

@@ -145,26 +125,26 @@ func getAllRepositories(tb testing.TB) []repo {
145125
name: "golangci/golangci-lint",
146126
dir: cloneGithubProject(tb, benchRoot, "golangci", "golangci-lint"),
147127
},
148-
{
149-
name: "goreleaser/goreleaser",
150-
dir: cloneGithubProject(tb, benchRoot, "goreleaser", "goreleaser"),
151-
},
152-
{
153-
name: "gohugoio/hugo",
154-
dir: cloneGithubProject(tb, benchRoot, "gohugoio", "hugo"),
155-
},
156-
{
157-
name: "pact-foundation/pact-go", // CGO inside
158-
dir: cloneGithubProject(tb, benchRoot, "pact-foundation", "pact-go"),
159-
},
160-
{
161-
name: "kubernetes/kubernetes",
162-
dir: cloneGithubProject(tb, benchRoot, "kubernetes", "kubernetes"),
163-
},
164-
{
165-
name: "moby/buildkit",
166-
dir: cloneGithubProject(tb, benchRoot, "moby", "buildkit"),
167-
},
128+
// {
129+
// name: "goreleaser/goreleaser",
130+
// dir: cloneGithubProject(tb, benchRoot, "goreleaser", "goreleaser"),
131+
// },
132+
// {
133+
// name: "gohugoio/hugo",
134+
// dir: cloneGithubProject(tb, benchRoot, "gohugoio", "hugo"),
135+
// },
136+
// {
137+
// name: "pact-foundation/pact-go", // CGO inside
138+
// dir: cloneGithubProject(tb, benchRoot, "pact-foundation", "pact-go"),
139+
// },
140+
// {
141+
// name: "kubernetes/kubernetes",
142+
// dir: cloneGithubProject(tb, benchRoot, "kubernetes", "kubernetes"),
143+
// },
144+
// {
145+
// name: "moby/buildkit",
146+
// dir: cloneGithubProject(tb, benchRoot, "moby", "buildkit"),
147+
// },
168148
{
169149
name: "go source code",
170150
dir: filepath.Join(build.Default.GOROOT, "src"),
@@ -242,9 +222,9 @@ func countGoLines(tb testing.TB) int {
242222
tb.Fatalf("can't run go lines counter: %s", err)
243223
}
244224

245-
parts := bytes.Split(bytes.TrimSpace(out), []byte(" "))
225+
lineCount, _, _ := bytes.Cut(bytes.TrimSpace(out), []byte(" "))
246226

247-
n, err := strconv.Atoi(string(parts[0]))
227+
n, err := strconv.Atoi(string(lineCount))
248228
if err != nil {
249229
tb.Log(string(out))
250230
tb.Fatalf("can't parse go lines count: %s", err)
@@ -399,7 +379,14 @@ func getLinterNames(tb testing.TB, fastOnly bool) []string {
399379
tb.Helper()
400380

401381
// add linter names here if needed.
402-
var excluded []string
382+
excluded := []string{
383+
"gci", // Formatter
384+
"gofmt", // Formatter
385+
"gofumpt", // Formatter
386+
"goimports", // Formatter
387+
"golines", // Formatter
388+
"swaggo", // Formatter
389+
}
403390

404391
linters, err := lintersdb.NewLinterBuilder().Build(config.NewDefault())
405392
require.NoError(tb, err)
@@ -410,6 +397,10 @@ func getLinterNames(tb testing.TB, fastOnly bool) []string {
410397
continue
411398
}
412399

400+
if lc.Internal {
401+
continue
402+
}
403+
413404
if fastOnly && lc.IsSlowLinter() {
414405
continue
415406
}

0 commit comments

Comments
 (0)