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 26, 2022
1 parent a9dc1ce commit a828ca9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -1927,6 +1941,7 @@ linters:
- unconvert
- unparam
- unused
- usestdlibvars
- varcheck
- varnamelen
- wastedassign
Expand Down Expand Up @@ -2028,6 +2043,7 @@ linters:
- unconvert
- unparam
- unused
- usestdlibvars
- varcheck
- varnamelen
- wastedassign
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
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.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
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.

10 changes: 10 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type LintersSettings struct {
Thelper ThelperSettings
Unparam UnparamSettings
Unused StaticCheckSettings
UseStdlibVars UseStdlibVarsSettings
Varcheck VarCheckSettings
Varnamelen VarnamelenSettings
Whitespace WhitespaceSettings
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions pkg/golinters/usestdlibvars.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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().
Expand Down
13 changes: 13 additions & 0 deletions test/testdata/usestdlibvars.go
Original file line number Diff line number Diff line change
@@ -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`
}

0 comments on commit a828ca9

Please sign in to comment.