Skip to content

Commit

Permalink
fix index out of range
Browse files Browse the repository at this point in the history
  • Loading branch information
4meepo committed Oct 19, 2023
1 parent 8a15bce commit cc14d3c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions tagalign.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
if err != nil {
// if tag value is not a valid string, report it directly
w.report(pass, field, column, errTagValueSyntax, field.Tag.Value)
fields = append(fields[:i], fields[i+1:]...)
fields = removeField(fields, i)
continue
}

tags, err := structtag.Parse(tag)
if err != nil {
// if tag value is not a valid struct tag, report it directly
w.report(pass, field, column, err.Error(), field.Tag.Value)
fields = append(fields[:i], fields[i+1:]...)
fields = removeField(fields, i)
continue
}

Expand Down Expand Up @@ -449,3 +449,11 @@ func max(a, b int) int {
}
return b
}

func removeField(fields []*ast.Field, index int) []*ast.Field {
if index < 0 || index >= len(fields) {
return fields
}

return append(fields[:index], fields[index+1:]...)
}
8 changes: 4 additions & 4 deletions testdata/bad_syntax_tag/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type FooBar struct {
type FooBar2 struct {
Foo int `json:"foo" validate:"required"`

Bar string `json:bar` // want `bad syntax for struct tag value`

FooFoo int8 `json:"foo_foo"`
BarBar int `json:"bar_bar" validate:"required"`
FooFoo int8 `json:"foo_foo"`
BarBar int `json:"bar_bar" validate:"required"`
XXX int `json:"xxx" validate:"required"`
Bar string `json:bar` // want `bad syntax for struct tag value`
}

0 comments on commit cc14d3c

Please sign in to comment.