diff --git a/.golangci.example.yml b/.golangci.example.yml index 2457f27c0324..b4bfd0f0fcbf 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -783,6 +783,10 @@ linters-settings: force-short-decl-cuddling: false strict-append: true + maintidx: + # show functions with maintainability index < N only. + under: 20 + # The custom section can be used to define linter plugins to be loaded at runtime. # See README doc for more info. custom: diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index b04196d19120..12f9a0dbca17 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -80,6 +80,9 @@ var defaultLintersSettings = LintersSettings{ Forbidigo: ForbidigoSettings{ ExcludeGodocExamples: true, }, + MaintIdx: MaintIdxSettings{ + Under: 20, + }, } type LintersSettings struct { @@ -117,6 +120,7 @@ type LintersSettings struct { Ireturn IreturnSettings ImportAs ImportAsSettings Lll LllSettings + MaintIdx MaintIdxSettings Makezero MakezeroSettings Maligned MalignedSettings Misspell MisspellSettings @@ -371,6 +375,10 @@ type LllSettings struct { TabWidth int `mapstructure:"tab-width"` } +type MaintIdxSettings struct { + Under int `mapstructure:"under"` +} + type MakezeroSettings struct { Always bool } diff --git a/pkg/golinters/maintidx.go b/pkg/golinters/maintidx.go index 2045fbf4fd8f..94ca94cebab5 100644 --- a/pkg/golinters/maintidx.go +++ b/pkg/golinters/maintidx.go @@ -1,20 +1,33 @@ package golinters import ( - "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/yagipy/maintidx" "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -func NewMaintIdx() *goanalysis.Linter { - analyzers := []*analysis.Analyzer{ - maintidx.Analyzer, +func NewMaintIdx(cfg *config.MaintIdxSettings) *goanalysis.Linter { + analyzer := maintidx.Analyzer + + cfgMap := map[string]map[string]interface{}{} + cfgMap[analyzer.Name] = map[string]interface{}{ + "under": 20, + } + + if cfg != nil { + cfgMap[analyzer.Name] = map[string]interface{}{ + "under": cfg.Under, + } } return goanalysis.NewLinter( - "maintidx", - "maintidx measures the maintainability index of each function.", - analyzers, - nil, + analyzer.Name, + analyzer.Doc, + []*analysis.Analyzer{ + analyzer, + }, + cfgMap, ).WithLoadMode(goanalysis.LoadModeSyntax) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 8427ffb34540..f49578fcf7fe 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -113,6 +113,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var ifshortCfg *config.IfshortSettings var importAsCfg *config.ImportAsSettings var ireturnCfg *config.IreturnSettings + var maintIdxCfg *config.MaintIdxSettings var nilNilCfg *config.NilNilSettings var predeclaredCfg *config.PredeclaredSettings var reviveCfg *config.ReviveSettings @@ -141,6 +142,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { ifshortCfg = &m.cfg.LintersSettings.Ifshort importAsCfg = &m.cfg.LintersSettings.ImportAs ireturnCfg = &m.cfg.LintersSettings.Ireturn + maintIdxCfg = &m.cfg.LintersSettings.MaintIdx nilNilCfg = &m.cfg.LintersSettings.NilNil predeclaredCfg = &m.cfg.LintersSettings.Predeclared reviveCfg = &m.cfg.LintersSettings.Revive @@ -555,8 +557,8 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetBugs). WithLoadForGoAnalysis(). WithURL("https://github.com/breml/errchkjson"), - linter.NewConfig(golinters.NewMaintIdx()). - WithSince("v1.1.0"). + linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)). + WithSince("v1.44.0"). WithPresets(linter.PresetComplexity). WithLoadForGoAnalysis(). WithURL("https://github.com/yagipy/maintidx"), diff --git a/test/testdata/configs/maintidx_under_100.yml b/test/testdata/configs/maintidx_under_100.yml new file mode 100644 index 000000000000..8d54963412e7 --- /dev/null +++ b/test/testdata/configs/maintidx_under_100.yml @@ -0,0 +1,3 @@ +linters-settings: + maintidx: + under: 100 diff --git a/test/testdata/maintidx_under_100.go b/test/testdata/maintidx_under_100.go new file mode 100644 index 000000000000..421294ad6ed0 --- /dev/null +++ b/test/testdata/maintidx_under_100.go @@ -0,0 +1,197 @@ +// args: -Emaintidx +// config_path: testdata/configs/maintidx_under_100.yml +package testdata + +func over20() { // ERROR "Function name: over20, Cyclomatic Complexity: 76, Halstead Volume: 1636.00, Maintainability Index: 17" +} + +func under20() { // ERROR "Function name: under20, Cyclomatic Complexity: 76, Halstead Volume: 1636.00, Maintainability Index: 17" + for true { + if false { + if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else { + n := 0 + switch n { + case 0: + case 1: + default: + } + } + } else if false { + if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else { + n := 0 + switch n { + case 0: + case 1: + default: + } + } + } else if false { + if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else { + n := 0 + switch n { + case 0: + case 1: + default: + } + } + } else if false { + if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else { + n := 0 + switch n { + case 0: + case 1: + default: + } + } + } else { + if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else if false { + n := 0 + switch n { + case 0: + case 1: + default: + } + } else { + n := 0 + switch n { + case 0: + case 1: + default: + } + } + } + } +}