From b0c71bab1c80f6f3a762b947057858407766fa28 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Tue, 29 Aug 2023 03:51:18 +0300 Subject: [PATCH] fix: linter memory aliasing issue --- gen/generator.go | 2 +- gen/schema_gen_sum.go | 2 +- internal/xslices/xslices.go | 10 ++++------ internal/xslices/xslices_test.go | 5 +---- jsonschema/parser_enum.go | 2 +- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/gen/generator.go b/gen/generator.go index 44bfb832e..1a333ad53 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -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), diff --git a/gen/schema_gen_sum.go b/gen/schema_gen_sum.go index 94272726e..440ef50b6 100644 --- a/gen/schema_gen_sum.go +++ b/gen/schema_gen_sum.go @@ -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 }) diff --git a/internal/xslices/xslices.go b/internal/xslices/xslices.go index d0aecc2c4..fcc5e03cf 100644 --- a/internal/xslices/xslices.go +++ b/internal/xslices/xslices.go @@ -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. diff --git a/internal/xslices/xslices_test.go b/internal/xslices/xslices_test.go index 281d678f3..d168529c8 100644 --- a/internal/xslices/xslices_test.go +++ b/internal/xslices/xslices_test.go @@ -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)) diff --git a/jsonschema/parser_enum.go b/jsonschema/parser_enum.go index bfa9a8486..220c91dd8 100644 --- a/jsonschema/parser_enum.go +++ b/jsonschema/parser_enum.go @@ -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 }) }