From 0f7735b18a59dc16eef522478244212f840ee34d Mon Sep 17 00:00:00 2001 From: Vladislav Fursov Date: Thu, 18 Jan 2024 18:12:48 +0400 Subject: [PATCH] by default we skip checking and fixing the first argument in the append function. --- processor.go | 12 +++++++++++- protogetter.go | 15 ++++++++------- testdata/test.go | 7 +++++++ testdata/test.go.golden | 7 +++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/processor.go b/processor.go index e42b38d..d65199d 100644 --- a/processor.go +++ b/processor.go @@ -12,16 +12,18 @@ import ( type processor struct { info *types.Info filter *PosFilter + cfg *Config to strings.Builder from strings.Builder err error } -func Process(info *types.Info, filter *PosFilter, n ast.Node) (*Result, error) { +func Process(info *types.Info, filter *PosFilter, n ast.Node, cfg *Config) (*Result, error) { p := &processor{ info: info, filter: filter, + cfg: cfg, } return p.process(n) @@ -51,6 +53,14 @@ func (c *processor) process(n ast.Node) (*Result, error) { } case *ast.CallExpr: + if !c.cfg.ReplaceFirstArgInAppend && len(x.Args) > 0 { + if v, ok := x.Fun.(*ast.Ident); ok && v.Name == "append" { + // Skip first argument of append function. + c.filter.AddPos(x.Args[0].Pos()) + break + } + } + f, ok := x.Fun.(*ast.SelectorExpr) if !ok { return &Result{}, nil diff --git a/protogetter.go b/protogetter.go index bb84c6b..31eee85 100644 --- a/protogetter.go +++ b/protogetter.go @@ -62,10 +62,11 @@ func flags(opts *Config) flag.FlagSet { } type Config struct { - Mode Mode // Zero value is StandaloneMode. - SkipGeneratedBy []string - SkipFiles []string - SkipAnyGenerated bool + Mode Mode // Zero value is StandaloneMode. + SkipGeneratedBy []string + SkipFiles []string + SkipAnyGenerated bool + ReplaceFirstArgInAppend bool } func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) { @@ -127,7 +128,7 @@ func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) { filter := NewPosFilter() ins.Preorder(nodeTypes, func(node ast.Node) { - report := analyse(pass, filter, node) + report := analyse(pass, filter, node, cfg) if report == nil { return } @@ -143,7 +144,7 @@ func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) { return issues, nil } -func analyse(pass *analysis.Pass, filter *PosFilter, n ast.Node) *Report { +func analyse(pass *analysis.Pass, filter *PosFilter, n ast.Node, cfg *Config) *Report { // fmt.Printf("\n>>> check: %s\n", formatNode(n)) // ast.Print(pass.Fset, n) if filter.IsFiltered(n.Pos()) { @@ -151,7 +152,7 @@ func analyse(pass *analysis.Pass, filter *PosFilter, n ast.Node) *Report { return nil } - result, err := Process(pass.TypesInfo, filter, n) + result, err := Process(pass.TypesInfo, filter, n, cfg) if err != nil { pass.Report(analysis.Diagnostic{ Pos: n.Pos(), diff --git a/testdata/test.go b/testdata/test.go index 4df5698..f9a78be 100644 --- a/testdata/test.go +++ b/testdata/test.go @@ -90,6 +90,9 @@ func testInvalid(t *proto.Test) { } _ = *t.OptEnum // want `avoid direct access to proto field \*t\.OptEnum, use t\.GetOptEnum\(\) instead` + + t.RepeatedEmbeddeds = append(t.GetRepeatedEmbeddeds(), t.RepeatedEmbeddeds...) // want `avoid direct access to proto field t\.RepeatedEmbeddeds, use t\.GetRepeatedEmbeddeds\(\) instead` + t.RepeatedEmbeddeds = append(t.RepeatedEmbeddeds, t.Embedded) // want `avoid direct access to proto field t\.Embedded, use t\.GetEmbedded\(\) instead` } func testValid(t *proto.Test) { @@ -166,4 +169,8 @@ func testValid(t *proto.Test) { OptBool: new(bool), } *v.OptBool = true + + t.RepeatedEmbeddeds = append(t.GetRepeatedEmbeddeds(), t.GetRepeatedEmbeddeds()...) + t.RepeatedEmbeddeds = append(t.RepeatedEmbeddeds, t.GetEmbedded()) + t.RepeatedEmbeddeds = append(t.RepeatedEmbeddeds, &proto.Embedded{}) } diff --git a/testdata/test.go.golden b/testdata/test.go.golden index d98bcc3..4c13339 100644 --- a/testdata/test.go.golden +++ b/testdata/test.go.golden @@ -90,6 +90,9 @@ func testInvalid(t *proto.Test) { } _ = t.GetOptEnum() // want `avoid direct access to proto field \*t\.OptEnum, use t\.GetOptEnum\(\) instead` + + t.RepeatedEmbeddeds = append(t.GetRepeatedEmbeddeds(), t.GetRepeatedEmbeddeds()...) // want `avoid direct access to proto field t\.RepeatedEmbeddeds, use t\.GetRepeatedEmbeddeds\(\) instead` + t.RepeatedEmbeddeds = append(t.RepeatedEmbeddeds, t.GetEmbedded()) // want `avoid direct access to proto field t\.Embedded, use t\.GetEmbedded\(\) instead` } func testValid(t *proto.Test) { @@ -166,4 +169,8 @@ func testValid(t *proto.Test) { OptBool: new(bool), } *v.OptBool = true + + t.RepeatedEmbeddeds = append(t.GetRepeatedEmbeddeds(), t.GetRepeatedEmbeddeds()...) + t.RepeatedEmbeddeds = append(t.RepeatedEmbeddeds, t.GetEmbedded()) + t.RepeatedEmbeddeds = append(t.RepeatedEmbeddeds, &proto.Embedded{}) }