
Fix #324, relates #314 1. Update gocritic to the latest version 2. Use proper gocritic checkers repo, old repo was archived 3. Get enabled by default gocritic checks in sync with go-critic: don't enable performance, experimental and opinionated checks by default 4. Support of `enabled-tags` options for gocritic 5. Enable almost all gocritic checks for the project 6. Make rich debugging for gocritic 7. Meticulously validate gocritic checks config
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package golinters
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
|
|
gocycloAPI "github.com/golangci/gocyclo/pkg/gocyclo"
|
|
|
|
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
|
"github.com/golangci/golangci-lint/pkg/result"
|
|
)
|
|
|
|
type Gocyclo struct{}
|
|
|
|
func (Gocyclo) Name() string {
|
|
return "gocyclo"
|
|
}
|
|
|
|
func (Gocyclo) Desc() string {
|
|
return "Computes and checks the cyclomatic complexity of functions"
|
|
}
|
|
|
|
func (g Gocyclo) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
|
|
var stats []gocycloAPI.Stat
|
|
for _, f := range lintCtx.ASTCache.GetAllValidFiles() {
|
|
stats = gocycloAPI.BuildStats(f.F, f.Fset, stats)
|
|
}
|
|
if len(stats) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
sort.Slice(stats, func(i, j int) bool {
|
|
return stats[i].Complexity > stats[j].Complexity
|
|
})
|
|
|
|
res := make([]result.Issue, 0, len(stats))
|
|
for _, s := range stats {
|
|
if s.Complexity <= lintCtx.Settings().Gocyclo.MinComplexity {
|
|
break // Break as the stats is already sorted from greatest to least
|
|
}
|
|
|
|
res = append(res, result.Issue{
|
|
Pos: s.Pos,
|
|
Text: fmt.Sprintf("cyclomatic complexity %d of func %s is high (> %d)",
|
|
s.Complexity, formatCode(s.FuncName, lintCtx.Cfg), lintCtx.Settings().Gocyclo.MinComplexity),
|
|
FromLinter: g.Name(),
|
|
})
|
|
}
|
|
|
|
return res, nil
|
|
}
|