diff --git a/.golangci.reference.yml b/.golangci.reference.yml index dd1459c6b2e5..2266434c51cb 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1911,6 +1911,7 @@ linters: - prealloc - predeclared - promlinter + - reusestdlibvars - revive - rowserrcheck - scopelint @@ -2012,6 +2013,7 @@ linters: - prealloc - predeclared - promlinter + - reusestdlibvars - revive - rowserrcheck - scopelint diff --git a/go.mod b/go.mod index b8a76694618f..1be126debf36 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/reusestdlibvars v1.3.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..4c912b6a77db 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/reusestdlibvars v1.3.0 h1:z+vuf5N8oH5cqTmuB6FnD4YgRrRxyMkfpz3waVxGkUA= +github.com/sashamelentyev/reusestdlibvars v1.3.0/go.mod h1:HjPjJzew6PcxQAQrvLIq0Wl5x0R/nxD79UGsPJxyX6Y= 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/golinters/reusestdlibvars.go b/pkg/golinters/reusestdlibvars.go new file mode 100644 index 000000000000..f57130347b59 --- /dev/null +++ b/pkg/golinters/reusestdlibvars.go @@ -0,0 +1,19 @@ +package golinters + +import ( + "github.com/sashamelentyev/reusestdlibvars/pkg/analyzer" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewReuseStdlibVars() *goanalysis.Linter { + a := analyzer.New() + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 048143018dc4..36b937a4c744 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -665,6 +665,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/yeya24/promlinter"), + linter.NewConfig(golinters.NewReuseStdlibVars()). + WithSince("v1.48.0"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/sashamelentyev/reusestdlibvars"), + linter.NewConfig(golinters.NewRevive(reviveCfg)). WithSince("v1.37.0"). WithPresets(linter.PresetStyle, linter.PresetMetaLinter). diff --git a/test/testdata/reusestdlibvars.go b/test/testdata/reusestdlibvars.go new file mode 100644 index 000000000000..a0d601754fbb --- /dev/null +++ b/test/testdata/reusestdlibvars.go @@ -0,0 +1,89 @@ +//golangcitest:args -Ereusestdlibvars +package testdata + +import "net/http" + +func _200() { + _ = 200 +} + +func _200_1() { + var w http.ResponseWriter + w.WriteHeader(200) // ERROR `can use http.StatusOK instead "200"` +} + +func sunday() { + _ = "Sunday" // ERROR `can use time.Sunday.String\(\) instead "Sunday"` +} + +func monday() { + _ = "Monday" // ERROR `can use time.Monday.String\(\) instead "Monday"` +} + +func tuesday() { + _ = "Tuesday" // ERROR `can use time.Tuesday.String\(\) instead "Tuesday"` +} + +func wednesday() { + _ = "Wednesday" // ERROR `can use time.Wednesday.String\(\) instead "Wednesday"` +} + +func thursday() { + _ = "Thursday" // ERROR `can use time.Thursday.String\(\) instead "Thursday"` +} + +func friday() { + _ = "Friday" // ERROR `can use time.Friday.String\(\) instead "Friday"` +} + +func saturday() { + _ = "Saturday" // ERROR `can use time.Saturday.String\(\) instead "Saturday"` +} + +func january() { + _ = "January" // ERROR `can use time.January.String\(\) instead "January"` +} + +func february() { + _ = "February" // ERROR `can use time.February.String\(\) instead "February"` +} + +func march() { + _ = "March" // ERROR `can use time.March.String\(\) instead "March"` +} + +func april() { + _ = "April" // ERROR `can use time.April.String\(\) instead "April"` +} + +func may() { + _ = "May" // ERROR `can use time.May.String\(\) instead "May"` +} + +func june() { + _ = "June" // ERROR `can use time.June.String\(\) instead "June"` +} + +func july() { + _ = "July" // ERROR `can use time.July.String\(\) instead "July"` +} + +func august() { + _ = "August" // ERROR `can use time.August.String\(\) instead "August"` +} + +func september() { + _ = "September" // ERROR `can use time.September.String\(\) instead "September"` +} + +func october() { + _ = "October" // ERROR `can use time.October.String\(\) instead "October"` +} + +func november() { + _ = "November" // ERROR `can use time.November.String\(\) instead "November"` +} + +func december() { + _ = "December" // ERROR `can use time.December.String\(\) instead "December"` +}