diff --git a/go.mod b/go.mod index 342cafb7498d..7f898b16efeb 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f244ce258d9d..fd76436046b5 100644 --- a/go.sum +++ b/go.sum @@ -798,6 +798,8 @@ github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8/go.mod h1:dniwbG github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/yagipy/maintidx v1.0.0 h1:h5NvIsCz+nRDapQ0exNv4aJ0yXSI0420omVANTv3GJM= +github.com/yagipy/maintidx v1.0.0/go.mod h1:0qNf/I/CCZXSMhsRsrEPDZ+DkekpKLXAJfsTACwgXLk= github.com/yeya24/promlinter v0.1.0 h1:goWULN0jH5Yajmu/K+v1xCqIREeB+48OiJ2uu2ssc7U= github.com/yeya24/promlinter v0.1.0/go.mod h1:rs5vtZzeBHqqMwXqFScncpCF6u06lezhZepno9AB1Oc= github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg= diff --git a/pkg/golinters/maintidx.go b/pkg/golinters/maintidx.go new file mode 100644 index 000000000000..2045fbf4fd8f --- /dev/null +++ b/pkg/golinters/maintidx.go @@ -0,0 +1,20 @@ +package golinters + +import ( + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" + "github.com/yagipy/maintidx" + "golang.org/x/tools/go/analysis" +) + +func NewMaintIdx() *goanalysis.Linter { + analyzers := []*analysis.Analyzer{ + maintidx.Analyzer, + } + + return goanalysis.NewLinter( + "maintidx", + "maintidx measures the maintainability index of each function.", + analyzers, + nil, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index db386a17d58d..11ae40444168 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -439,6 +439,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithSince("v1.8.0"). WithPresets(linter.PresetStyle), + linter.NewConfig(golinters.NewMaintIdx()). + WithSince("v1.1.0"). + WithPresets(linter.PresetComplexity). + WithLoadForGoAnalysis(). + WithURL("https://github.com/yagipy/maintidx"), + linter.NewConfig(golinters.NewMakezero()). WithSince("v1.34.0"). WithPresets(linter.PresetStyle, linter.PresetBugs). diff --git a/test/testdata/maintidx.go b/test/testdata/maintidx.go new file mode 100644 index 000000000000..51f95235040c --- /dev/null +++ b/test/testdata/maintidx.go @@ -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: + } + } + } + } +}