Skip to content

Commit

Permalink
by default we skip checking and fixing the first argument in the appe…
Browse files Browse the repository at this point in the history
…nd function.
  • Loading branch information
ghostiam committed Jan 18, 2024
1 parent 280e547 commit 0f7735b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
12 changes: 11 additions & 1 deletion processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions protogetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -143,15 +144,15 @@ 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()) {
// fmt.Printf(">>> filtered\n")
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(),
Expand Down
7 changes: 7 additions & 0 deletions testdata/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{})
}
7 changes: 7 additions & 0 deletions testdata/test.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{})
}

0 comments on commit 0f7735b

Please sign in to comment.