File tree 7 files changed +91
-0
lines changed
7 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -1669,6 +1669,29 @@ linters-settings:
1669
1669
# Default: true
1670
1670
begin : false
1671
1671
1672
+ usestdlibvars :
1673
+ # Suggest the use of http.MethodXX
1674
+ # Default: true
1675
+ http-method : false
1676
+ # Suggest the use of http.StatusXX
1677
+ # Default: true
1678
+ http-status-code : false
1679
+ # Suggest the use of time.Weekday
1680
+ # Default: true
1681
+ time-weekday : true
1682
+ # Suggest the use of time.Month
1683
+ # Default: false
1684
+ time-month : true
1685
+ # Suggest the use of time.Layout
1686
+ # Default: false
1687
+ time-layout : true
1688
+ # Suggest the use of crypto.Hash
1689
+ # Default: false
1690
+ crypto-hash : true
1691
+ # Suggest the use of pc.DefaultXXPath
1692
+ # Default: false
1693
+ default-rpc-path : true
1694
+
1672
1695
unparam :
1673
1696
# Inspect exported functions.
1674
1697
#
@@ -1934,6 +1957,7 @@ linters:
1934
1957
- unconvert
1935
1958
- unparam
1936
1959
- unused
1960
+ - usestdlibvars
1937
1961
- varcheck
1938
1962
- varnamelen
1939
1963
- wastedassign
@@ -2035,6 +2059,7 @@ linters:
2035
2059
- unconvert
2036
2060
- unparam
2037
2061
- unused
2062
+ - usestdlibvars
2038
2063
- varcheck
2039
2064
- varnamelen
2040
2065
- wastedassign
Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ require (
74
74
github.com/ryancurrah/gomodguard v1.2.4
75
75
github.com/ryanrolds/sqlclosecheck v0.3.0
76
76
github.com/sanposhiho/wastedassign/v2 v2.0.6
77
+ github.com/sashamelentyev/usestdlibvars v1.8.0
77
78
github.com/securego/gosec/v2 v2.12.0
78
79
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
79
80
github.com/shirou/gopsutil/v3 v3.22.6
Original file line number Diff line number Diff line change @@ -178,6 +178,7 @@ type LintersSettings struct {
178
178
Thelper ThelperSettings
179
179
Unparam UnparamSettings
180
180
Unused StaticCheckSettings
181
+ UseStdlibVars UseStdlibVarsSettings
181
182
Varcheck VarCheckSettings
182
183
Varnamelen VarnamelenSettings
183
184
Whitespace WhitespaceSettings
@@ -587,6 +588,16 @@ type TenvSettings struct {
587
588
All bool `mapstructure:"all"`
588
589
}
589
590
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
+
590
601
type UnparamSettings struct {
591
602
CheckExported bool `mapstructure:"check-exported"`
592
603
Algo string
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -164,6 +164,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
164
164
thelperCfg * config.ThelperSettings
165
165
unparamCfg * config.UnparamSettings
166
166
unusedCfg * config.StaticCheckSettings
167
+ usestdlibvars * config.UseStdlibVarsSettings
167
168
varcheckCfg * config.VarCheckSettings
168
169
varnamelenCfg * config.VarnamelenSettings
169
170
whitespaceCfg * config.WhitespaceSettings
@@ -767,6 +768,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
767
768
WithChangeTypes ().
768
769
WithURL ("https://github.com/dominikh/go-tools/tree/master/unused" ),
769
770
771
+ linter .NewConfig (golinters .NewUseStdlibVars (usestdlibvars )).
772
+ WithSince ("v1.48.0" ).
773
+ WithPresets (linter .PresetStyle ).
774
+ WithURL ("https://github.com/sashamelentyev/usestdlibvars" ),
775
+
770
776
linter .NewConfig (golinters .NewVarcheck (varcheckCfg )).
771
777
WithSince ("v1.0.0" ).
772
778
WithLoadForGoAnalysis ().
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments