Skip to content

Commit 66a1abd

Browse files
sashamelentyevSeigeC
authored andcommitted
feat: add usestdlibvars (golangci#3016)
1 parent 289b662 commit 66a1abd

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

.golangci.reference.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,29 @@ linters-settings:
16941694
# Default: true
16951695
begin: false
16961696

1697+
usestdlibvars:
1698+
# Suggest the use of http.MethodXX
1699+
# Default: true
1700+
http-method: false
1701+
# Suggest the use of http.StatusXX
1702+
# Default: true
1703+
http-status-code: false
1704+
# Suggest the use of time.Weekday
1705+
# Default: true
1706+
time-weekday: true
1707+
# Suggest the use of time.Month
1708+
# Default: false
1709+
time-month: true
1710+
# Suggest the use of time.Layout
1711+
# Default: false
1712+
time-layout: true
1713+
# Suggest the use of crypto.Hash
1714+
# Default: false
1715+
crypto-hash: true
1716+
# Suggest the use of pc.DefaultXXPath
1717+
# Default: false
1718+
default-rpc-path: true
1719+
16971720
unparam:
16981721
# Inspect exported functions.
16991722
#
@@ -1959,6 +1982,7 @@ linters:
19591982
- unconvert
19601983
- unparam
19611984
- unused
1985+
- usestdlibvars
19621986
- varcheck
19631987
- varnamelen
19641988
- wastedassign
@@ -2060,6 +2084,7 @@ linters:
20602084
- unconvert
20612085
- unparam
20622086
- unused
2087+
- usestdlibvars
20632088
- varcheck
20642089
- varnamelen
20652090
- wastedassign

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ require (
7474
github.com/ryancurrah/gomodguard v1.2.4
7575
github.com/ryanrolds/sqlclosecheck v0.3.0
7676
github.com/sanposhiho/wastedassign/v2 v2.0.6
77+
github.com/sashamelentyev/usestdlibvars v1.8.0
7778
github.com/securego/gosec/v2 v2.12.0
7879
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
7980
github.com/shirou/gopsutil/v3 v3.22.6

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ type LintersSettings struct {
178178
Thelper ThelperSettings
179179
Unparam UnparamSettings
180180
Unused StaticCheckSettings
181+
UseStdlibVars UseStdlibVarsSettings
181182
Varcheck VarCheckSettings
182183
Varnamelen VarnamelenSettings
183184
Whitespace WhitespaceSettings
@@ -587,6 +588,16 @@ type TenvSettings struct {
587588
All bool `mapstructure:"all"`
588589
}
589590

591+
type UseStdlibVarsSettings struct {
592+
HTTPMethod bool `mapstructure:"http-method"`
593+
HTTPStatusCode bool `mapstructure:"http-status-code"`
594+
TimeWeekday bool `mapstructure:"time-weekday"`
595+
TimeMonth bool `mapstructure:"time-month"`
596+
TimeLayout bool `mapstructure:"time-layout"`
597+
CryptoHash bool `mapstructure:"crypto-hash"`
598+
DefaultRPCPathFlag bool `mapstructure:"default-rpc-path"`
599+
}
600+
590601
type UnparamSettings struct {
591602
CheckExported bool `mapstructure:"check-exported"`
592603
Algo string

pkg/golinters/usestdlibvars.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package golinters
2+
3+
import (
4+
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
11+
func NewUseStdlibVars(cfg *config.UseStdlibVarsSettings) *goanalysis.Linter {
12+
a := analyzer.New()
13+
14+
cfgMap := make(map[string]map[string]interface{})
15+
if cfg != nil {
16+
cfgMap[a.Name] = map[string]interface{}{
17+
analyzer.HTTPMethodFlag: cfg.HTTPMethod,
18+
analyzer.HTTPStatusCodeFlag: cfg.HTTPStatusCode,
19+
analyzer.TimeWeekdayFlag: cfg.TimeWeekday,
20+
analyzer.TimeMonthFlag: cfg.TimeMonth,
21+
analyzer.TimeLayoutFlag: cfg.TimeLayout,
22+
analyzer.CryptoHashFlag: cfg.CryptoHash,
23+
analyzer.DefaultRPCPathFlag: cfg.DefaultRPCPathFlag,
24+
}
25+
}
26+
27+
return goanalysis.NewLinter(
28+
a.Name,
29+
a.Doc,
30+
[]*analysis.Analyzer{a},
31+
cfgMap,
32+
).WithLoadMode(goanalysis.LoadModeSyntax)
33+
}

pkg/lint/lintersdb/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
164164
thelperCfg *config.ThelperSettings
165165
unparamCfg *config.UnparamSettings
166166
unusedCfg *config.StaticCheckSettings
167+
usestdlibvars *config.UseStdlibVarsSettings
167168
varcheckCfg *config.VarCheckSettings
168169
varnamelenCfg *config.VarnamelenSettings
169170
whitespaceCfg *config.WhitespaceSettings
@@ -767,6 +768,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
767768
WithChangeTypes().
768769
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),
769770

771+
linter.NewConfig(golinters.NewUseStdlibVars(usestdlibvars)).
772+
WithSince("v1.48.0").
773+
WithPresets(linter.PresetStyle).
774+
WithURL("https://github.com/sashamelentyev/usestdlibvars"),
775+
770776
linter.NewConfig(golinters.NewVarcheck(varcheckCfg)).
771777
WithSince("v1.0.0").
772778
WithLoadForGoAnalysis().

test/testdata/usestdlibvars.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//golangcitest:args -Eusestdlibvars
2+
package testdata
3+
4+
import "net/http"
5+
6+
func _200() {
7+
_ = 200
8+
}
9+
10+
func _200_1() {
11+
var w http.ResponseWriter
12+
w.WriteHeader(200) // ERROR `"200" can be replaced by http.StatusOK`
13+
}

0 commit comments

Comments
 (0)