Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: replace raw loops with funcs from slices and maps #4299

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ require (
gitlab.com/bosi/decorder v0.4.1
go-simpler.org/musttag v0.8.0
go-simpler.org/sloglint v0.4.0
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
golang.org/x/tools v0.16.1
gopkg.in/yaml.v3 v3.0.1
honnef.co/go/tools v0.4.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions pkg/golinters/goanalysis/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sort"
"sync"

"golang.org/x/exp/maps"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"

Expand Down Expand Up @@ -159,10 +160,7 @@ func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *pac
act.objectFacts = make(map[objectFactKey]analysis.Fact)
act.packageFacts = make(map[packageFactKey]analysis.Fact)

paths := make([]string, 0, len(pkg.Imports))
for path := range pkg.Imports {
paths = append(paths, path)
}
paths := maps.Keys(pkg.Imports)
sort.Strings(paths) // for determinism
for _, path := range paths {
dep := r.makeAction(a, pkg.Imports[path], initialPkgs, actions, actAlloc)
Expand Down Expand Up @@ -212,10 +210,7 @@ func (r *runner) prepareAnalysis(pkgs []*packages.Package,
}
}

allActions := make([]*action, 0, len(actions))
for _, act := range actions {
allActions = append(allActions, act)
}
allActions := maps.Values(actions)

debugf("Built %d actions", len(actions))

Expand Down
19 changes: 4 additions & 15 deletions pkg/golinters/gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/go-critic/go-critic/checkers"
gocriticlinter "github.com/go-critic/go-critic/linter"
"golang.org/x/exp/maps"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
Expand Down Expand Up @@ -219,10 +220,7 @@ func (w *goCriticWrapper) configureCheckerInfo(info *gocriticlinter.CheckerInfo,
info.Name, k)
}

var supportedKeys []string
for sk := range info.Params {
supportedKeys = append(supportedKeys, sk)
}
supportedKeys := maps.Keys(info.Params)
sort.Strings(supportedKeys)

