golangci: enable errorlint and fix everything it found

This commit is contained in:
Roman Khimov 2022-09-02 14:29:47 +03:00
parent c703ac6805
commit 4f3ffe7290
17 changed files with 34 additions and 28 deletions

View file

@ -44,6 +44,7 @@ linters:
# - exhaustive
- bodyclose
- contextcheck
- errorlint
- gofmt
- whitespace
- goimports

View file

@ -1,6 +1,7 @@
package main
import (
"errors"
"io"
"os"
"path/filepath"
@ -110,7 +111,7 @@ func TestServerStart(t *testing.T) {
var line string
require.Eventually(t, func() bool {
line, err = e.Out.ReadString('\n')
if err != nil && err != io.EOF {
if err != nil && !errors.Is(err, io.EOF) {
t.Fatalf("unexpected error while reading CLI output: %s", err)
}
return err == nil

View file

@ -947,7 +947,7 @@ func readWallet(ctx *cli.Context) (*wallet.Wallet, *string, error) {
if path == "-" {
w := &wallet.Wallet{}
if err := json.NewDecoder(os.Stdin).Decode(w); err != nil {
return nil, nil, fmt.Errorf("js %s", err)
return nil, nil, fmt.Errorf("js %w", err)
}
return w, nil, nil
}

View file

@ -1702,7 +1702,7 @@ func (bc *Blockchain) HasTransaction(hash util.Uint256) bool {
if bc.memPool.ContainsKey(hash) {
return true
}
return bc.dao.HasTransaction(hash) == dao.ErrAlreadyExists
return errors.Is(bc.dao.HasTransaction(hash), dao.ErrAlreadyExists)
}
// HasBlock returns true if the blockchain contains the given
@ -1761,7 +1761,7 @@ func (bc *Blockchain) HeaderHeight() uint32 {
// GetContractState returns contract by its script hash.
func (bc *Blockchain) GetContractState(hash util.Uint160) *state.Contract {
contract, err := bc.contracts.Management.GetContract(bc.dao, hash)
if contract == nil && err != storage.ErrKeyNotFound {
if contract == nil && !errors.Is(err, storage.ErrKeyNotFound) {
bc.log.Warn("failed to get contract state", zap.Error(err))
}
return contract

View file

@ -176,7 +176,7 @@ func (dao *Simple) GetTokenTransferInfo(acc util.Uint160) (*state.TokenTransferI
key := dao.makeTTIKey(acc)
bs := state.NewTokenTransferInfo()
err := dao.GetAndDecode(bs, key)
if err != nil && err != storage.ErrKeyNotFound {
if err != nil && !errors.Is(err, storage.ErrKeyNotFound) {
return nil, err
}
return bs, nil
@ -257,7 +257,7 @@ func (dao *Simple) GetTokenTransferLog(acc util.Uint160, newestTimestamp uint64,
key := dao.getTokenTransferLogKey(acc, newestTimestamp, index, isNEP11)
value, err := dao.Store.Get(key)
if err != nil {
if err == storage.ErrKeyNotFound {
if errors.Is(err, storage.ErrKeyNotFound) {
return new(state.TokenTransferLog), nil
}
return nil, err
@ -306,7 +306,7 @@ func (dao *Simple) GetAppExecResults(hash util.Uint256, trig trigger.Type) ([]st
aer := new(state.AppExecResult)
aer.DecodeBinary(r)
if r.Err != nil {
if r.Err == iocore.EOF {
if errors.Is(r.Err, iocore.EOF) {
break
}
return nil, r.Err

View file

@ -173,7 +173,7 @@ func (m *Management) getContract(ic *interop.Context, args []stackitem.Item) sta
hash := toHash160(args[0])
ctr, err := m.GetContract(ic.DAO, hash)
if err != nil {
if err == storage.ErrKeyNotFound {
if errors.Is(err, storage.ErrKeyNotFound) {
return stackitem.Null{}
}
panic(err)

View file

@ -469,7 +469,7 @@ func (n *Notary) GetDepositFor(dao *dao.Simple, acc util.Uint160) *state.Deposit
if err == nil {
return deposit
}
if err == storage.ErrKeyNotFound {
if errors.Is(err, storage.ErrKeyNotFound) {
return nil
}
panic(fmt.Errorf("failed to get deposit for %s from storage: %w", acc.StringBE(), err))

View file

@ -1,6 +1,8 @@
package storage
import (
"errors"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/filter"
@ -35,7 +37,7 @@ func NewLevelDBStore(cfg dbconfig.LevelDBOptions) (*LevelDBStore, error) {
// Get implements the Store interface.
func (s *LevelDBStore) Get(key []byte) ([]byte, error) {
value, err := s.db.Get(key, nil)
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
err = ErrKeyNotFound
}
return value, err

View file

@ -1,6 +1,7 @@
package neorpc
import (
"errors"
"fmt"
)
@ -125,9 +126,9 @@ func (e *Error) Error() string {
// Is denotes whether the error matches the target one.
func (e *Error) Is(target error) bool {
clTarget, ok := target.(*Error)
if !ok {
return false
var clTarget *Error
if errors.As(target, &clTarget) {
return e.Code == clTarget.Code
}
return e.Code == clTarget.Code
return false
}

View file

@ -176,7 +176,7 @@ func (m *Message) decodePayload() error {
}
r := io.NewBinReaderFromBuf(buf)
p.DecodeBinary(r)
if r.Err == nil || r.Err == payload.ErrTooManyHeaders {
if r.Err == nil || errors.Is(r.Err, payload.ErrTooManyHeaders) {
m.Payload = p
}

View file

@ -417,9 +417,9 @@ func (s *Server) run() {
zap.Error(drop.reason),
zap.Int("peerCount", s.PeerCount()))
addr := drop.peer.PeerAddr().String()
if drop.reason == errIdenticalID {
if errors.Is(drop.reason, errIdenticalID) {
s.discovery.RegisterBadAddr(addr)
} else if drop.reason == errAlreadyConnected {
} else if errors.Is(drop.reason, errAlreadyConnected) {
// There is a race condition when peer can be disconnected twice for the this reason
// which can lead to no connections to peer at all. Here we check for such a possibility.
stillConnected := false
@ -1380,15 +1380,15 @@ func (s *Server) iteratePeersWithSendMsg(msg *Message, send func(Peer, bool, []b
continue
}
err := send(peer, blocking, pkt)
switch err {
case nil:
if err == nil {
if msg.Command == CMDGetAddr {
peer.AddGetAddrSent()
}
sentN++
case errBusy: // Can be retried.
} else if errors.Is(err, errBusy) {
// Can be retried.
continue
default:
} else {
deadN++
}
finished[i] = true

View file

@ -170,7 +170,7 @@ func (p *TCPPeer) handleConn() {
msg := &Message{StateRootInHeader: p.server.config.StateRootInHeader}
err = msg.Decode(r)
if err == payload.ErrTooManyHeaders {
if errors.Is(err, payload.ErrTooManyHeaders) {
p.server.log.Warn("not all headers were processed")
r.Err = nil
} else if err != nil {

View file

@ -2,6 +2,7 @@ package metrics
import (
"context"
"errors"
"net/http"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -21,7 +22,7 @@ func (ms *Service) Start() {
if ms.config.Enabled {
ms.log.Info("service is running", zap.String("endpoint", ms.Addr))
err := ms.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
if err != nil && !errors.Is(err, http.ErrServerClosed) {
ms.log.Warn("service couldn't start on configured port")
}
} else {

View file

@ -69,7 +69,7 @@ var ErrResponseTooLarge = errors.New("too big response")
func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) {
buf := make([]byte, limit+1)
n, err := gio.ReadFull(rc, buf)
if err == gio.ErrUnexpectedEOF && n <= limit {
if errors.Is(err, gio.ErrUnexpectedEOF) && n <= limit {
return buf[:n], nil
}
if err == nil || n > limit {

View file

@ -340,7 +340,7 @@ func (s *Server) Start() {
}
s.https.Addr = ln.Addr().String()
err = s.https.ServeTLS(ln, cfg.CertFile, cfg.KeyFile)
if err != http.ErrServerClosed {
if !errors.Is(err, http.ErrServerClosed) {
s.log.Error("failed to start TLS RPC server", zap.Error(err))
s.errChan <- err
}
@ -354,7 +354,7 @@ func (s *Server) Start() {
s.Addr = ln.Addr().String() // set Addr to the actual address
go func() {
err = s.Serve(ln)
if err != http.ErrServerClosed {
if !errors.Is(err, http.ErrServerClosed) {
s.log.Error("failed to start RPC server", zap.Error(err))
s.errChan <- err
}

View file

@ -678,7 +678,7 @@ func (c *VMCLI) Run() error {
l := getReadlineInstanceFromContext(c.shell)
for {
line, err := l.Readline()
if err == io.EOF || err == readline.ErrInterrupt {
if errors.Is(err, io.EOF) || errors.Is(err, readline.ErrInterrupt) {
return nil // OK, stop execution.
}
if err != nil {

View file

@ -169,7 +169,7 @@ func FromJSON(data []byte, maxCount int) (Item, error) {
d.UseNumber()
if item, err := d.decode(); err != nil {
return nil, err
} else if _, err := d.Token(); err != gio.EOF {
} else if _, err := d.Token(); !errors.Is(err, gio.EOF) {
return nil, fmt.Errorf("%w: unexpected items", ErrInvalidValue)
} else {
return item, nil