Skip to content

Commit

Permalink
apply suggestion from #1741
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 17, 2021
1 parent 3a9d212 commit cec1e78
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 11 deletions.
1 change: 0 additions & 1 deletion pkg/golinters/gocognit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// nolint:dupl
package golinters

import (
Expand Down
12 changes: 2 additions & 10 deletions pkg/golinters/gocyclo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// nolint:dupl
package golinters

import (
"fmt"
"sort"
"sync"

"github.com/fzipp/gocyclo"
Expand Down Expand Up @@ -31,24 +29,18 @@ func NewGocyclo() *goanalysis.Linter {
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var stats []gocyclo.Stat
var stats gocyclo.Stats
for _, f := range pass.Files {
stats = gocyclo.AnalyzeASTFile(f, pass.Fset, stats)
}
if len(stats) == 0 {
return nil, nil
}

sort.SliceStable(stats, func(i, j int) bool {
return stats[i].Complexity > stats[j].Complexity
})
stats = stats.SortAndFilter(-1, lintCtx.Settings().Gocyclo.MinComplexity)

res := make([]goanalysis.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, goanalysis.NewIssue(&result.Issue{
Pos: s.Pos,
Text: fmt.Sprintf("cyclomatic complexity %d of func %s is high (> %d)",
Expand Down

0 comments on commit cec1e78

Please sign in to comment.