return fmt.Errorf("checker %s config param %s doesn't exist, all existing: %s",
Expand Down Expand Up @@ -311,11 +309,7 @@ func (s *goCriticSettingsWrapper) checkerTagsDebugf() {

tagToCheckers := s.buildTagToCheckersMap()

allTags := make([]string, 0, len(tagToCheckers))
for tag := range tagToCheckers {
allTags = append(allTags, tag)
}

allTags := maps.Keys(tagToCheckers)
sort.Strings(allTags)

goCriticDebugf("All gocritic existing tags and checks:")
Expand Down Expand Up @@ -609,12 +603,7 @@ func intersectStringSlice(s1, s2 []string) []string {
}

func sprintAllowedCheckerNames(allowedNames map[string]bool) string {
namesSlice := make([]string, 0, len(allowedNames))

for name := range allowedNames {
namesSlice = append(namesSlice, name)
}

namesSlice := maps.Keys(allowedNames)
return sprintStrings(namesSlice)
}

Expand Down
34 changes: 18 additions & 16 deletions pkg/golinters/govet_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package golinters

import (
"sort"
"testing"

"golang.org/x/exp/slices"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/asmdecl"
"golang.org/x/tools/go/analysis/passes/assign"
Expand All @@ -18,39 +18,41 @@ import (

func TestGovet(t *testing.T) {
// Checking that every default analyzer is in "all analyzers" list.
var checkList []*analysis.Analyzer
checkList = append(checkList, defaultAnalyzers...)
checkList := append([]*analysis.Analyzer{}, defaultAnalyzers...)
checkList = append(checkList, shadow.Analyzer) // special case, used in analyzersFromConfig

for _, defaultAnalyzer := range checkList {
found := false
for _, a := range allAnalyzers {
if a.Name == defaultAnalyzer.Name {
found = true
break
}
}
found := slices.ContainsFunc(allAnalyzers, func(a *analysis.Analyzer) bool {
return a.Name == defaultAnalyzer.Name
})
if !found {
t.Errorf("%s is not in allAnalyzers", defaultAnalyzer.Name)
}
}
}

type sortedAnalyzers []*analysis.Analyzer
func sortAnalyzers(a, b *analysis.Analyzer) int {
if a.Name < b.Name {
return -1
}

if a.Name > b.Name {
return 1
}

func (p sortedAnalyzers) Len() int { return len(p) }
func (p sortedAnalyzers) Less(i, j int) bool { return p[i].Name < p[j].Name }
func (p sortedAnalyzers) Swap(i, j int) { p[i].Name, p[j].Name = p[j].Name, p[i].Name }
return 0
}

func TestGovetSorted(t *testing.T) {
// Keeping analyzers sorted so their order match the import order.
t.Run("All", func(t *testing.T) {
if !sort.IsSorted(sortedAnalyzers(allAnalyzers)) {
if !slices.IsSortedFunc(allAnalyzers, sortAnalyzers) {
t.Error("please keep all analyzers list sorted by name")
}
})

t.Run("Default", func(t *testing.T) {
if !sort.IsSorted(sortedAnalyzers(defaultAnalyzers)) {
if !slices.IsSortedFunc(defaultAnalyzers, sortAnalyzers) {
t.Error("please keep default analyzers list sorted by name")
}
})
Expand Down
6 changes: 2 additions & 4 deletions pkg/golinters/thelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/kulti/thelper/pkg/analyzer"
"golang.org/x/exp/maps"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
Expand Down Expand Up @@ -42,10 +43,7 @@ func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
linterLogger.Fatalf("thelper: at least one option must be enabled")
}

var args []string
for k := range opts {
args = append(args, k)
}
args := maps.Keys(opts)

cfgMap := map[string]map[string]any{
a.Name: {
Expand Down
12 changes: 4 additions & 8 deletions pkg/lint/lintersdb/enabled_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"sort"

"golang.org/x/exp/maps"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
Expand Down Expand Up @@ -115,10 +117,7 @@ func (es EnabledSet) GetOptimizedLinters() ([]*linter.Config, error) {
es.verbosePrintLintersStatus(resultLintersSet)
es.combineGoAnalysisLinters(resultLintersSet)

var resultLinters []*linter.Config
for _, lc := range resultLintersSet {
resultLinters = append(resultLinters, lc)
}
resultLinters := maps.Values(resultLintersSet)

// Make order of execution of linters (go/analysis metalinter and unused) stable.
sort.Slice(resultLinters, func(i, j int) bool {
Expand Down Expand Up @@ -185,10 +184,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)

ml := goanalysis.NewMetaLinter(goanalysisLinters)

var presets []string
for p := range goanalysisPresets {
presets = append(presets, p)
}
presets := maps.Keys(goanalysisPresets)

mlConfig := &linter.Config{
Linter: ml,
Expand Down
6 changes: 2 additions & 4 deletions pkg/printers/checkstyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

"github.com/go-xmlfmt/xmlfmt"
"golang.org/x/exp/maps"

"github.com/golangci/golangci-lint/pkg/result"
)
Expand Down Expand Up @@ -74,10 +75,7 @@ func (p Checkstyle) Print(issues []result.Issue) error {
file.Errors = append(file.Errors, newError)
}

out.Files = make([]*checkstyleFile, 0, len(files))
for _, file := range files {
out.Files = append(out.Files, file)
}
out.Files = maps.Values(files)

sort.Slice(out.Files, func(i, j int) bool {
return out.Files[i].Name < out.Files[j].Name
Expand Down
6 changes: 3 additions & 3 deletions pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sort"
"strings"

"golang.org/x/exp/maps"

"github.com/golangci/golangci-lint/pkg/result"
)

Expand Down Expand Up @@ -71,9 +73,7 @@ func (p JunitXML) Print(issues []result.Issue) error {
}

var res testSuitesXML
for _, val := range suites {
res.TestSuites = append(res.TestSuites, val)
}
res.TestSuites = maps.Values(suites)

sort.Slice(res.TestSuites, func(i, j int) bool {
return res.TestSuites[i].Suite < res.TestSuites[j].Suite
Expand Down
7 changes: 3 additions & 4 deletions pkg/result/processors/nolint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sort"
"strings"

"golang.org/x/exp/maps"

"github.com/golangci/golangci-lint/pkg/golinters"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
Expand Down Expand Up @@ -289,10 +291,7 @@ func (p *Nolint) Finish() {
return
}

unknownLinters := make([]string, 0, len(p.unknownLintersSet))
for name := range p.unknownLintersSet {
unknownLinters = append(unknownLinters, name)
}
unknownLinters := maps.Keys(p.unknownLintersSet)
sort.Strings(unknownLinters)

p.log.Warnf("Found unknown linters in //nolint directives: %s", strings.Join(unknownLinters, ", "))
Expand Down
7 changes: 2 additions & 5 deletions scripts/expand_website_templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"unicode"
"unicode/utf8"

"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"

"github.com/golangci/golangci-lint/internal/renameio"
Expand Down Expand Up @@ -360,11 +361,7 @@ func getThanksList() string {
}
}

var authors []string
for author := range addedAuthors {
authors = append(authors, author)
}

authors := maps.Keys(addedAuthors)
sort.Slice(authors, func(i, j int) bool {
return strings.ToLower(authors[i]) < strings.ToLower(authors[j])
})
Expand Down
Loading