Skip to content

Commit fcd38a3

Browse files
committed
chore: use new language elements
1 parent c49bb16 commit fcd38a3

File tree

8 files changed

+48
-67
lines changed

8 files changed

+48
-67
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/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/dump_info/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func saveCLIHelp(ctx context.Context, dst string) error {
151151
return fmt.Errorf("can't run linters cmd: %w", err)
152152
}
153153

154-
lintersOutParts := bytes.Split(lintersOut, []byte("\n\n"))
154+
lintersOutEnable, _, _ := bytes.Cut(lintersOut, []byte("\n\n"))
155155

156156
rumCmdHelp, err := getCmdHelp(ctx, "run")
157157
if err != nil {
@@ -164,7 +164,7 @@ func saveCLIHelp(ctx context.Context, dst string) error {
164164
}
165165

166166
data := types.CLIHelp{
167-
Enable: string(lintersOutParts[0]),
167+
Enable: string(lintersOutEnable),
168168
RunCmdHelp: rumCmdHelp,
169169
FmtCmdHelp: fmtCmdHelp,
170170
}

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
}

test/testshared/integration/run.go

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

33
import (
4-
"os"
54
"os/exec"
65
"path/filepath"
76
"slices"
@@ -32,12 +31,7 @@ func RunTestSourcesFromDir(t *testing.T, dir string) {
3231

3332
binPath := testshared.InstallGolangciLint(t)
3433

35-
cwd, err := os.Getwd()
36-
require.NoError(t, err)
37-
t.Cleanup(func() { _ = os.Chdir(cwd) })
38-
39-
err = os.Chdir(dir)
40-
require.NoError(t, err)
34+
t.Chdir(dir)
4135

4236
log := logutils.NewStderrLog(logutils.DebugKeyTest)
4337
log.SetLevel(logutils.LogLevelInfo)

0 commit comments

Comments
 (0)