Skip to content

Commit 32e8517

Browse files
authored
cyclop: add missing settings (#1743)
1 parent 9c47715 commit 32e8517

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

pkg/config/config.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ 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-
}
191186
Varcheck struct {
192187
CheckExportedFields bool `mapstructure:"exported-fields"`
193188
}
@@ -278,6 +273,7 @@ type LintersSettings struct {
278273
Forbidigo ForbidigoSettings
279274
Ifshort IfshortSettings
280275
Predeclared PredeclaredSettings
276+
Cyclop Cyclop
281277

282278
Custom map[string]CustomLinterSettings
283279
}
@@ -453,6 +449,12 @@ type PredeclaredSettings struct {
453449
Qualified bool `mapstructure:"q"`
454450
}
455451

452+
type Cyclop struct {
453+
MaxComplexity int `mapstructure:"max-complexity"`
454+
PackageAverage float64 `mapstructure:"package-average"`
455+
SkipTests bool `mapstructure:"skip-tests"`
456+
}
457+
456458
var defaultLintersSettings = LintersSettings{
457459
Lll: LllSettings{
458460
LineLength: 120,

pkg/golinters/cyclop.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,36 @@ import (
44
"github.com/bkielbasa/cyclop/pkg/analyzer"
55
"golang.org/x/tools/go/analysis"
66

7+
"github.com/golangci/golangci-lint/pkg/config"
78
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
89
)
910

1011
const cyclopName = "cyclop"
1112

12-
func NewCyclop() *goanalysis.Linter {
13+
func NewCyclop(settings *config.Cyclop) *goanalysis.Linter {
14+
a := analyzer.NewAnalyzer()
15+
16+
var cfg map[string]map[string]interface{}
17+
if settings != nil {
18+
d := map[string]interface{}{
19+
"skipTests": settings.SkipTests,
20+
}
21+
22+
if settings.MaxComplexity != 0 {
23+
d["maxComplexity"] = settings.MaxComplexity
24+
}
25+
26+
if settings.PackageAverage != 0 {
27+
d["packageAverage"] = settings.PackageAverage
28+
}
29+
30+
cfg = map[string]map[string]interface{}{a.Name: d}
31+
}
32+
1333
return goanalysis.NewLinter(
1434
cyclopName,
1535
"checks function and package cyclomatic complexity",
16-
[]*analysis.Analyzer{
17-
analyzer.NewAnalyzer(),
18-
},
19-
nil,
36+
[]*analysis.Analyzer{a},
37+
cfg,
2038
).WithLoadMode(goanalysis.LoadModeTypesInfo)
2139
}

pkg/lint/lintersdb/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
9797
var predeclaredCfg *config.PredeclaredSettings
9898
var ifshortCfg *config.IfshortSettings
9999
var reviveCfg *config.ReviveSettings
100+
var cyclopCfg *config.Cyclop
100101
if m.cfg != nil {
101102
govetCfg = &m.cfg.LintersSettings.Govet
102103
testpackageCfg = &m.cfg.LintersSettings.Testpackage
@@ -106,6 +107,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
106107
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
107108
ifshortCfg = &m.cfg.LintersSettings.Ifshort
108109
reviveCfg = &m.cfg.LintersSettings.Revive
110+
cyclopCfg = &m.cfg.LintersSettings.Cyclop
109111
}
110112
const megacheckName = "megacheck"
111113
lcs := []*linter.Config{
@@ -194,7 +196,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
194196
linter.NewConfig(golinters.NewGocyclo()).
195197
WithPresets(linter.PresetComplexity).
196198
WithURL("https://github.com/alecthomas/gocyclo"),
197-
linter.NewConfig(golinters.NewCyclop()).
199+
linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
198200
WithLoadForGoAnalysis().
199201
WithPresets(linter.PresetComplexity).
200202
WithURL("https://github.com/bkielbasa/cyclop"),

test/testdata/cyclop.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
1-
// args: -Ecyclop
1+
//args: -Ecyclop
2+
//config: linters-settings.cyclop.max-complexity=15
23
package testdata
34

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-
}
5+
func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"
6+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
7+
return
268
}
27-
28-
if i > 2 {
9+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
10+
return
11+
}
12+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
13+
return
2914
}
3015
}

test/testdata/gocyclo.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//config: linters-settings.gocyclo.min-complexity=20
33
package testdata
44

5+
import "net/http"
6+
57
func GocycloBigComplexity(s string) { // ERROR "cyclomatic complexity .* of func .* is high .*"
6-
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
8+
if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
79
return
810
}
911

0 commit comments

Comments
 (0)