Skip to content

Commit 026567f

Browse files
authored
dev: some simplifications (#5843)
1 parent f54365b commit 026567f

File tree

9 files changed

+19
-33
lines changed

9 files changed

+19
-33
lines changed

pkg/commands/internal/migrate/migrate_linter_names.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -749,11 +749,8 @@ func allEnabled(old versionone.Linters, linters []LinterInfo) []LinterInfo {
749749
var results []LinterInfo
750750

751751
for _, linter := range linters {
752-
for _, name := range old.Enable {
753-
if linter.isName(name) {
754-
results = append(results, linter)
755-
break
756-
}
752+
if slices.ContainsFunc(old.Enable, linter.isName) {
753+
results = append(results, linter)
757754
}
758755
}
759756

@@ -764,11 +761,8 @@ func allDisabled(old versionone.Linters, linters []LinterInfo) []LinterInfo {
764761
var results []LinterInfo
765762

766763
for _, linter := range linters {
767-
for _, name := range old.Disable {
768-
if linter.isName(name) {
769-
results = append(results, linter)
770-
break
771-
}
764+
if slices.ContainsFunc(old.Disable, linter.isName) {
765+
results = append(results, linter)
772766
}
773767
}
774768

pkg/goformatters/internal/diff.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,7 @@ func ExtractDiagnosticFromPatch(
250250
}
251251

252252
func toDiagnostic(ft *token.File, change Change, adjLine int) analysis.Diagnostic {
253-
from := change.From + adjLine
254-
if from > ft.LineCount() {
255-
from = ft.LineCount()
256-
}
253+
from := min(change.From+adjLine, ft.LineCount())
257254

258255
start := ft.LineStart(from)
259256

pkg/golinters/govet/govet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
func TestGovet(t *testing.T) {
2020
// Checking that every default analyzer is in "all analyzers" list.
21-
checkList := append([]*analysis.Analyzer{}, defaultAnalyzers...)
21+
checkList := slices.Clone(defaultAnalyzers)
2222
checkList = append(checkList, shadow.Analyzer) // special case, used in analyzersFromConfig
2323

2424
for _, defaultAnalyzer := range checkList {

pkg/golinters/tagliatelle/tagliatelle.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tagliatelle
22

33
import (
4+
"maps"
5+
46
"github.com/ldez/tagliatelle"
57

68
"github.com/golangci/golangci-lint/v2/pkg/config"
@@ -19,9 +21,7 @@ func New(settings *config.TagliatelleSettings) *goanalysis.Linter {
1921
}
2022

2123
if settings != nil {
22-
for k, v := range settings.Case.Rules {
23-
cfg.Rules[k] = v
24-
}
24+
maps.Copy(cfg.Rules, settings.Case.Rules)
2525

2626
cfg.ExtendedRules = toExtendedRules(settings.Case.ExtendedRules)
2727
cfg.UseFieldName = settings.Case.UseFieldName

pkg/report/log.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package report
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/golangci/golangci-lint/v2/pkg/logutils"
@@ -50,7 +51,7 @@ func (lw LogWrapper) Infof(format string, args ...any) {
5051
func (lw LogWrapper) Child(name string) logutils.Log {
5152
c := lw
5253
c.origLog = lw.origLog.Child(name)
53-
c.tags = append([]string{}, lw.tags...)
54+
c.tags = slices.Clone(lw.tags)
5455
c.tags = append(c.tags, name)
5556
return c
5657
}

pkg/result/processors/base_rule.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package processors
22

33
import (
44
"regexp"
5+
"slices"
56

67
"github.com/golangci/golangci-lint/v2/pkg/config"
78
"github.com/golangci/golangci-lint/v2/pkg/fsutils"
@@ -73,13 +74,7 @@ func (r *baseRule) match(issue *result.Issue, lines *fsutils.LineCache, log logu
7374
}
7475

7576
func (r *baseRule) matchLinter(issue *result.Issue) bool {
76-
for _, linter := range r.linters {
77-
if linter == issue.FromLinter {
78-
return true
79-
}
80-
}
81-
82-
return false
77+
return slices.Contains(r.linters, issue.FromLinter)
8378
}
8479

8580
func (r *baseRule) matchSource(issue *result.Issue, lineCache *fsutils.LineCache, log logutils.Log) bool {

pkg/result/processors/nolint_filter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ func (p *NolintFilter) buildIgnoredRangesForFile(f *ast.File, fset *token.FileSe
185185
ast.Walk(&e, f)
186186

187187
// TODO: merge all ranges: there are repeated ranges
188-
allRanges := append([]ignoredRange{}, inlineRanges...)
189-
allRanges = append(allRanges, e.expandedRanges...)
188+
allRanges := slices.Concat(inlineRanges, e.expandedRanges)
190189

191190
return allRanges
192191
}

test/testshared/integration/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"os/exec"
66
"path/filepath"
7+
"slices"
78
"strings"
89
"testing"
910
"time"
@@ -70,7 +71,7 @@ func testOneSource(t *testing.T, log *logutils.StderrLog, binPath, sourcePath st
7071
}
7172

7273
for _, addArg := range []string{"", "-Etypecheck"} {
73-
caseArgs := append([]string{}, args...)
74+
caseArgs := slices.Clone(args)
7475

7576
if addArg != "" {
7677
caseArgs = append(caseArgs, addArg)

test/testshared/runner.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"os/exec"
66
"path/filepath"
7+
"slices"
78
"strings"
89
"sync"
910
"syscall"
@@ -286,10 +287,8 @@ func (r *RunnerResult) ExpectNoIssues() {
286287
func (r *RunnerResult) ExpectExitCode(possibleCodes ...int) *RunnerResult {
287288
r.tb.Helper()
288289

289-
for _, pc := range possibleCodes {
290-
if pc == r.exitCode {
291-
return r
292-
}
290+
if slices.Contains(possibleCodes, r.exitCode) {
291+
return r
293292
}
294293

295294
assert.Fail(r.tb, "invalid exit code", "exit code (%d) must be one of %v: %s", r.exitCode, possibleCodes, r.output)

0 commit comments

Comments
 (0)