From d9a82cd3cd8be01257569366966de452513db4be Mon Sep 17 00:00:00 2001 From: marcello33 Date: Thu, 15 Jun 2023 13:18:25 +0200 Subject: [PATCH] dev: fix: errorcheck lint issues --- common/lru/basiclru_test.go | 7 +++++-- common/types_test.go | 2 +- crypto/signify/signify.go | 1 + ethdb/dbtest/testsuite.go | 16 ++++++++-------- ethdb/pebble/pebble.go | 10 +++++----- log/format_test.go | 2 +- metrics/counter_float64.go | 5 +++-- metrics/counter_float_64_test.go | 4 ++++ metrics/syslog.go | 16 ++++++++-------- p2p/nat/natpmp.go | 2 +- p2p/netutil/iptrack_test.go | 4 ++-- rpc/client.go | 2 +- rpc/handler.go | 10 +++++----- rpc/server.go | 2 +- rpc/types.go | 1 + 15 files changed, 47 insertions(+), 37 deletions(-) diff --git a/common/lru/basiclru_test.go b/common/lru/basiclru_test.go index 0b29a1f15d..a092123ba4 100644 --- a/common/lru/basiclru_test.go +++ b/common/lru/basiclru_test.go @@ -27,7 +27,10 @@ import ( // Some of these test cases were adapted // from https://github.com/hashicorp/golang-lru/blob/master/simplelru/lru_test.go +// nolint:gocognit func TestBasicLRU(t *testing.T) { + t.Parallel() + cache := NewBasicLRU[int, int](128) for i := 0; i < 256; i++ { @@ -210,9 +213,9 @@ func BenchmarkLRU(b *testing.B) { for i := range keys { b := make([]byte, 32) - crand.Read(b) + _, _ = crand.Read(b) keys[i] = string(b) - crand.Read(b) + _, _ = crand.Read(b) values[i] = b } diff --git a/common/types_test.go b/common/types_test.go index 4e6d4d1a70..302d9d6d3f 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -179,7 +179,7 @@ func TestMixedcaseAddressMarshal(t *testing.T) { if err != nil { t.Fatal(err) } - json.Unmarshal(blob, &output) + _ = json.Unmarshal(blob, &output) if output != input { t.Fatal("Failed to marshal/unmarshal MixedcaseAddress object") diff --git a/crypto/signify/signify.go b/crypto/signify/signify.go index eb029e5099..ebe33b1ddd 100644 --- a/crypto/signify/signify.go +++ b/crypto/signify/signify.go @@ -96,5 +96,6 @@ func SignFile(input string, output string, key string, untrustedComment string, fmt.Fprintln(out, base64.StdEncoding.EncodeToString(dataSig)) fmt.Fprintln(out, "trusted comment:", trustedComment) fmt.Fprintln(out, base64.StdEncoding.EncodeToString(commentSig)) + // nolint:gosec return os.WriteFile(output, out.Bytes(), 0644) } diff --git a/ethdb/dbtest/testsuite.go b/ethdb/dbtest/testsuite.go index 98305a528f..911c53c562 100644 --- a/ethdb/dbtest/testsuite.go +++ b/ethdb/dbtest/testsuite.go @@ -399,7 +399,7 @@ func BenchDatabaseSuite(b *testing.B, New func() ethdb.KeyValueStore) { defer db.Close() for i := 0; i < len(keys); i++ { - db.Put(keys[i], vals[i]) + _ = db.Put(keys[i], vals[i]) } } b.Run("WriteSorted", func(b *testing.B) { @@ -417,13 +417,13 @@ func BenchDatabaseSuite(b *testing.B, New func() ethdb.KeyValueStore) { defer db.Close() for i := 0; i < len(keys); i++ { - db.Put(keys[i], vals[i]) + _ = db.Put(keys[i], vals[i]) } b.ResetTimer() b.ReportAllocs() for i := 0; i < len(keys); i++ { - db.Get(keys[i]) + _, _ = db.Get(keys[i]) } } b.Run("ReadSorted", func(b *testing.B) { @@ -441,7 +441,7 @@ func BenchDatabaseSuite(b *testing.B, New func() ethdb.KeyValueStore) { defer db.Close() for i := 0; i < len(keys); i++ { - db.Put(keys[i], vals[i]) + _ = db.Put(keys[i], vals[i]) } b.ResetTimer() b.ReportAllocs() @@ -470,9 +470,9 @@ func BenchDatabaseSuite(b *testing.B, New func() ethdb.KeyValueStore) { batch := db.NewBatch() for i := 0; i < len(keys); i++ { - batch.Put(keys[i], vals[i]) + _ = batch.Put(keys[i], vals[i]) } - batch.Write() + _ = batch.Write() } b.Run("BenchWriteSorted", func(b *testing.B) { benchBatchWrite(b, sKeys, sVals) @@ -504,8 +504,8 @@ func randBytes(length int) []byte { } func makeDataset(size, ksize, vsize int, order bool) ([][]byte, [][]byte) { - var keys [][]byte - var vals [][]byte + keys := make([][]byte, 0, ksize) + vals := make([][]byte, 0, vsize) for i := 0; i < size; i += 1 { keys = append(keys, randBytes(ksize)) diff --git a/ethdb/pebble/pebble.go b/ethdb/pebble/pebble.go index 5bd310a0a3..0e53506672 100644 --- a/ethdb/pebble/pebble.go +++ b/ethdb/pebble/pebble.go @@ -517,15 +517,15 @@ type batch struct { // Put inserts the given value into the batch for later committing. func (b *batch) Put(key, value []byte) error { - b.b.Set(key, value, nil) + _ = b.b.Set(key, value, nil) b.size += len(key) + len(value) return nil } -// Delete inserts the a key removal into the batch for later committing. +// Delete inserts the key removal into the batch for later committing. func (b *batch) Delete(key []byte) error { - b.b.Delete(key, nil) + _ = b.b.Delete(key, nil) b.size += len(key) return nil @@ -559,9 +559,9 @@ func (b *batch) Replay(w ethdb.KeyValueWriter) error { // The (k,v) slices might be overwritten if the batch is reset/reused, // and the receiver should copy them if they are to be retained long-term. if kind == pebble.InternalKeyKindSet { - w.Put(k, v) + _ = w.Put(k, v) } else if kind == pebble.InternalKeyKindDelete { - w.Delete(k) + _ = w.Delete(k) } else { return fmt.Errorf("unhandled operation, keytype: %v", kind) } diff --git a/log/format_test.go b/log/format_test.go index e3398cf965..c161ff7dbd 100644 --- a/log/format_test.go +++ b/log/format_test.go @@ -95,7 +95,7 @@ func TestPrettyUint256(t *testing.T) { for _, tt := range tests { v := new(uint256.Int) - v.SetFromDecimal(tt.int) + _ = v.SetFromDecimal(tt.int) if have := formatLogfmtUint256(v); have != tt.s { t.Errorf("invalid output %s, want %s", have, tt.s) diff --git a/metrics/counter_float64.go b/metrics/counter_float64.go index a3634f490e..370ac1c225 100644 --- a/metrics/counter_float64.go +++ b/metrics/counter_float64.go @@ -58,7 +58,7 @@ func NewRegisteredCounterFloat64(name string, r Registry) CounterFloat64 { if nil == r { r = DefaultRegistry } - r.Register(name, c) + _ = r.Register(name, c) return c } @@ -73,7 +73,7 @@ func NewRegisteredCounterFloat64Forced(name string, r Registry) CounterFloat64 { if nil == r { r = DefaultRegistry } - r.Register(name, c) + _ = r.Register(name, c) return c } @@ -109,6 +109,7 @@ type NilCounterFloat64 struct{} func (NilCounterFloat64) Clear() {} // Count is a no-op. +// nolint:goconst func (NilCounterFloat64) Count() float64 { return 0.0 } // Dec is a no-op. diff --git a/metrics/counter_float_64_test.go b/metrics/counter_float_64_test.go index 3557f47128..6c448a0e37 100644 --- a/metrics/counter_float_64_test.go +++ b/metrics/counter_float_64_test.go @@ -73,11 +73,13 @@ func TestCounterFloat64Dec2(t *testing.T) { } } +// nolint:goconst func TestCounterFloat64Inc1(t *testing.T) { t.Parallel() c := NewCounterFloat64() c.Inc(1.0) + if count := c.Count(); count != 1.0 { t.Errorf("c.Count(): 1.0 != %v\n", count) } @@ -117,11 +119,13 @@ func TestCounterFloat64Zero(t *testing.T) { } } +// nolint:goconst func TestGetOrRegisterCounterFloat64(t *testing.T) { t.Parallel() r := NewRegistry() NewRegisteredCounterFloat64("foo", r).Inc(47.0) + if c := GetOrRegisterCounterFloat64("foo", r); c.Count() != 47.0 { t.Fatal(c) } diff --git a/metrics/syslog.go b/metrics/syslog.go index f23b07e199..5d251da769 100644 --- a/metrics/syslog.go +++ b/metrics/syslog.go @@ -16,20 +16,20 @@ func Syslog(r Registry, d time.Duration, w *syslog.Writer) { r.Each(func(name string, i interface{}) { switch metric := i.(type) { case Counter: - w.Info(fmt.Sprintf("counter %s: count: %d", name, metric.Count())) + _ = w.Info(fmt.Sprintf("counter %s: count: %d", name, metric.Count())) case CounterFloat64: - w.Info(fmt.Sprintf("counter %s: count: %f", name, metric.Count())) + _ = w.Info(fmt.Sprintf("counter %s: count: %f", name, metric.Count())) case Gauge: - w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Value())) + _ = w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Value())) case GaugeFloat64: - w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Value())) + _ = w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Value())) case Healthcheck: metric.Check() - w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error())) + _ = w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error())) case Histogram: h := metric.Snapshot() ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) - w.Info(fmt.Sprintf( + _ = w.Info(fmt.Sprintf( "histogram %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f", name, h.Count(), @@ -45,7 +45,7 @@ func Syslog(r Registry, d time.Duration, w *syslog.Writer) { )) case Meter: m := metric.Snapshot() - w.Info(fmt.Sprintf( + _ = w.Info(fmt.Sprintf( "meter %s: count: %d 1-min: %.2f 5-min: %.2f 15-min: %.2f mean: %.2f", name, m.Count(), @@ -57,7 +57,7 @@ func Syslog(r Registry, d time.Duration, w *syslog.Writer) { case Timer: t := metric.Snapshot() ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) - w.Info(fmt.Sprintf( + _ = w.Info(fmt.Sprintf( "timer %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f 1-min: %.2f 5-min: %.2f 15-min: %.2f mean-rate: %.2f", name, t.Count(), diff --git a/p2p/nat/natpmp.go b/p2p/nat/natpmp.go index 40f2aff44e..91de9fec58 100644 --- a/p2p/nat/natpmp.go +++ b/p2p/nat/natpmp.go @@ -61,7 +61,7 @@ func (n *pmp) AddMapping(protocol string, extport, intport int, name string, lif // to the caller. if uint16(extport) != res.MappedExternalPort { // Destroy the mapping in NAT device. - n.c.AddPortMapping(strings.ToLower(protocol), intport, 0, 0) + _, _ = n.c.AddPortMapping(strings.ToLower(protocol), intport, 0, 0) return fmt.Errorf("port %d already mapped to another address (%s)", extport, protocol) } diff --git a/p2p/netutil/iptrack_test.go b/p2p/netutil/iptrack_test.go index ee3bba861e..f975e6651c 100644 --- a/p2p/netutil/iptrack_test.go +++ b/p2p/netutil/iptrack_test.go @@ -123,8 +123,8 @@ func TestIPTrackerForceGC(t *testing.T) { for i := 0; i < 5*max; i++ { e1 := make([]byte, 4) e2 := make([]byte, 4) - crand.Read(e1) - crand.Read(e2) + _, _ = crand.Read(e1) + _, _ = crand.Read(e2) it.AddStatement(string(e1), string(e2)) it.AddContact(string(e1)) clock.Run(rate) diff --git a/rpc/client.go b/rpc/client.go index 573cbf2940..f828d3574b 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -667,7 +667,7 @@ func (c *Client) read(codec ServerCodec) { msgs, batch, err := codec.readBatch() if _, ok := err.(*json.SyntaxError); ok { msg := errorMessage(&parseError{err.Error()}) - codec.writeJSON(context.Background(), msg, true) + _ = codec.writeJSON(context.Background(), msg, true) } if err != nil { c.readErr <- err diff --git a/rpc/handler.go b/rpc/handler.go index 08fbf9a83d..3b40537421 100644 --- a/rpc/handler.go +++ b/rpc/handler.go @@ -167,7 +167,7 @@ func (b *batchCallBuffer) doWrite(ctx context.Context, conn jsonWriter, isErrorR b.wrote = true // can only write once if len(b.resp) > 0 { - conn.writeJSON(ctx, b.resp, isErrorResponse) + _ = conn.writeJSON(ctx, b.resp, isErrorResponse) } } @@ -177,7 +177,7 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) { if len(msgs) == 0 { h.startCallProc(func(cp *callProc) { resp := errorMessage(&invalidRequestError{"empty batch"}) - h.conn.writeJSON(cp.ctx, resp, true) + _ = h.conn.writeJSON(cp.ctx, resp, true) }) return } @@ -258,7 +258,7 @@ func (h *handler) handleMsg(msg *jsonrpcMessage) { cancel() responded.Do(func() { resp := msg.errorResponse(&internalServerError{errcodeTimeout, errMsgTimeout}) - h.conn.writeJSON(cp.ctx, resp, true) + _ = h.conn.writeJSON(cp.ctx, resp, true) }) }) } @@ -270,11 +270,11 @@ func (h *handler) handleMsg(msg *jsonrpcMessage) { h.addSubscriptions(cp.notifiers) if answer != nil { responded.Do(func() { - h.conn.writeJSON(cp.ctx, answer, false) + _ = h.conn.writeJSON(cp.ctx, answer, false) }) } for _, n := range cp.notifiers { - n.activate() + _ = n.activate() } }) } diff --git a/rpc/server.go b/rpc/server.go index 2e12b802d4..eafebeff6b 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -156,7 +156,7 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) { if err != nil { if err != io.EOF { resp := errorMessage(&invalidMessageError{"parse error"}) - codec.writeJSON(ctx, resp, true) + _ = codec.writeJSON(ctx, resp, true) } return } diff --git a/rpc/types.go b/rpc/types.go index 130f71c276..d04dfe7d95 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -76,6 +76,7 @@ const ( // Returned errors: // - an invalid block number error when the given argument isn't a known strings // - an out of range error when the given block number is either too little or too large +// nolint:goconst func (bn *BlockNumber) UnmarshalJSON(data []byte) error { input := strings.TrimSpace(string(data)) if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {