From b68f1870887aad8ed0fa2747fdd46cd6112f20d2 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Feb 2021 14:30:58 +0530 Subject: [PATCH 1/7] Fix unnecessary calls to Printf --- .deepsource.toml | 12 ++++++++++++ metrics/generic/generic.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 000000000..f6c0dd8fa --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,12 @@ +version = 1 + +test_patterns = ["**/*_test.go"] + +exclude_patterns = ["examples/**"] + +[[analyzers]] +name = "go" +enabled = true + + [analyzers.meta] + import_paths = ["github.com/go-kit/kit"] diff --git a/metrics/generic/generic.go b/metrics/generic/generic.go index b69334507..39216c04f 100644 --- a/metrics/generic/generic.go +++ b/metrics/generic/generic.go @@ -182,7 +182,7 @@ func (h *Histogram) LabelValues() []string { func (h *Histogram) Print(w io.Writer) { h.h.RLock() defer h.h.RUnlock() - fmt.Fprintf(w, h.h.String()) + fmt.Fprint(w, h.h.String()) } // safeHistogram exists as gohistogram.Histogram is not goroutine-safe. From 14055b66fc59d60a4aa543e834d72f2de5e1f56a Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Feb 2021 14:30:58 +0530 Subject: [PATCH 2/7] Fix nil context being passed to function --- transport/http/proto/proto_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/transport/http/proto/proto_test.go b/transport/http/proto/proto_test.go index 7b59d71a2..070bbec02 100644 --- a/transport/http/proto/proto_test.go +++ b/transport/http/proto/proto_test.go @@ -1,6 +1,7 @@ package proto import ( + "context" "io/ioutil" "net/http" "net/http/httptest" @@ -14,7 +15,7 @@ func TestEncodeProtoRequest(t *testing.T) { r := httptest.NewRequest(http.MethodGet, "/cat", nil) - err := EncodeProtoRequest(nil, r, cat) + err := EncodeProtoRequest(context.TODO(), r, cat) if err != nil { t.Errorf("expected no encoding errors but got: %s", err) return @@ -51,7 +52,7 @@ func TestEncodeProtoResponse(t *testing.T) { wr := httptest.NewRecorder() - err := EncodeProtoResponse(nil, wr, cat) + err := EncodeProtoResponse(context.TODO(), wr, cat) if err != nil { t.Errorf("expected no encoding errors but got: %s", err) return From 4b4d77be6e8a55e48435d01e85a1ecc7a1218d9b Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Feb 2021 14:30:59 +0530 Subject: [PATCH 3/7] Remove empty default in select --- sd/etcd/client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sd/etcd/client_test.go b/sd/etcd/client_test.go index 1bdb7aee9..e53565e9c 100644 --- a/sd/etcd/client_test.go +++ b/sd/etcd/client_test.go @@ -1,11 +1,11 @@ package etcd import ( + "context" "errors" "reflect" "testing" "time" - "context" etcd "go.etcd.io/etcd/client" ) @@ -135,7 +135,7 @@ func (fw *fakeWatcher) Next(context.Context) (*etcd.Response, error) { return nil, nil case <-fw.err: return nil, errors.New("error from underlying etcd watcher") - default: + } } } From 7494f7708990f1058564690ef309a91020b11254 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Feb 2021 14:31:00 +0530 Subject: [PATCH 4/7] Replace `strings.Index` with `strings.Contains` --- cmd/kitgen/path_test.go | 2 +- cmd/kitgen/transform.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/kitgen/path_test.go b/cmd/kitgen/path_test.go index 818d89cd7..10a71cfe0 100644 --- a/cmd/kitgen/path_test.go +++ b/cmd/kitgen/path_test.go @@ -40,7 +40,7 @@ func TestImportPathSadpath(t *testing.T) { if actual != "" { t.Errorf("Expected empty path, got %q", actual) } - if strings.Index(err.Error(), expected) == -1 { + if !strings.Contains(err.Error(), expected) { t.Errorf("Expected %q to include %q", err, expected) } }) diff --git a/cmd/kitgen/transform.go b/cmd/kitgen/transform.go index a66ff96da..112fe38b3 100644 --- a/cmd/kitgen/transform.go +++ b/cmd/kitgen/transform.go @@ -59,7 +59,7 @@ func importPath(targetDir, gopath string) (string, error) { if err != nil { continue } - if strings.Index(res, "..") == -1 { + if !strings.Contains(res, "..") { return res, nil } } From 963503ef0ea23f8884553a0ffb7f8babbc02b7c5 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Feb 2021 14:31:01 +0530 Subject: [PATCH 5/7] Replace `bytes.Compare` with `bytes.Equal` --- sd/zk/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sd/zk/client_test.go b/sd/zk/client_test.go index e201536d8..f335ab4d3 100644 --- a/sd/zk/client_test.go +++ b/sd/zk/client_test.go @@ -63,7 +63,7 @@ func TestNewClient(t *testing.T) { if want, have := sessionTimeout, clientImpl.sessionTimeout; want != have { t.Errorf("want %d, have %d", want, have) } - if want, have := payload, clientImpl.rootNodePayload; bytes.Compare(want[0], have[0]) != 0 || bytes.Compare(want[1], have[1]) != 0 { + if want, have := payload, clientImpl.rootNodePayload; !bytes.Equal(want[0], have[0]) || !bytes.Equal(want[1], have[1]) { t.Errorf("want %s, have %s", want, have) } From ddd974911356e83a951b7c09c66fbc86442a1845 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Feb 2021 14:31:02 +0530 Subject: [PATCH 6/7] Merge variable declaration with assignment --- log/sync_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/log/sync_test.go b/log/sync_test.go index 253f256d1..fd7cdfd0f 100644 --- a/log/sync_test.go +++ b/log/sync_test.go @@ -58,8 +58,7 @@ func TestSwapLoggerConcurrency(t *testing.T) { } func TestSyncLoggerConcurrency(t *testing.T) { - var w io.Writer - w = &bytes.Buffer{} + var w io.Writer = &bytes.Buffer{} logger := log.NewLogfmtLogger(w) logger = log.NewSyncLogger(logger) testConcurrency(t, logger, 10000) From ee40b538bf5ebb146ddfc951b4f07c9eb77b2cf8 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Feb 2021 14:31:02 +0530 Subject: [PATCH 7/7] Remove unnecessary use of `fmt.Sprintf` --- .deepsource.toml | 12 ------------ auth/jwt/transport_test.go | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) delete mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml deleted file mode 100644 index f6c0dd8fa..000000000 --- a/.deepsource.toml +++ /dev/null @@ -1,12 +0,0 @@ -version = 1 - -test_patterns = ["**/*_test.go"] - -exclude_patterns = ["examples/**"] - -[[analyzers]] -name = "go" -enabled = true - - [analyzers.meta] - import_paths = ["github.com/go-kit/kit"] diff --git a/auth/jwt/transport_test.go b/auth/jwt/transport_test.go index 83d0f17f8..f20055be2 100644 --- a/auth/jwt/transport_test.go +++ b/auth/jwt/transport_test.go @@ -76,7 +76,7 @@ func TestGRPCToContext(t *testing.T) { } // Invalid Authorization header is passed - md["authorization"] = []string{fmt.Sprintf("%s", signedKey)} + md["authorization"] = []string{signedKey} ctx = reqFunc(context.Background(), md) token = ctx.Value(JWTTokenContextKey) if token != nil {