Skip to content

Commit

Permalink
cyclop: add missing settings (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 18, 2021
1 parent 9c47715 commit 32e8517
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
12 changes: 7 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@ 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 Expand Up @@ -278,6 +273,7 @@ type LintersSettings struct {
Forbidigo ForbidigoSettings
Ifshort IfshortSettings
Predeclared PredeclaredSettings
Cyclop Cyclop

Custom map[string]CustomLinterSettings
}
Expand Down Expand Up @@ -453,6 +449,12 @@ type PredeclaredSettings struct {
Qualified bool `mapstructure:"q"`
}

type Cyclop struct {
MaxComplexity int `mapstructure:"max-complexity"`
PackageAverage float64 `mapstructure:"package-average"`
SkipTests bool `mapstructure:"skip-tests"`
}

var defaultLintersSettings = LintersSettings{
Lll: LllSettings{
LineLength: 120,
Expand Down
28 changes: 23 additions & 5 deletions pkg/golinters/cyclop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,36 @@ import (
"github.com/bkielbasa/cyclop/pkg/analyzer"
"golang.org/x/tools/go/analysis"

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

const cyclopName = "cyclop"

func NewCyclop() *goanalysis.Linter {
func NewCyclop(settings *config.Cyclop) *goanalysis.Linter {
a := analyzer.NewAnalyzer()

var cfg map[string]map[string]interface{}
if settings != nil {
d := map[string]interface{}{
"skipTests": settings.SkipTests,
}

if settings.MaxComplexity != 0 {
d["maxComplexity"] = settings.MaxComplexity
}

if settings.PackageAverage != 0 {
d["packageAverage"] = settings.PackageAverage
}

cfg = map[string]map[string]interface{}{a.Name: d}
}

return goanalysis.NewLinter(
cyclopName,
"checks function and package cyclomatic complexity",
[]*analysis.Analyzer{
analyzer.NewAnalyzer(),
},
nil,
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
4 changes: 3 additions & 1 deletion pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var predeclaredCfg *config.PredeclaredSettings
var ifshortCfg *config.IfshortSettings
var reviveCfg *config.ReviveSettings
var cyclopCfg *config.Cyclop
if m.cfg != nil {
govetCfg = &m.cfg.LintersSettings.Govet
testpackageCfg = &m.cfg.LintersSettings.Testpackage
Expand All @@ -106,6 +107,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
ifshortCfg = &m.cfg.LintersSettings.Ifshort
reviveCfg = &m.cfg.LintersSettings.Revive
cyclopCfg = &m.cfg.LintersSettings.Cyclop
}
const megacheckName = "megacheck"
lcs := []*linter.Config{
Expand Down Expand Up @@ -194,7 +196,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewGocyclo()).
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/alecthomas/gocyclo"),
linter.NewConfig(golinters.NewCyclop()).
linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
WithLoadForGoAnalysis().
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/bkielbasa/cyclop"),
Expand Down
35 changes: 10 additions & 25 deletions test/testdata/cyclop.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
// args: -Ecyclop
//args: -Ecyclop
//config: linters-settings.cyclop.max-complexity=15
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 {
}
func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}

if i > 2 {
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}
}
4 changes: 3 additions & 1 deletion test/testdata/gocyclo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//config: linters-settings.gocyclo.min-complexity=20
package testdata

import "net/http"

func GocycloBigComplexity(s string) { // ERROR "cyclomatic complexity .* of func .* is high .*"
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}

Expand Down

0 comments on commit 32e8517

Please sign in to comment.