dev: fix: errorcheck lint issues

This commit is contained in:
marcello33 2023-06-15 13:18:25 +02:00
parent 218917076c
commit d9a82cd3cd
15 changed files with 47 additions and 37 deletions

View file

@ -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
}

View file

@ -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")

View file

@ -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)
}

View file

@ -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))

View file

@ -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)
}

View file

@ -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)

View file

@ -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.

View file

@ -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)
}

View file

@ -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(),

View file

@ -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)
}

View file

@ -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)

View file

@ -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

View file

@ -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()
}
})
}

View file

@ -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
}

View file

@ -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] == '"' {