Skip to content

Commit

Permalink
Merge pull request #1016 from tdakkota/fix/fix-g601
Browse files Browse the repository at this point in the history
fix: linter memory aliasing issue
  • Loading branch information
ernado authored Aug 29, 2023
2 parents 2e4b8dc + b0c71ba commit 3e71bb7
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (g *Generator) makeWebhooks(webhooks []openapi.Webhook) error {
continue
}

xslices.Filter(&spec.Parameters, func(p *openapi.Parameter) bool {
spec.Parameters = xslices.Filter(spec.Parameters, func(p *openapi.Parameter) bool {
if p.In.Path() {
log.Warn("Webhooks can't have path parameters",
zap.String("name", p.Name),
Expand Down
2 changes: 1 addition & 1 deletion gen/schema_gen_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (g *schemaGen) oneOf(name string, schema *jsonschema.Schema, side bool) (*i
})

// Filter discriminator field in-place.
xslices.Filter(&s.Fields, func(f *ir.Field) bool {
s.Fields = xslices.Filter(s.Fields, func(f *ir.Field) bool {
return f.Tag.JSON != propName
})

Expand Down
10 changes: 4 additions & 6 deletions internal/xslices/xslices.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ package xslices
import "golang.org/x/exp/slices"

// Filter performs in-place filtering of a slice.
func Filter[S ~[]E, E any](sptr *S, keep func(E) bool) {
var (
n int
s = *sptr
)
func Filter[S ~[]E, E any](s S, keep func(E) bool) S {
var n int
for _, v := range s {
if keep(v) {
s[n] = v
n++
}
}
*sptr = s[:n]
s = s[:n]
return s
}

// FindFunc returns the first element satisfying the predicate.
Expand Down
5 changes: 1 addition & 4 deletions internal/xslices/xslices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ func TestFilter(t *testing.T) {
odd := func(x int) bool {
return x%2 == 1
}
filter := func(v []int, cb func(int) bool) []int {
Filter(&v, cb)
return v
}
filter := Filter[[]int]

a.Empty(filter([]int(nil), odd))
a.Empty(filter([]int{}, odd))
Expand Down
2 changes: 1 addition & 1 deletion jsonschema/parser_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func handleNullableEnum(s *Schema) {
})
// Filter all `null`s.
if s.Nullable {
xslices.Filter(&s.Enum, func(v any) bool {
s.Enum = xslices.Filter(s.Enum, func(v any) bool {
return v != nil
})
}
Expand Down

0 comments on commit 3e71bb7

Please sign in to comment.