Skip to content

Commit a3c523d

Browse files
feat: add usestdlibvars
1 parent a9dc1ce commit a3c523d

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

.golangci.reference.yml

+16
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,20 @@ linters-settings:
16621662
# Default: true
16631663
begin: false
16641664

1665+
usestdlibvars:
1666+
# Suggest the use of http.MethodXX
1667+
http-method: true
1668+
# Suggest the use of http.StatusXX
1669+
http-status-code: true
1670+
# Suggest the use of time.Weekday
1671+
time-weekday: false
1672+
# Suggest the use of time.Month
1673+
time-month: false
1674+
# Suggest the use of time.Layout
1675+
time-layout: false
1676+
# Suggest the use of crypto.Hash
1677+
crypto-hash: false
1678+
16651679
unparam:
16661680
# Inspect exported functions.
16671681
#
@@ -1927,6 +1941,7 @@ linters:
19271941
- unconvert
19281942
- unparam
19291943
- unused
1944+
- usestdlibvars
19301945
- varcheck
19311946
- varnamelen
19321947
- wastedassign
@@ -2028,6 +2043,7 @@ linters:
20282043
- unconvert
20292044
- unparam
20302045
- unused
2046+
- usestdlibvars
20312047
- varcheck
20322048
- varnamelen
20332049
- wastedassign

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ require (
7474
github.com/ryancurrah/gomodguard v1.2.3
7575
github.com/ryanrolds/sqlclosecheck v0.3.0
7676
github.com/sanposhiho/wastedassign/v2 v2.0.6
77+
github.com/sashamelentyev/usestdlibvars v1.7.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

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+9
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,15 @@ type TenvSettings struct {
588588
All bool `mapstructure:"all"`
589589
}
590590

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+
}
599+
591600
type UnparamSettings struct {
592601
CheckExported bool `mapstructure:"check-exported"`
593602
Algo string

pkg/golinters/usestdlibvars.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}
24+
}
25+
26+
return goanalysis.NewLinter(
27+
a.Name,
28+
a.Doc,
29+
[]*analysis.Analyzer{a},
30+
cfgMap,
31+
).WithLoadMode(goanalysis.LoadModeSyntax)
32+
}

pkg/lint/lintersdb/manager.go

+6
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
@@ -766,6 +767,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
766767
WithChangeTypes().
767768
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),
768769

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

test/testdata/usestdlibvars.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 `can use http.StatusOK instead "200"`
13+
}
14+
15+
func sunday() {
16+
_ = "Sunday" // ERROR `can use time.Sunday.String\(\) instead "Sunday"`
17+
}
18+
19+
func monday() {
20+
_ = "Monday" // ERROR `can use time.Monday.String\(\) instead "Monday"`
21+
}
22+
23+
func tuesday() {
24+
_ = "Tuesday" // ERROR `can use time.Tuesday.String\(\) instead "Tuesday"`
25+
}
26+
27+
func wednesday() {
28+
_ = "Wednesday" // ERROR `can use time.Wednesday.String\(\) instead "Wednesday"`
29+
}
30+
31+
func thursday() {
32+
_ = "Thursday" // ERROR `can use time.Thursday.String\(\) instead "Thursday"`
33+
}
34+
35+
func friday() {
36+
_ = "Friday" // ERROR `can use time.Friday.String\(\) instead "Friday"`
37+
}
38+
39+
func saturday() {
40+
_ = "Saturday" // ERROR `can use time.Saturday.String\(\) instead "Saturday"`
41+
}
42+
43+
func january() {
44+
_ = "January" // ERROR `can use time.January.String\(\) instead "January"`
45+
}
46+
47+
func february() {
48+
_ = "February" // ERROR `can use time.February.String\(\) instead "February"`
49+
}
50+
51+
func march() {
52+
_ = "March" // ERROR `can use time.March.String\(\) instead "March"`
53+
}
54+
55+
func april() {
56+
_ = "April" // ERROR `can use time.April.String\(\) instead "April"`
57+
}
58+
59+
func may() {
60+
_ = "May" // ERROR `can use time.May.String\(\) instead "May"`
61+
}
62+
63+
func june() {
64+
_ = "June" // ERROR `can use time.June.String\(\) instead "June"`
65+
}
66+
67+
func july() {
68+
_ = "July" // ERROR `can use time.July.String\(\) instead "July"`
69+
}
70+
71+
func august() {
72+
_ = "August" // ERROR `can use time.August.String\(\) instead "August"`
73+
}
74+
75+
func september() {
76+
_ = "September" // ERROR `can use time.September.String\(\) instead "September"`
77+
}
78+
79+
func october() {
80+
_ = "October" // ERROR `can use time.October.String\(\) instead "October"`
81+
}
82+
83+
func november() {
84+
_ = "November" // ERROR `can use time.November.String\(\) instead "November"`
85+
}
86+
87+
func december() {
88+
_ = "December" // ERROR `can use time.December.String\(\) instead "December"`
89+
}

0 commit comments

Comments
 (0)