Skip to content

cyclop: add missing settings #1743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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