diff --git a/musttag.go b/musttag.go index 5fb6bb5..1254288 100644 --- a/musttag.go +++ b/musttag.go @@ -81,9 +81,6 @@ func flags(funcs *[]Func) flag.FlagSet { // for tests only. var ( - // should the same struct be reported only once for the same tag? - reportOnce = true - reportf = func(pass *analysis.Pass, st *structType, fn Func, fnPos token.Position) { const format = "`%s` should be annotated with the `%s` tag as it is passed to `%s` at %s" pass.Reportf(st.Pos, format, st.Name, fn.Tag, fn.shortName(), fnPos) @@ -105,14 +102,6 @@ func run(pass *analysis.Pass, funcs map[string]Func) (any, error) { modulePackages[pkg] = struct{}{} } - // store previous reports to prevent reporting - // the same struct more than once (if reportOnce is true). - type report struct { - pos token.Pos // the position for report. - tag string // the missing struct tag. - } - reports := make(map[report]struct{}) - walk := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) filter := []ast.Node{(*ast.CallExpr)(nil)} @@ -168,15 +157,9 @@ func run(pass *analysis.Pass, funcs map[string]Func) (any, error) { return // nothing to report. } - r := report{result.Pos, fn.Tag} - if _, ok := reports[r]; ok && reportOnce { - return // already reported. - } - p := pass.Fset.Position(call.Pos()) p.Filename, _ = filepath.Rel(moduleDir, p.Filename) reportf(pass, result, fn, p) - reports[r] = struct{}{} }) return nil, nil diff --git a/musttag_test.go b/musttag_test.go index a671f09..459ccb1 100644 --- a/musttag_test.go +++ b/musttag_test.go @@ -25,18 +25,11 @@ func TestAnalyzer(t *testing.T) { ) t.Run("builtins", func(t *testing.T) { - original := reportf - reportf = func(pass *analysis.Pass, st *structType, fn Func, fnPos token.Position) { - fnPos.Line = 0 // it's annoying to fix line numbers expectations when a new line is added. - original(pass, st, fn, fnPos) - } testdata := analysistest.TestData() analysistest.Run(t, testdata, analyzer, "builtins") }) t.Run("tests", func(t *testing.T) { - // for the tests we want to record reports from all functions. - reportOnce = false reportf = func(pass *analysis.Pass, st *structType, fn Func, fnPos token.Position) { pass.Reportf(st.Pos, fn.shortName()) } diff --git a/testdata/builtins.go b/testdata/builtins.go index ce11bc2..98c8fae 100644 --- a/testdata/builtins.go +++ b/testdata/builtins.go @@ -11,15 +11,40 @@ import ( "gopkg.in/yaml.v3" ) -// TODO(junk1tm): drop `reportOnce` and test each builtin function. - type User struct { /* want "`User` should be annotated with the `json` tag as it is passed to `json.Marshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `json` tag as it is passed to `json.MarshalIndent` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `json` tag as it is passed to `json.Unmarshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `json` tag as it is passed to `json.Encoder.Encode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `json` tag as it is passed to `json.Decoder.Decode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `xml` tag as it is passed to `xml.Marshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `xml` tag as it is passed to `xml.MarshalIndent` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `xml` tag as it is passed to `xml.Unmarshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `xml` tag as it is passed to `xml.Encoder.Encode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `xml` tag as it is passed to `xml.Decoder.Decode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `xml` tag as it is passed to `xml.Encoder.EncodeElement` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `xml` tag as it is passed to `xml.Decoder.DecodeElement` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `yaml` tag as it is passed to `yaml.v3.Marshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `yaml` tag as it is passed to `yaml.v3.Unmarshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `yaml` tag as it is passed to `yaml.v3.Encoder.Encode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `yaml` tag as it is passed to `yaml.v3.Decoder.Decode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `toml` tag as it is passed to `toml.Unmarshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `toml` tag as it is passed to `toml.Decode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `toml` tag as it is passed to `toml.DecodeFS` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `toml` tag as it is passed to `toml.DecodeFile` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `toml` tag as it is passed to `toml.Encoder.Encode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `toml` tag as it is passed to `toml.Decoder.Decode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `mapstructure` tag as it is passed to `mapstructure.Decode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `mapstructure` tag as it is passed to `mapstructure.DecodeMetadata` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `mapstructure` tag as it is passed to `mapstructure.WeakDecode` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `mapstructure` tag as it is passed to `mapstructure.WeakDecodeMetadata` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `custom` tag as it is passed to `custom.Marshal` at testdata/src/builtins/builtins.go" + "`User` should be annotated with the `custom` tag as it is passed to `custom.Unmarshal` at testdata/src/builtins/builtins.go" */ Name string Email string `json:"email" xml:"email" yaml:"email" toml:"email" mapstructure:"email" custom:"email"`