Skip to content

Commit cc14d3c

Browse files
committed
fix index out of range
1 parent 8a15bce commit cc14d3c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

tagalign.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
221221
if err != nil {
222222
// if tag value is not a valid string, report it directly
223223
w.report(pass, field, column, errTagValueSyntax, field.Tag.Value)
224-
fields = append(fields[:i], fields[i+1:]...)
224+
fields = removeField(fields, i)
225225
continue
226226
}
227227

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

@@ -449,3 +449,11 @@ func max(a, b int) int {
449449
}
450450
return b
451451
}
452+
453+
func removeField(fields []*ast.Field, index int) []*ast.Field {
454+
if index < 0 || index >= len(fields) {
455+
return fields
456+
}
457+
458+
return append(fields[:index], fields[index+1:]...)
459+
}

testdata/bad_syntax_tag/example.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type FooBar struct {
1010
type FooBar2 struct {
1111
Foo int `json:"foo" validate:"required"`
1212

13-
Bar string `json:bar` // want `bad syntax for struct tag value`
14-
15-
FooFoo int8 `json:"foo_foo"`
16-
BarBar int `json:"bar_bar" validate:"required"`
13+
FooFoo int8 `json:"foo_foo"`
14+
BarBar int `json:"bar_bar" validate:"required"`
15+
XXX int `json:"xxx" validate:"required"`
16+
Bar string `json:bar` // want `bad syntax for struct tag value`
1717
}

0 commit comments

Comments
 (0)