From a828ca944337c52e2c133d3176f35ed65e0ae357 Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Mon, 25 Jul 2022 11:10:01 +0300 Subject: [PATCH 1/3] feat: add usestdlibvars --- .golangci.reference.yml | 16 ++++++++++++++++ go.mod | 1 + go.sum | 2 ++ pkg/config/linters_settings.go | 10 ++++++++++ pkg/golinters/usestdlibvars.go | 32 ++++++++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 6 ++++++ test/testdata/usestdlibvars.go | 13 +++++++++++++ 7 files changed, 80 insertions(+) create mode 100644 pkg/golinters/usestdlibvars.go create mode 100644 test/testdata/usestdlibvars.go diff --git a/.golangci.reference.yml b/.golangci.reference.yml index dd1459c6b2e5..0448b34159e5 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1662,6 +1662,20 @@ linters-settings: # Default: true begin: false + usestdlibvars: + # Suggest the use of http.MethodXX + http-method: true + # Suggest the use of http.StatusXX + http-status-code: true + # Suggest the use of time.Weekday + time-weekday: false + # Suggest the use of time.Month + time-month: false + # Suggest the use of time.Layout + time-layout: false + # Suggest the use of crypto.Hash + crypto-hash: false + unparam: # Inspect exported functions. # @@ -1927,6 +1941,7 @@ linters: - unconvert - unparam - unused + - usestdlibvars - varcheck - varnamelen - wastedassign @@ -2028,6 +2043,7 @@ linters: - unconvert - unparam - unused + - usestdlibvars - varcheck - varnamelen - wastedassign diff --git a/go.mod b/go.mod index b8a76694618f..6df5fb233f52 100644 --- a/go.mod +++ b/go.mod @@ -74,6 +74,7 @@ require ( github.com/ryancurrah/gomodguard v1.2.3 github.com/ryanrolds/sqlclosecheck v0.3.0 github.com/sanposhiho/wastedassign/v2 v2.0.6 + github.com/sashamelentyev/usestdlibvars v1.7.0 github.com/securego/gosec/v2 v2.12.0 github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c github.com/shirou/gopsutil/v3 v3.22.6 diff --git a/go.sum b/go.sum index 80931b84c0e2..3d273f7200b0 100644 --- a/go.sum +++ b/go.sum @@ -600,6 +600,8 @@ github.com/ryanrolds/sqlclosecheck v0.3.0 h1:AZx+Bixh8zdUBxUA1NxbxVAS78vTPq4rCb8 github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA= github.com/sanposhiho/wastedassign/v2 v2.0.6 h1:+6/hQIHKNJAUixEj6EmOngGIisyeI+T3335lYTyxRoA= github.com/sanposhiho/wastedassign/v2 v2.0.6/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI= +github.com/sashamelentyev/usestdlibvars v1.7.0 h1:jjDzfl4RjcUWer1EwhNyipT+f+LL0HmcuGF0jDhb7sM= +github.com/sashamelentyev/usestdlibvars v1.7.0/go.mod h1:BFt7b5mSVHaaa26ZupiNRV2ODViQBxZZVhtAxAJRrjs= github.com/securego/gosec/v2 v2.12.0 h1:CQWdW7ATFpvLSohMVsajscfyHJ5rsGmEXmsNcsDNmAg= github.com/securego/gosec/v2 v2.12.0/go.mod h1:iTpT+eKTw59bSgklBHlSnH5O2tNygHMDxfvMubA4i7I= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index b0bc1ac82ffd..a2a21423b517 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -179,6 +179,7 @@ type LintersSettings struct { Thelper ThelperSettings Unparam UnparamSettings Unused StaticCheckSettings + UseStdlibVars UseStdlibVarsSettings Varcheck VarCheckSettings Varnamelen VarnamelenSettings Whitespace WhitespaceSettings @@ -588,6 +589,15 @@ type TenvSettings struct { All bool `mapstructure:"all"` } +type UseStdlibVarsSettings struct { + HTTPMethod bool `mapstructure:"http-method"` + HTTPStatusCode bool `mapstructure:"http-status-code"` + TimeWeekday bool `mapstructure:"time-weekday"` + TimeMonth bool `mapstructure:"time-month"` + TimeLayout bool `mapstructure:"time-layout"` + CryptoHash bool `mapstructure:"crypto-hash"` +} + type UnparamSettings struct { CheckExported bool `mapstructure:"check-exported"` Algo string diff --git a/pkg/golinters/usestdlibvars.go b/pkg/golinters/usestdlibvars.go new file mode 100644 index 000000000000..4156c903abbb --- /dev/null +++ b/pkg/golinters/usestdlibvars.go @@ -0,0 +1,32 @@ +package golinters + +import ( + "github.com/sashamelentyev/usestdlibvars/pkg/analyzer" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewUseStdlibVars(cfg *config.UseStdlibVarsSettings) *goanalysis.Linter { + a := analyzer.New() + + cfgMap := make(map[string]map[string]interface{}) + if cfg != nil { + cfgMap[a.Name] = map[string]interface{}{ + analyzer.HTTPMethodFlag: cfg.HTTPMethod, + analyzer.HTTPStatusCodeFlag: cfg.HTTPStatusCode, + analyzer.TimeWeekdayFlag: cfg.TimeWeekday, + analyzer.TimeMonthFlag: cfg.TimeMonth, + analyzer.TimeLayoutFlag: cfg.TimeLayout, + analyzer.CryptoHashFlag: cfg.CryptoHash, + } + } + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + cfgMap, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 048143018dc4..7e92af686af3 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -164,6 +164,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { thelperCfg *config.ThelperSettings unparamCfg *config.UnparamSettings unusedCfg *config.StaticCheckSettings + usestdlibvars *config.UseStdlibVarsSettings varcheckCfg *config.VarCheckSettings varnamelenCfg *config.VarnamelenSettings whitespaceCfg *config.WhitespaceSettings @@ -766,6 +767,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithChangeTypes(). WithURL("https://github.com/dominikh/go-tools/tree/master/unused"), + linter.NewConfig(golinters.NewUseStdlibVars(usestdlibvars)). + WithSince("v1.48.0"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/sashamelentyev/usestdlibvars"), + linter.NewConfig(golinters.NewVarcheck(varcheckCfg)). WithSince("v1.0.0"). WithLoadForGoAnalysis(). diff --git a/test/testdata/usestdlibvars.go b/test/testdata/usestdlibvars.go new file mode 100644 index 000000000000..c3c4e46324ae --- /dev/null +++ b/test/testdata/usestdlibvars.go @@ -0,0 +1,13 @@ +//golangcitest:args -Eusestdlibvars +package testdata + +import "net/http" + +func _200() { + _ = 200 +} + +func _200_1() { + var w http.ResponseWriter + w.WriteHeader(200) // ERROR `"200" can be replaced by http.StatusOK` +} From 8b17573a55955d73424b9b44faee25790c175e51 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 30 Jul 2022 20:09:08 +0200 Subject: [PATCH 2/3] review --- .golangci.reference.yml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 0448b34159e5..ca9bd8ba1cb2 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1664,17 +1664,23 @@ linters-settings: usestdlibvars: # Suggest the use of http.MethodXX - http-method: true + # Default: true + http-method: false # Suggest the use of http.StatusXX - http-status-code: true + # Default: true + http-status-code: false # Suggest the use of time.Weekday - time-weekday: false + # Default: true + time-weekday: true # Suggest the use of time.Month - time-month: false + # Default: false + time-month: true # Suggest the use of time.Layout - time-layout: false + # Default: false + time-layout: true # Suggest the use of crypto.Hash - crypto-hash: false + # Default: false + crypto-hash: true unparam: # Inspect exported functions. From 61d8ae09e80827cb45f9dcdde2b8a5ce5ab84163 Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Tue, 2 Aug 2022 16:07:37 +0300 Subject: [PATCH 3/3] feat: add usestdlibvars --- .golangci.reference.yml | 3 +++ go.mod | 2 +- go.sum | 4 ++-- pkg/config/linters_settings.go | 13 +++++++------ pkg/golinters/usestdlibvars.go | 1 + 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index d9e721c6ea8c..27123e6414d6 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1688,6 +1688,9 @@ linters-settings: # Suggest the use of crypto.Hash # Default: false crypto-hash: true + # Suggest the use of pc.DefaultXXPath + # Default: false + default-rpc-path: true unparam: # Inspect exported functions. diff --git a/go.mod b/go.mod index 399d595f8a71..259f16a4829f 100644 --- a/go.mod +++ b/go.mod @@ -74,7 +74,7 @@ require ( github.com/ryancurrah/gomodguard v1.2.4 github.com/ryanrolds/sqlclosecheck v0.3.0 github.com/sanposhiho/wastedassign/v2 v2.0.6 - github.com/sashamelentyev/usestdlibvars v1.7.0 + github.com/sashamelentyev/usestdlibvars v1.8.0 github.com/securego/gosec/v2 v2.12.0 github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c github.com/shirou/gopsutil/v3 v3.22.6 diff --git a/go.sum b/go.sum index d4c056faebea..aefdfdb50238 100644 --- a/go.sum +++ b/go.sum @@ -600,8 +600,8 @@ github.com/ryanrolds/sqlclosecheck v0.3.0 h1:AZx+Bixh8zdUBxUA1NxbxVAS78vTPq4rCb8 github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA= github.com/sanposhiho/wastedassign/v2 v2.0.6 h1:+6/hQIHKNJAUixEj6EmOngGIisyeI+T3335lYTyxRoA= github.com/sanposhiho/wastedassign/v2 v2.0.6/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI= -github.com/sashamelentyev/usestdlibvars v1.7.0 h1:jjDzfl4RjcUWer1EwhNyipT+f+LL0HmcuGF0jDhb7sM= -github.com/sashamelentyev/usestdlibvars v1.7.0/go.mod h1:BFt7b5mSVHaaa26ZupiNRV2ODViQBxZZVhtAxAJRrjs= +github.com/sashamelentyev/usestdlibvars v1.8.0 h1:QnWP9IOEuRyYKH+IG0LlQIjuJlc0rfdo4K3/Zh3WRMw= +github.com/sashamelentyev/usestdlibvars v1.8.0/go.mod h1:BFt7b5mSVHaaa26ZupiNRV2ODViQBxZZVhtAxAJRrjs= github.com/securego/gosec/v2 v2.12.0 h1:CQWdW7ATFpvLSohMVsajscfyHJ5rsGmEXmsNcsDNmAg= github.com/securego/gosec/v2 v2.12.0/go.mod h1:iTpT+eKTw59bSgklBHlSnH5O2tNygHMDxfvMubA4i7I= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index fdb0e05baa1a..4c47514cb6f7 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -588,12 +588,13 @@ type TenvSettings struct { } type UseStdlibVarsSettings struct { - HTTPMethod bool `mapstructure:"http-method"` - HTTPStatusCode bool `mapstructure:"http-status-code"` - TimeWeekday bool `mapstructure:"time-weekday"` - TimeMonth bool `mapstructure:"time-month"` - TimeLayout bool `mapstructure:"time-layout"` - CryptoHash bool `mapstructure:"crypto-hash"` + HTTPMethod bool `mapstructure:"http-method"` + HTTPStatusCode bool `mapstructure:"http-status-code"` + TimeWeekday bool `mapstructure:"time-weekday"` + TimeMonth bool `mapstructure:"time-month"` + TimeLayout bool `mapstructure:"time-layout"` + CryptoHash bool `mapstructure:"crypto-hash"` + DefaultRPCPathFlag bool `mapstructure:"default-rpc-path"` } type UnparamSettings struct { diff --git a/pkg/golinters/usestdlibvars.go b/pkg/golinters/usestdlibvars.go index 4156c903abbb..dbb6d953b2b3 100644 --- a/pkg/golinters/usestdlibvars.go +++ b/pkg/golinters/usestdlibvars.go @@ -20,6 +20,7 @@ func NewUseStdlibVars(cfg *config.UseStdlibVarsSettings) *goanalysis.Linter { analyzer.TimeMonthFlag: cfg.TimeMonth, analyzer.TimeLayoutFlag: cfg.TimeLayout, analyzer.CryptoHashFlag: cfg.CryptoHash, + analyzer.DefaultRPCPathFlag: cfg.DefaultRPCPathFlag, } }