Skip to content

Add 'cyclop' linter #1738

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 17, 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
8 changes: 8 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ linters-settings:
gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 10

cyclop:
# the maximal code complexity to report
max-complexity: 10
# the maximal average package complexity. If it's higher than 0.0 (float) the check is enabled (default 0.0)
package-average: 0.0
# should ignore tests (default false)
skip-tests: false
godot:
# comments to be checked: `declarations`, `toplevel`, or `all`
scope: declarations
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/alexkohler/prealloc v0.0.0-20210204145425-77a5b5dd9799
github.com/ashanbrown/forbidigo v1.1.0
github.com/ashanbrown/makezero v0.0.0-20201205152432-7b7cdbb3025a
github.com/bkielbasa/cyclop v1.2.0
github.com/bombsimon/wsl/v3 v3.1.0
github.com/charithe/durationcheck v0.0.3
github.com/daixiang0/gci v0.2.8
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ 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
21 changes: 21 additions & 0 deletions pkg/golinters/cyclop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package golinters

import (
"github.com/bkielbasa/cyclop/pkg/analyzer"
"golang.org/x/tools/go/analysis"

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

const cyclopName = "cyclop"

func NewCyclop() *goanalysis.Linter {
return goanalysis.NewLinter(
cyclopName,
"checks function and package cyclomatic complexity",
[]*analysis.Analyzer{
analyzer.NewAnalyzer(),
},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewGocyclo()).
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/alecthomas/gocyclo"),
linter.NewConfig(golinters.NewCyclop()).
WithLoadForGoAnalysis().
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/bkielbasa/cyclop"),
linter.NewConfig(golinters.NewGocognit()).
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/uudashr/gocognit"),
Expand Down
30 changes: 30 additions & 0 deletions test/testdata/cyclop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// args: -Ecyclop
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 {
}
}

if i > 2 {
}
}