feat: add usestdlibvars (#3016)
This commit is contained in:
parent
15cba447fd
commit
0ba1388a41
@ -1669,6 +1669,29 @@ linters-settings:
|
|||||||
# Default: true
|
# Default: true
|
||||||
begin: false
|
begin: false
|
||||||
|
|
||||||
|
usestdlibvars:
|
||||||
|
# Suggest the use of http.MethodXX
|
||||||
|
# Default: true
|
||||||
|
http-method: false
|
||||||
|
# Suggest the use of http.StatusXX
|
||||||
|
# Default: true
|
||||||
|
http-status-code: false
|
||||||
|
# Suggest the use of time.Weekday
|
||||||
|
# Default: true
|
||||||
|
time-weekday: true
|
||||||
|
# Suggest the use of time.Month
|
||||||
|
# Default: false
|
||||||
|
time-month: true
|
||||||
|
# Suggest the use of time.Layout
|
||||||
|
# Default: false
|
||||||
|
time-layout: true
|
||||||
|
# Suggest the use of crypto.Hash
|
||||||
|
# Default: false
|
||||||
|
crypto-hash: true
|
||||||
|
# Suggest the use of pc.DefaultXXPath
|
||||||
|
# Default: false
|
||||||
|
default-rpc-path: true
|
||||||
|
|
||||||
unparam:
|
unparam:
|
||||||
# Inspect exported functions.
|
# Inspect exported functions.
|
||||||
#
|
#
|
||||||
@ -1934,6 +1957,7 @@ linters:
|
|||||||
- unconvert
|
- unconvert
|
||||||
- unparam
|
- unparam
|
||||||
- unused
|
- unused
|
||||||
|
- usestdlibvars
|
||||||
- varcheck
|
- varcheck
|
||||||
- varnamelen
|
- varnamelen
|
||||||
- wastedassign
|
- wastedassign
|
||||||
@ -2035,6 +2059,7 @@ linters:
|
|||||||
- unconvert
|
- unconvert
|
||||||
- unparam
|
- unparam
|
||||||
- unused
|
- unused
|
||||||
|
- usestdlibvars
|
||||||
- varcheck
|
- varcheck
|
||||||
- varnamelen
|
- varnamelen
|
||||||
- wastedassign
|
- wastedassign
|
||||||
|
1
go.mod
1
go.mod
@ -74,6 +74,7 @@ require (
|
|||||||
github.com/ryancurrah/gomodguard v1.2.4
|
github.com/ryancurrah/gomodguard v1.2.4
|
||||||
github.com/ryanrolds/sqlclosecheck v0.3.0
|
github.com/ryanrolds/sqlclosecheck v0.3.0
|
||||||
github.com/sanposhiho/wastedassign/v2 v2.0.6
|
github.com/sanposhiho/wastedassign/v2 v2.0.6
|
||||||
|
github.com/sashamelentyev/usestdlibvars v1.8.0
|
||||||
github.com/securego/gosec/v2 v2.12.0
|
github.com/securego/gosec/v2 v2.12.0
|
||||||
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
|
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
|
||||||
github.com/shirou/gopsutil/v3 v3.22.6
|
github.com/shirou/gopsutil/v3 v3.22.6
|
||||||
|
2
go.sum
generated
2
go.sum
generated
@ -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/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 h1:+6/hQIHKNJAUixEj6EmOngGIisyeI+T3335lYTyxRoA=
|
||||||
github.com/sanposhiho/wastedassign/v2 v2.0.6/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI=
|
github.com/sanposhiho/wastedassign/v2 v2.0.6/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI=
|
||||||
|
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 h1:CQWdW7ATFpvLSohMVsajscfyHJ5rsGmEXmsNcsDNmAg=
|
||||||
github.com/securego/gosec/v2 v2.12.0/go.mod h1:iTpT+eKTw59bSgklBHlSnH5O2tNygHMDxfvMubA4i7I=
|
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=
|
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
|
@ -178,6 +178,7 @@ type LintersSettings struct {
|
|||||||
Thelper ThelperSettings
|
Thelper ThelperSettings
|
||||||
Unparam UnparamSettings
|
Unparam UnparamSettings
|
||||||
Unused StaticCheckSettings
|
Unused StaticCheckSettings
|
||||||
|
UseStdlibVars UseStdlibVarsSettings
|
||||||
Varcheck VarCheckSettings
|
Varcheck VarCheckSettings
|
||||||
Varnamelen VarnamelenSettings
|
Varnamelen VarnamelenSettings
|
||||||
Whitespace WhitespaceSettings
|
Whitespace WhitespaceSettings
|
||||||
@ -587,6 +588,16 @@ type TenvSettings struct {
|
|||||||
All bool `mapstructure:"all"`
|
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"`
|
||||||
|
DefaultRPCPathFlag bool `mapstructure:"default-rpc-path"`
|
||||||
|
}
|
||||||
|
|
||||||
type UnparamSettings struct {
|
type UnparamSettings struct {
|
||||||
CheckExported bool `mapstructure:"check-exported"`
|
CheckExported bool `mapstructure:"check-exported"`
|
||||||
Algo string
|
Algo string
|
||||||
|
33
pkg/golinters/usestdlibvars.go
Normal file
33
pkg/golinters/usestdlibvars.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
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,
|
||||||
|
analyzer.DefaultRPCPathFlag: cfg.DefaultRPCPathFlag,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return goanalysis.NewLinter(
|
||||||
|
a.Name,
|
||||||
|
a.Doc,
|
||||||
|
[]*analysis.Analyzer{a},
|
||||||
|
cfgMap,
|
||||||
|
).WithLoadMode(goanalysis.LoadModeSyntax)
|
||||||
|
}
|
@ -164,6 +164,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
|||||||
thelperCfg *config.ThelperSettings
|
thelperCfg *config.ThelperSettings
|
||||||
unparamCfg *config.UnparamSettings
|
unparamCfg *config.UnparamSettings
|
||||||
unusedCfg *config.StaticCheckSettings
|
unusedCfg *config.StaticCheckSettings
|
||||||
|
usestdlibvars *config.UseStdlibVarsSettings
|
||||||
varcheckCfg *config.VarCheckSettings
|
varcheckCfg *config.VarCheckSettings
|
||||||
varnamelenCfg *config.VarnamelenSettings
|
varnamelenCfg *config.VarnamelenSettings
|
||||||
whitespaceCfg *config.WhitespaceSettings
|
whitespaceCfg *config.WhitespaceSettings
|
||||||
@ -767,6 +768,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
|||||||
WithChangeTypes().
|
WithChangeTypes().
|
||||||
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),
|
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)).
|
linter.NewConfig(golinters.NewVarcheck(varcheckCfg)).
|
||||||
WithSince("v1.0.0").
|
WithSince("v1.0.0").
|
||||||
WithLoadForGoAnalysis().
|
WithLoadForGoAnalysis().
|
||||||
|
13
test/testdata/usestdlibvars.go
vendored
Normal file
13
test/testdata/usestdlibvars.go
vendored
Normal file
@ -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`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user