Skip to content

Commit

Permalink
add 'cyclop' linter (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkielbasa authored Feb 17, 2021
1 parent 123da8e commit 2121370
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ linters-settings:
gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 10

cyclop:
# the maximal code complexity to report
max-complexity: 10
# the maximal average package complexity. If it's higher than 0.0 (float) the check is enabled (default 0.0)
package-average: 0.0
# should ignore tests (default false)
skip-tests: false
godot:
# comments to be checked: `declarations`, `toplevel`, or `all`
scope: declarations
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/alexkohler/prealloc v0.0.0-20210204145425-77a5b5dd9799
github.com/ashanbrown/forbidigo v1.1.0
github.com/ashanbrown/makezero v0.0.0-20201205152432-7b7cdbb3025a
github.com/bkielbasa/cyclop v1.2.0
github.com/bombsimon/wsl/v3 v3.1.0
github.com/charithe/durationcheck v0.0.3
github.com/daixiang0/gci v0.2.8
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.

5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ type LintersSettings struct {
Gocyclo struct {
MinComplexity int `mapstructure:"min-complexity"`
}
Cyclop struct {
MaxComplexity int `mapstructure:"max-complexity"`
PackageAverage float64 `mapstructure:"package-average"`
SkipTests bool `mapstructure:"skip-tests"`
}
Varcheck struct {
CheckExportedFields bool `mapstructure:"exported-fields"`
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/golinters/cyclop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package golinters

import (
"github.com/bkielbasa/cyclop/pkg/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

const cyclopName = "cyclop"

func NewCyclop() *goanalysis.Linter {
return goanalysis.NewLinter(
cyclopName,
"checks function and package cyclomatic complexity",
[]*analysis.Analyzer{
analyzer.NewAnalyzer(),
},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewGocyclo()).
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/alecthomas/gocyclo"),
linter.NewConfig(golinters.NewCyclop()).
WithLoadForGoAnalysis().
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/bkielbasa/cyclop"),
linter.NewConfig(golinters.NewGocognit()).
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/uudashr/gocognit"),
Expand Down
30 changes: 30 additions & 0 deletions test/testdata/cyclop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// args: -Ecyclop
package testdata

import "math"

func cyclopComplexFunc() { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 11, max is 10"
i := math.MaxInt8
if i > 2 {
if i > 2 {
}
if i > 2 {
}
if i > 2 {
}
if i > 2 {
}
} else {
if i > 2 {
}
if i > 2 {
}
if i > 2 {
}
if i > 2 {
}
}

if i > 2 {
}
}

0 comments on commit 2121370

Please sign in to comment.