Skip to content

Commit 2121370

Browse files
authored
add 'cyclop' linter (#1738)
1 parent 123da8e commit 2121370

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

.golangci.example.yml

+8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ linters-settings:
190190
gocyclo:
191191
# minimal code complexity to report, 30 by default (but we recommend 10-20)
192192
min-complexity: 10
193+
194+
cyclop:
195+
# the maximal code complexity to report
196+
max-complexity: 10
197+
# the maximal average package complexity. If it's higher than 0.0 (float) the check is enabled (default 0.0)
198+
package-average: 0.0
199+
# should ignore tests (default false)
200+
skip-tests: false
193201
godot:
194202
# comments to be checked: `declarations`, `toplevel`, or `all`
195203
scope: declarations

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/alexkohler/prealloc v0.0.0-20210204145425-77a5b5dd9799
1010
github.com/ashanbrown/forbidigo v1.1.0
1111
github.com/ashanbrown/makezero v0.0.0-20201205152432-7b7cdbb3025a
12+
github.com/bkielbasa/cyclop v1.2.0
1213
github.com/bombsimon/wsl/v3 v3.1.0
1314
github.com/charithe/durationcheck v0.0.3
1415
github.com/daixiang0/gci v0.2.8

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ type LintersSettings struct {
183183
Gocyclo struct {
184184
MinComplexity int `mapstructure:"min-complexity"`
185185
}
186+
Cyclop struct {
187+
MaxComplexity int `mapstructure:"max-complexity"`
188+
PackageAverage float64 `mapstructure:"package-average"`
189+
SkipTests bool `mapstructure:"skip-tests"`
190+
}
186191
Varcheck struct {
187192
CheckExportedFields bool `mapstructure:"exported-fields"`
188193
}

pkg/golinters/cyclop.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package golinters
2+
3+
import (
4+
"github.com/bkielbasa/cyclop/pkg/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
const cyclopName = "cyclop"
11+
12+
func NewCyclop() *goanalysis.Linter {
13+
return goanalysis.NewLinter(
14+
cyclopName,
15+
"checks function and package cyclomatic complexity",
16+
[]*analysis.Analyzer{
17+
analyzer.NewAnalyzer(),
18+
},
19+
nil,
20+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
21+
}

pkg/lint/lintersdb/manager.go

+4
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
194194
linter.NewConfig(golinters.NewGocyclo()).
195195
WithPresets(linter.PresetComplexity).
196196
WithURL("https://github.com/alecthomas/gocyclo"),
197+
linter.NewConfig(golinters.NewCyclop()).
198+
WithLoadForGoAnalysis().
199+
WithPresets(linter.PresetComplexity).
200+
WithURL("https://github.com/bkielbasa/cyclop"),
197201
linter.NewConfig(golinters.NewGocognit()).
198202
WithPresets(linter.PresetComplexity).
199203
WithURL("https://github.com/uudashr/gocognit"),

test/testdata/cyclop.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// args: -Ecyclop
2+
package testdata
3+
4+
import "math"
5+
6+
func cyclopComplexFunc() { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 11, max is 10"
7+
i := math.MaxInt8
8+
if i > 2 {
9+
if i > 2 {
10+
}
11+
if i > 2 {
12+
}
13+
if i > 2 {
14+
}
15+
if i > 2 {
16+
}
17+
} else {
18+
if i > 2 {
19+
}
20+
if i > 2 {
21+
}
22+
if i > 2 {
23+
}
24+
if i > 2 {
25+
}
26+
}
27+
28+
if i > 2 {
29+
}
30+
}

0 commit comments

Comments
 (0)