From 97b10ecb63b9e659e18caab5b6d0f9e6e95bc1f4 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 17 Feb 2021 17:55:23 +0100 Subject: [PATCH] cyclop: add missing settings --- pkg/config/config.go | 12 +++++++----- pkg/golinters/cyclop.go | 28 +++++++++++++++++++++++----- pkg/lint/lintersdb/manager.go | 4 +++- test/testdata/cyclop.go | 35 ++++++++++------------------------- test/testdata/gocyclo.go | 4 +++- 5 files changed, 46 insertions(+), 37 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 314c81fd2db6..33f38b827676 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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"` } @@ -278,6 +273,7 @@ type LintersSettings struct { Forbidigo ForbidigoSettings Ifshort IfshortSettings Predeclared PredeclaredSettings + Cyclop Cyclop Custom map[string]CustomLinterSettings } @@ -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, diff --git a/pkg/golinters/cyclop.go b/pkg/golinters/cyclop.go index df1dd4835467..6f55b2797509 100644 --- a/pkg/golinters/cyclop.go +++ b/pkg/golinters/cyclop.go @@ -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) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 499563d8726c..11407913dbc7 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -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 @@ -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{ @@ -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"), diff --git a/test/testdata/cyclop.go b/test/testdata/cyclop.go index 2551758f62d3..50ce7e4b7969 100644 --- a/test/testdata/cyclop.go +++ b/test/testdata/cyclop.go @@ -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 } } diff --git a/test/testdata/gocyclo.go b/test/testdata/gocyclo.go index 2e2bd3819828..a5a6b1a4fdd2 100644 --- a/test/testdata/gocyclo.go +++ b/test/testdata/gocyclo.go @@ -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 }