File tree 7 files changed +80
-0
lines changed
7 files changed +80
-0
lines changed Original file line number Diff line number Diff line change @@ -1662,6 +1662,20 @@ linters-settings:
1662
1662
# Default: true
1663
1663
begin : false
1664
1664
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
+
1665
1679
unparam :
1666
1680
# Inspect exported functions.
1667
1681
#
@@ -1927,6 +1941,7 @@ linters:
1927
1941
- unconvert
1928
1942
- unparam
1929
1943
- unused
1944
+ - usestdlibvars
1930
1945
- varcheck
1931
1946
- varnamelen
1932
1947
- wastedassign
@@ -2028,6 +2043,7 @@ linters:
2028
2043
- unconvert
2029
2044
- unparam
2030
2045
- unused
2046
+ - usestdlibvars
2031
2047
- varcheck
2032
2048
- varnamelen
2033
2049
- wastedassign
Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ require (
74
74
github.com/ryancurrah/gomodguard v1.2.3
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.7.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 @@ -179,6 +179,7 @@ type LintersSettings struct {
179
179
Thelper ThelperSettings
180
180
Unparam UnparamSettings
181
181
Unused StaticCheckSettings
182
+ UseStdlibVars UseStdlibVarsSettings
182
183
Varcheck VarCheckSettings
183
184
Varnamelen VarnamelenSettings
184
185
Whitespace WhitespaceSettings
@@ -588,6 +589,15 @@ type TenvSettings struct {
588
589
All bool `mapstructure:"all"`
589
590
}
590
591
592
+ type UseStdlibVarsSettings struct {
593
+ HTTPMethod bool `mapstructure:"http-method"`
594
+ HTTPStatusCode bool `mapstructure:"http-status-code"`
595
+ TimeWeekday bool `mapstructure:"time-weekday"`
596
+ TimeMonth bool `mapstructure:"time-month"`
597
+ TimeLayout bool `mapstructure:"time-layout"`
598
+ CryptoHash bool `mapstructure:"crypto-hash"`
599
+ }
600
+
591
601
type UnparamSettings struct {
592
602
CheckExported bool `mapstructure:"check-exported"`
593
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
+ }
24
+ }
25
+
26
+ return goanalysis .NewLinter (
27
+ a .Name ,
28
+ a .Doc ,
29
+ []* analysis.Analyzer {a },
30
+ cfgMap ,
31
+ ).WithLoadMode (goanalysis .LoadModeSyntax )
32
+ }
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
@@ -766,6 +767,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
766
767
WithChangeTypes ().
767
768
WithURL ("https://github.com/dominikh/go-tools/tree/master/unused" ),
768
769
770
+ linter .NewConfig (golinters .NewUseStdlibVars (usestdlibvars )).
771
+ WithSince ("v1.48.0" ).
772
+ WithPresets (linter .PresetStyle ).
773
+ WithURL ("https://github.com/sashamelentyev/usestdlibvars" ),
774
+
769
775
linter .NewConfig (golinters .NewVarcheck (varcheckCfg )).
770
776
WithSince ("v1.0.0" ).
771
777
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