Skip to content

Commit f8be199

Browse files
committed
feat: add musttag linter
1 parent 036db83 commit f8be199

File tree

8 files changed

+98
-0
lines changed

8 files changed

+98
-0
lines changed

.golangci.reference.yml

+13
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,17 @@ linters-settings:
11961196
ignore-words:
11971197
- someword
11981198

1199+
musttag:
1200+
# A set of custom functions to check in addition to the builtin ones.
1201+
# Default: []
1202+
functions:
1203+
# The full name of the function, including the package.
1204+
- name: github.com/jmoiron/sqlx.Get
1205+
# The struct tag whose presence should be ensured.
1206+
tag: db
1207+
# The position of the argument to check.
1208+
argpos: 1
1209+
11991210
nakedret:
12001211
# Make an issue if func has more lines of code than this setting, and it has naked returns.
12011212
# Default: 30
@@ -2023,6 +2034,7 @@ linters:
20232034
- makezero
20242035
- maligned
20252036
- misspell
2037+
- musttag
20262038
- nakedret
20272039
- nestif
20282040
- nilerr
@@ -2130,6 +2142,7 @@ linters:
21302142
- makezero
21312143
- maligned
21322144
- misspell
2145+
- musttag
21332146
- nakedret
21342147
- nestif
21352148
- nilerr

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require (
5151
github.com/jingyugao/rowserrcheck v1.1.1
5252
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
5353
github.com/julz/importas v0.1.0
54+
github.com/junk1tm/musttag v0.4.1
5455
github.com/kisielk/errcheck v1.6.2
5556
github.com/kkHAIKE/contextcheck v1.1.3
5657
github.com/kulti/thelper v0.6.3

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
@@ -178,6 +178,7 @@ type LintersSettings struct {
178178
Makezero MakezeroSettings
179179
Maligned MalignedSettings
180180
Misspell MisspellSettings
181+
MustTag MustTagSettings
181182
Nakedret NakedretSettings
182183
Nestif NestifSettings
183184
NilNil NilNilSettings
@@ -531,6 +532,14 @@ type MisspellSettings struct {
531532
IgnoreWords []string `mapstructure:"ignore-words"`
532533
}
533534

535+
type MustTagSettings struct {
536+
Functions []struct {
537+
Name string `mapstructure:"name"`
538+
Tag string `mapstructure:"tag"`
539+
ArgPos int `mapstructure:"argpos"`
540+
} `mapstructure:"functions"`
541+
}
542+
534543
type NakedretSettings struct {
535544
MaxFuncLines int `mapstructure:"max-func-lines"`
536545
}

pkg/golinters/musttag.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package golinters
2+
3+
import (
4+
"github.com/junk1tm/musttag"
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 NewMustTag(setting *config.MustTagSettings) *goanalysis.Linter {
12+
var funcs []musttag.Func
13+
if setting != nil {
14+
for _, fn := range setting.Functions {
15+
funcs = append(funcs, musttag.Func{
16+
Name: fn.Name,
17+
Tag: fn.Tag,
18+
ArgPos: fn.ArgPos,
19+
})
20+
}
21+
}
22+
23+
a := musttag.New(funcs...)
24+
25+
return goanalysis.
26+
NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, nil).
27+
WithLoadMode(goanalysis.LoadModeTypesInfo)
28+
}

pkg/lint/lintersdb/manager.go

+8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
146146
makezeroCfg *config.MakezeroSettings
147147
malignedCfg *config.MalignedSettings
148148
misspellCfg *config.MisspellSettings
149+
musttagCfg *config.MustTagSettings
149150
nakedretCfg *config.NakedretSettings
150151
nestifCfg *config.NestifSettings
151152
nilNilCfg *config.NilNilSettings
@@ -222,6 +223,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
222223
makezeroCfg = &m.cfg.LintersSettings.Makezero
223224
malignedCfg = &m.cfg.LintersSettings.Maligned
224225
misspellCfg = &m.cfg.LintersSettings.Misspell
226+
musttagCfg = &m.cfg.LintersSettings.MustTag
225227
nakedretCfg = &m.cfg.LintersSettings.Nakedret
226228
nestifCfg = &m.cfg.LintersSettings.Nestif
227229
nilNilCfg = &m.cfg.LintersSettings.NilNil
@@ -624,6 +626,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
624626
WithAutoFix().
625627
WithURL("https://github.com/client9/misspell"),
626628

629+
linter.NewConfig(golinters.NewMustTag(musttagCfg)).
630+
WithSince("v1.51.0").
631+
WithLoadForGoAnalysis().
632+
WithPresets(linter.PresetStyle, linter.PresetBugs).
633+
WithURL("https://github.com/junk1tm/musttag"),
634+
627635
linter.NewConfig(golinters.NewNakedret(nakedretCfg)).
628636
WithSince("v1.19.0").
629637
WithPresets(linter.PresetStyle).

test/testdata/configs/musttag.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
linters-settings:
2+
musttag:
3+
functions:
4+
- name: encoding/asn1.Marshal
5+
tag: asn1
6+
argpos: 0
7+
- name: encoding/asn1.Unmarshal
8+
tag: asn1
9+
argpos: 1

test/testdata/musttag.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//golangcitest:args -Emusttag
2+
//golangcitest:config_path testdata/configs/musttag.yml
3+
package testdata
4+
5+
import (
6+
"encoding/asn1"
7+
"encoding/json"
8+
)
9+
10+
// builtin functions:
11+
func musttagJSON() {
12+
var user struct { // want `\Qexported fields should be annotated with the "json" tag`
13+
Name string
14+
Email string `json:"email"`
15+
}
16+
json.Marshal(user)
17+
json.Unmarshal(nil, &user)
18+
}
19+
20+
// custom functions from config:
21+
func musttagCustom() {
22+
var user struct { // want `\Qexported fields should be annotated with the "asn1" tag`
23+
Name string
24+
Email string `asn1:"email"`
25+
}
26+
asn1.Marshal(user)
27+
asn1.Unmarshal(nil, &user)
28+
}

0 commit comments

Comments
 (0)