github.com/shulhan/golangci-lint@v1.10.1/pkg/golinters/gocyclo.go (about) 1 package golinters 2 3 import ( 4 "context" 5 "fmt" 6 "sort" 7 8 gocycloAPI "github.com/golangci/gocyclo/pkg/gocyclo" 9 "github.com/golangci/golangci-lint/pkg/lint/linter" 10 "github.com/golangci/golangci-lint/pkg/result" 11 ) 12 13 type Gocyclo struct{} 14 15 func (Gocyclo) Name() string { 16 return "gocyclo" 17 } 18 19 func (Gocyclo) Desc() string { 20 return "Computes and checks the cyclomatic complexity of functions" 21 } 22 23 func (g Gocyclo) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { 24 var stats []gocycloAPI.Stat 25 for _, f := range lintCtx.ASTCache.GetAllValidFiles() { 26 stats = gocycloAPI.BuildStats(f.F, f.Fset, stats) 27 } 28 if len(stats) == 0 { 29 return nil, nil 30 } 31 32 sort.Slice(stats, func(i, j int) bool { 33 return stats[i].Complexity > stats[j].Complexity 34 }) 35 36 res := make([]result.Issue, 0, len(stats)) 37 for _, s := range stats { 38 if s.Complexity <= lintCtx.Settings().Gocyclo.MinComplexity { 39 break //Break as the stats is already sorted from greatest to least 40 } 41 42 res = append(res, result.Issue{ 43 Pos: s.Pos, 44 Text: fmt.Sprintf("cyclomatic complexity %d of func %s is high (> %d)", 45 s.Complexity, formatCode(s.FuncName, lintCtx.Cfg), lintCtx.Settings().Gocyclo.MinComplexity), 46 FromLinter: g.Name(), 47 }) 48 } 49 50 return res, nil 51 }