Skip to content
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

Add maintidx linter #2435

Merged
merged 3 commits into from
Jan 18, 2022
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
6 changes: 6 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ linters-settings:
# tab width in spaces. Default to 1.
tab-width: 1

maintidx:
# Show functions with maintainability index lower than N.
# A high index indicates better maintainability (it's kind of the opposite of complexity).
# default: 20
under: 100

makezero:
# Allow only slices initialized with a length of zero. Default is false.
always: false
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ require (
github.com/ultraware/whitespace v0.0.4
github.com/uudashr/gocognit v1.0.5
github.com/valyala/quicktemplate v1.7.0
github.com/yagipy/maintidx v1.0.0
github.com/yeya24/promlinter v0.1.0
gitlab.com/bosi/decorder v0.2.1
golang.org/x/tools v0.1.9-0.20211228192929-ee1ca4ffc4da
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.

8 changes: 8 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ var defaultLintersSettings = LintersSettings{
LineLength: 120,
TabWidth: 1,
},
MaintIdx: MaintIdxSettings{
Under: 20,
},
Nakedret: NakedretSettings{
MaxFuncLines: 30,
},
Expand Down Expand Up @@ -128,6 +131,7 @@ type LintersSettings struct {
ImportAs ImportAsSettings
Ireturn IreturnSettings
Lll LllSettings
MaintIdx MaintIdxSettings
Makezero MakezeroSettings
Maligned MalignedSettings
Misspell MisspellSettings
Expand Down Expand Up @@ -389,6 +393,10 @@ type LllSettings struct {
TabWidth int `mapstructure:"tab-width"`
}

type MaintIdxSettings struct {
Under int `mapstructure:"under"`
}

type MakezeroSettings struct {
Always bool
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/golinters/maintidx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package golinters

import (
"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(cfg *config.MaintIdxSettings) *goanalysis.Linter {
analyzer := maintidx.Analyzer

cfgMap := map[string]map[string]interface{}{
analyzer.Name: {"under": 20},
}

if cfg != nil {
cfgMap[analyzer.Name] = map[string]interface{}{
"under": cfg.Under,
}
}

return goanalysis.NewLinter(
analyzer.Name,
analyzer.Doc,
[]*analysis.Analyzer{
analyzer,
},
cfgMap,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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 nlreturnCfg *config.NlreturnSettings
var predeclaredCfg *config.PredeclaredSettings
Expand Down Expand Up @@ -143,6 +144,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
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
Expand Down Expand Up @@ -439,6 +441,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.8.0").
WithPresets(linter.PresetStyle),

linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
WithSince("v1.44.0").
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/yagipy/maintidx"),

linter.NewConfig(golinters.NewMakezero()).
WithSince("v1.34.0").
WithPresets(linter.PresetStyle, linter.PresetBugs).
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/maintidx_under_100.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
maintidx:
under: 100
196 changes: 196 additions & 0 deletions test/testdata/maintidx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
//args: -Emaintidx
package testdata

func over20() {
}

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:
}
}
}
}
}
Loading