From ec700cffb83f65db374f326d75cfba4100435c7a Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 15 Jan 2025 10:41:33 +0000 Subject: [PATCH] refactor: replace empty slice literal with `var` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An empty slice can be represented by `nil` or an empty slice literal. They are functionally equivalent — their `len` and `cap` are both zero — but the `nil` slice is the preferred style. For more information about empty slices, see [Declaring Empty Slices](https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices). --- plugin/plugin.go | 5 +++-- proxy/formatter_benchmark_test.go | 9 ++++++--- proxy/merging_benchmark_test.go | 3 ++- proxy/plugin/modifier.go | 3 ++- proxy/request.go | 3 ++- router/gin/engine.go | 2 +- transport/http/client/plugin/plugin.go | 3 ++- transport/http/server/plugin/plugin.go | 3 ++- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index 72cc3fef7..a6524c8ce 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 /* - Package plugin provides tools for loading and registering plugins +Package plugin provides tools for loading and registering plugins */ package plugin @@ -18,7 +18,8 @@ func Scan(folder, pattern string) ([]string, error) { return []string{}, err } - plugins := []string{} + var plugins []string + for _, file := range files { if !file.IsDir() && strings.Contains(file.Name(), pattern) { plugins = append(plugins, filepath.Join(folder, file.Name())) diff --git a/proxy/formatter_benchmark_test.go b/proxy/formatter_benchmark_test.go index affa75565..cc6e97406 100644 --- a/proxy/formatter_benchmark_test.go +++ b/proxy/formatter_benchmark_test.go @@ -252,12 +252,14 @@ func BenchmarkEntityFormatter_flatmapAlt(b *testing.B) { }, IsComplete: true, } - subCol := []interface{}{} + var subCol []interface{} + for i := 0; i < size; i++ { subCol = append(subCol, i) } sub["e"] = subCol - sampleSubCol := []interface{}{} + var sampleSubCol []interface{} + for i := 0; i < size; i++ { sampleSubCol = append(sampleSubCol, sub) } @@ -285,7 +287,8 @@ func BenchmarkEntityFormatter_flatmap(b *testing.B) { IsComplete: true, } - cmds := []interface{}{} + var cmds []interface{} + for _, path := range blacklist { cmds = append(cmds, map[string]interface{}{ "type": "del", diff --git a/proxy/merging_benchmark_test.go b/proxy/merging_benchmark_test.go index 4963d1bca..6e769538d 100644 --- a/proxy/merging_benchmark_test.go +++ b/proxy/merging_benchmark_test.go @@ -51,7 +51,8 @@ func BenchmarkNewMergeDataMiddleware(b *testing.B) { func BenchmarkNewMergeDataMiddleware_sequential(b *testing.B) { backends := make([]*config.Backend, 10) pattern := "/some" - keys := []string{} + var keys []string + for i := range backends { b := &config.Backend{ URLKeys: make([]string, 4*i), diff --git a/proxy/plugin/modifier.go b/proxy/plugin/modifier.go index 32881e8b3..0875e01cc 100644 --- a/proxy/plugin/modifier.go +++ b/proxy/plugin/modifier.go @@ -116,7 +116,8 @@ func LoadWithLoggerAndContext(ctx context.Context, path, pattern string, rmf Reg } func load(ctx context.Context, plugins []string, rmf RegisterModifierFunc, logger logging.Logger) (int, error) { - errors := []error{} + var errors []error + loadedPlugins := 0 for k, pluginName := range plugins { if err := open(ctx, pluginName, rmf, logger); err != nil { diff --git a/proxy/request.go b/proxy/request.go index eb6cc414a..50763e898 100644 --- a/proxy/request.go +++ b/proxy/request.go @@ -27,7 +27,8 @@ func (r *Request) GeneratePath(URLPattern string) { } buff := []byte(URLPattern) for k, v := range r.Params { - key := []byte{} + var key []byte + key = append(key, "{{."...) key = append(key, k...) key = append(key, "}}"...) diff --git a/router/gin/engine.go b/router/gin/engine.go index b75c491a3..d2d5bc22d 100644 --- a/router/gin/engine.go +++ b/router/gin/engine.go @@ -43,7 +43,7 @@ func NewEngine(cfg config.ServiceConfig, opt EngineOptions) *gin.Engine { engine.HandleMethodNotAllowed = true engine.ContextWithFallback = true - paths := []string{} + var paths []string ginOptions := engineConfiguration{} if v, ok := cfg.ExtraConfig[Namespace]; ok { diff --git a/transport/http/client/plugin/plugin.go b/transport/http/client/plugin/plugin.go index 6bccc9310..aafba79ec 100644 --- a/transport/http/client/plugin/plugin.go +++ b/transport/http/client/plugin/plugin.go @@ -52,7 +52,8 @@ func LoadWithLogger(path, pattern string, rcf RegisterClientFunc, logger logging } func load(plugins []string, rcf RegisterClientFunc, logger logging.Logger) (int, error) { - errors := []error{} + var errors []error + loadedPlugins := 0 for k, pluginName := range plugins { if err := open(pluginName, rcf, logger); err != nil { diff --git a/transport/http/server/plugin/plugin.go b/transport/http/server/plugin/plugin.go index 9ca5cafea..bcdf43aa1 100644 --- a/transport/http/server/plugin/plugin.go +++ b/transport/http/server/plugin/plugin.go @@ -52,7 +52,8 @@ func LoadWithLogger(path, pattern string, rcf RegisterHandlerFunc, logger loggin } func load(plugins []string, rcf RegisterHandlerFunc, logger logging.Logger) (int, error) { - errors := []error{} + var errors []error + loadedPlugins := 0 for k, pluginName := range plugins { if err := open(pluginName, rcf, logger); err != nil {