From eb910ef1b8884abcb35110d4b6fb9cee455ec6e1 Mon Sep 17 00:00:00 2001 From: alingse Date: Sun, 10 Jul 2022 13:58:09 +0800 Subject: [PATCH] Feature: Add asasalint to lint pass []any as any update go.mod --- go.mod | 1 + go.sum | 2 ++ pkg/config/linters_settings.go | 7 +++++++ pkg/golinters/asasalint.go | 26 ++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 8 ++++++++ test/testdata/asasalint.go | 17 +++++++++++++++++ 6 files changed, 61 insertions(+) create mode 100644 pkg/golinters/asasalint.go create mode 100644 test/testdata/asasalint.go diff --git a/go.mod b/go.mod index 67cd4c202670..fa628c2c92a0 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/GaijinEntertainment/go-exhaustruct/v2 v2.2.0 github.com/OpenPeeDeeP/depguard v1.1.0 github.com/alexkohler/prealloc v1.0.0 + github.com/alingse/asasalint v0.0.5 github.com/ashanbrown/forbidigo v1.3.0 github.com/ashanbrown/makezero v1.1.1 github.com/bkielbasa/cyclop v1.2.0 diff --git a/go.sum b/go.sum index c1c4c3431486..8d956aceef7d 100644 --- a/go.sum +++ b/go.sum @@ -70,6 +70,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alexkohler/prealloc v1.0.0 h1:Hbq0/3fJPQhNkN0dR95AVrr6R7tou91y0uHG5pOcUuw= github.com/alexkohler/prealloc v1.0.0/go.mod h1:VetnK3dIgFBBKmg0YnD9F9x6Icjd+9cvfHR56wJVlKE= +github.com/alingse/asasalint v0.0.5 h1:/U8o+wtkyc9wsmDiyvCAcGr5uskgM2tVLNlqZJouid0= +github.com/alingse/asasalint v0.0.5/go.mod h1:nCaoMhw7a9kSJObvQyVzNTPBDbNpdocqrSP7t/cW5+I= github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 3609958c69b8..64ecc01836f4 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -113,6 +113,7 @@ var defaultLintersSettings = LintersSettings{ } type LintersSettings struct { + Asasalint AsasalintSettings BiDiChk BiDiChkSettings Cyclop Cyclop Decorder DecorderSettings @@ -184,6 +185,12 @@ type LintersSettings struct { Custom map[string]CustomLinterSettings } +type AsasalintSettings struct { + Exclude []string `mapstructure:"exclude"` + NoDefaultExclude bool `mapstructure:"no_default_exclude"` + IgnoreInTest bool `mapstructure:"ignore_in_test"` +} + type BiDiChkSettings struct { LeftToRightEmbedding bool `mapstructure:"left-to-right-embedding"` RightToLeftEmbedding bool `mapstructure:"right-to-left-embedding"` diff --git a/pkg/golinters/asasalint.go b/pkg/golinters/asasalint.go new file mode 100644 index 000000000000..e3d0ac9cabbf --- /dev/null +++ b/pkg/golinters/asasalint.go @@ -0,0 +1,26 @@ +package golinters + +import ( + "github.com/alingse/asasalint" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewAsasalint(cfg *config.AsasalintSettings) *goanalysis.Linter { + setting := asasalint.LinterSetting{} + if cfg != nil { + setting.Exclude = cfg.Exclude + setting.NoDefaultExclude = cfg.NoDefaultExclude + setting.IgnoreInTest = cfg.IgnoreInTest + } + a := asasalint.NewAnalyzer(setting) + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index f85a0b16681e..b55d05a4ead7 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -101,6 +101,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) //nolint:funlen func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var ( + asasalintCfg *config.AsasalintSettings bidichkCfg *config.BiDiChkSettings cyclopCfg *config.Cyclop decorderCfg *config.DecorderSettings @@ -171,6 +172,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { ) if m.cfg != nil { + asasalintCfg = &m.cfg.LintersSettings.Asasalint bidichkCfg = &m.cfg.LintersSettings.BiDiChk cyclopCfg = &m.cfg.LintersSettings.Cyclop decorderCfg = &m.cfg.LintersSettings.Decorder @@ -266,6 +268,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { // The linters are sorted in the alphabetical order (case-insensitive). // When a new linter is added the version in `WithSince(...)` must be the next minor version of golangci-lint. lcs := []*linter.Config{ + linter.NewConfig(golinters.NewAsasalint(asasalintCfg)). + WithSince("1.47.0"). + WithPresets(linter.PresetBugs). + WithLoadForGoAnalysis(). + WithURL("https://github.com/alingse/asasalint"), + linter.NewConfig(golinters.NewAsciicheck()). WithSince("v1.26.0"). WithPresets(linter.PresetBugs, linter.PresetStyle). diff --git a/test/testdata/asasalint.go b/test/testdata/asasalint.go new file mode 100644 index 000000000000..ed2af407e9af --- /dev/null +++ b/test/testdata/asasalint.go @@ -0,0 +1,17 @@ +package testdata + +import "fmt" + +func getArgsLength(args ...any) int { + return len(args) +} + +func checkArgsLength(args ...any) int { + return getArgsLength(args) +} + +func someCall() { + var a = []any{1, 2, 3} + fmt.Println(checkArgsLength(a...) == getArgsLength(a)) + fmt.Println(checkArgsLength(a...) == getArgsLength(a...)) +}