Skip to content

Commit

Permalink
feat: add usestdlibvars
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamelentyev committed Jul 25, 2022
1 parent a9dc1ce commit 987af5d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -1927,6 +1927,7 @@ linters:
- unconvert
- unparam
- unused
- usestdlibvars
- varcheck
- varnamelen
- wastedassign
Expand Down Expand Up @@ -2028,6 +2029,7 @@ linters:
- unconvert
- unparam
- unused
- usestdlibvars
- varcheck
- varnamelen
- wastedassign
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -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.5.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
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/golinters/usestdlibvars.go
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewUseStdlibVars() *goanalysis.Linter {
a := analyzer.New()

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -766,6 +766,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithChangeTypes().
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),

linter.NewConfig(golinters.NewUseStdlibVars()).
WithSince("v1.48.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/sashamelentyev/usestdlibvars"),

linter.NewConfig(golinters.NewVarcheck(varcheckCfg)).
WithSince("v1.0.0").
WithLoadForGoAnalysis().
Expand Down
89 changes: 89 additions & 0 deletions test/testdata/usestdlibvars.go
@@ -0,0 +1,89 @@
//golangcitest:args -Eusestdlibvars
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"`
}

0 comments on commit 987af5d

Please sign in to comment.