File tree 5 files changed +46
-37
lines changed
5 files changed +46
-37
lines changed Original file line number Diff line number Diff line change @@ -183,11 +183,6 @@ type LintersSettings struct {
183
183
Gocyclo struct {
184
184
MinComplexity int `mapstructure:"min-complexity"`
185
185
}
186
- Cyclop struct {
187
- MaxComplexity int `mapstructure:"max-complexity"`
188
- PackageAverage float64 `mapstructure:"package-average"`
189
- SkipTests bool `mapstructure:"skip-tests"`
190
- }
191
186
Varcheck struct {
192
187
CheckExportedFields bool `mapstructure:"exported-fields"`
193
188
}
@@ -278,6 +273,7 @@ type LintersSettings struct {
278
273
Forbidigo ForbidigoSettings
279
274
Ifshort IfshortSettings
280
275
Predeclared PredeclaredSettings
276
+ Cyclop Cyclop
281
277
282
278
Custom map [string ]CustomLinterSettings
283
279
}
@@ -453,6 +449,12 @@ type PredeclaredSettings struct {
453
449
Qualified bool `mapstructure:"q"`
454
450
}
455
451
452
+ type Cyclop struct {
453
+ MaxComplexity int `mapstructure:"max-complexity"`
454
+ PackageAverage float64 `mapstructure:"package-average"`
455
+ SkipTests bool `mapstructure:"skip-tests"`
456
+ }
457
+
456
458
var defaultLintersSettings = LintersSettings {
457
459
Lll : LllSettings {
458
460
LineLength : 120 ,
Original file line number Diff line number Diff line change @@ -4,18 +4,36 @@ import (
4
4
"github.com/bkielbasa/cyclop/pkg/analyzer"
5
5
"golang.org/x/tools/go/analysis"
6
6
7
+ "github.com/golangci/golangci-lint/pkg/config"
7
8
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8
9
)
9
10
10
11
const cyclopName = "cyclop"
11
12
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
+
13
33
return goanalysis .NewLinter (
14
34
cyclopName ,
15
35
"checks function and package cyclomatic complexity" ,
16
- []* analysis.Analyzer {
17
- analyzer .NewAnalyzer (),
18
- },
19
- nil ,
36
+ []* analysis.Analyzer {a },
37
+ cfg ,
20
38
).WithLoadMode (goanalysis .LoadModeTypesInfo )
21
39
}
Original file line number Diff line number Diff line change @@ -97,6 +97,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
97
97
var predeclaredCfg * config.PredeclaredSettings
98
98
var ifshortCfg * config.IfshortSettings
99
99
var reviveCfg * config.ReviveSettings
100
+ var cyclopCfg * config.Cyclop
100
101
if m .cfg != nil {
101
102
govetCfg = & m .cfg .LintersSettings .Govet
102
103
testpackageCfg = & m .cfg .LintersSettings .Testpackage
@@ -106,6 +107,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
106
107
predeclaredCfg = & m .cfg .LintersSettings .Predeclared
107
108
ifshortCfg = & m .cfg .LintersSettings .Ifshort
108
109
reviveCfg = & m .cfg .LintersSettings .Revive
110
+ cyclopCfg = & m .cfg .LintersSettings .Cyclop
109
111
}
110
112
const megacheckName = "megacheck"
111
113
lcs := []* linter.Config {
@@ -194,7 +196,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
194
196
linter .NewConfig (golinters .NewGocyclo ()).
195
197
WithPresets (linter .PresetComplexity ).
196
198
WithURL ("https://github.com/alecthomas/gocyclo" ),
197
- linter .NewConfig (golinters .NewCyclop ()).
199
+ linter .NewConfig (golinters .NewCyclop (cyclopCfg )).
198
200
WithLoadForGoAnalysis ().
199
201
WithPresets (linter .PresetComplexity ).
200
202
WithURL ("https://github.com/bkielbasa/cyclop" ),
Original file line number Diff line number Diff line change 1
- // args: -Ecyclop
1
+ //args: -Ecyclop
2
+ //config: linters-settings.cyclop.max-complexity=15
2
3
package testdata
3
4
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
26
8
}
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
29
14
}
30
15
}
Original file line number Diff line number Diff line change 2
2
//config: linters-settings.gocyclo.min-complexity=20
3
3
package testdata
4
4
5
+ import "net/http"
6
+
5
7
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" {
7
9
return
8
10
}
9
11
You can’t perform that action at this time.
0 commit comments