Skip to content

Commit

Permalink
dev: replace raw loops with funcs from slices and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Jan 4, 2024
1 parent d23c354 commit d92aea5
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 72 deletions.
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
2 changes: 2 additions & 0 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
33 changes: 15 additions & 18 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,36 @@ 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 (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 }

func TestGovetSorted(t *testing.T) {
// Keeping analyzers sorted so their order match the import order.
cmp := func(a, b *analysis.Analyzer) int {
if a.Name < b.Name {
return -1
} else if a.Name > b.Name {
return 1
}
return 0
}
t.Run("All", func(t *testing.T) {
if !sort.IsSorted(sortedAnalyzers(allAnalyzers)) {
if !slices.IsSortedFunc(allAnalyzers, cmp) {
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, cmp) {
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
4 changes: 2 additions & 2 deletions pkg/lint/lintersdb/enabled_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/maps"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/lint/linter"
Expand Down Expand Up @@ -107,11 +108,10 @@ func TestGetEnabledLintersSet(t *testing.T) {
}

els := es.build(&c.cfg, defaultLinters)
var enabledLinters []string
for ln, lc := range els {
assert.Equal(t, ln, lc.Name())
enabledLinters = append(enabledLinters, ln)
}
enabledLinters := maps.Keys(els)

assert.ElementsMatch(t, c.exp, enabledLinters)
})
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

0 comments on commit d92aea5

Please sign in to comment.