Merge pull request #2674 from nspcc-dev/update-linter-config

Update linter config
This commit is contained in:
Roman Khimov 2022-09-02 20:17:58 +03:00 committed by GitHub
commit 457f904b1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 78 additions and 53 deletions

View file

@ -34,7 +34,7 @@ jobs:
- run:
name: go-lint
command: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.44.2
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.49.0
make lint
test_1_17:

View file

@ -32,20 +32,32 @@ linters:
- revive
# some default golangci-lint linters
- deadcode
- errcheck
- gosimple
- godot
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
# extra linters
# - exhaustive
# - goconst
# - goerr113
# - gomnd
# - nonamedreturns
# - unparam
- bidichk
- bodyclose
- contextcheck
- decorder
- durationcheck
- errorlint
- exportloopref
- gofmt
- misspell
- predeclared
- reassign
- whitespace
- goimports
disable-all: true

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

@ -693,15 +693,15 @@ func (bc *Blockchain) Run() {
}
}
func (bc *Blockchain) tryRunGC(old uint32) time.Duration {
func (bc *Blockchain) tryRunGC(oldHeight uint32) time.Duration {
var dur time.Duration
new := atomic.LoadUint32(&bc.persistedHeight)
var tgtBlock = int64(new)
newHeight := atomic.LoadUint32(&bc.persistedHeight)
var tgtBlock = int64(newHeight)
tgtBlock -= int64(bc.config.MaxTraceableBlocks)
if bc.config.P2PStateExchangeExtensions {
syncP := new / uint32(bc.config.StateSyncInterval)
syncP := newHeight / uint32(bc.config.StateSyncInterval)
syncP--
syncP *= uint32(bc.config.StateSyncInterval)
if tgtBlock > int64(syncP) {
@ -712,9 +712,9 @@ func (bc *Blockchain) tryRunGC(old uint32) time.Duration {
tgtBlock /= int64(bc.config.GarbageCollectionPeriod)
tgtBlock *= int64(bc.config.GarbageCollectionPeriod)
// Count periods.
old /= bc.config.GarbageCollectionPeriod
new /= bc.config.GarbageCollectionPeriod
if tgtBlock > int64(bc.config.GarbageCollectionPeriod) && new != old {
oldHeight /= bc.config.GarbageCollectionPeriod
newHeight /= bc.config.GarbageCollectionPeriod
if tgtBlock > int64(bc.config.GarbageCollectionPeriod) && newHeight != oldHeight {
tgtBlock /= int64(bc.config.GarbageCollectionPeriod)
tgtBlock *= int64(bc.config.GarbageCollectionPeriod)
dur = bc.stateRoot.GC(uint32(tgtBlock), bc.store)
@ -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

@ -77,7 +77,7 @@ func TestAddBlock(t *testing.T) {
}
func TestRemoveOldTransfers(t *testing.T) {
// Creating proper number of transfers/blocks takes unneccessary time, so emulate
// Creating proper number of transfers/blocks takes unnecessary time, so emulate
// some DB with stale entries.
bc := newTestChain(t)
h, err := bc.GetHeader(bc.GetHeaderHash(0))

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
@ -828,14 +828,14 @@ func (dao *Simple) StoreAsTransaction(tx *transaction.Transaction, index uint32,
return nil
}
func (dao *Simple) getKeyBuf(len int) []byte {
func (dao *Simple) getKeyBuf(l int) []byte {
if dao.private {
if dao.keyBuf == nil {
dao.keyBuf = make([]byte, 0, 1+4+limits.MaxStorageKeyLen) // Prefix, uint32, key.
}
return dao.keyBuf[:len] // Should have enough capacity.
return dao.keyBuf[:l] // Should have enough capacity.
}
return make([]byte, len)
return make([]byte, l)
}
func (dao *Simple) getDataBuf() *io.BufBinWriter {

View file

@ -37,7 +37,7 @@ type Billet struct {
// NewBillet returns a new billet for MPT trie restoring. It accepts a MemCachedStore
// to decouple storage errors from logic errors so that all storage errors are
// processed during `store.Persist()` at the caller. Another benifit is
// processed during `store.Persist()` at the caller. Another benefit is
// that every `Put` can be considered an atomic operation.
func NewBillet(rootHash util.Uint256, mode TrieMode, prefix storage.KeyPrefix, store *storage.MemCachedStore) *Billet {
return &Billet{

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

View file

@ -13,8 +13,8 @@ func TestValidatorUnmarshal(t *testing.T) {
require.NoError(t, json.Unmarshal(old, v))
require.Equal(t, int64(100500), v.Votes)
new := []byte(`{"publickey":"02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62","votes":42}`)
require.NoError(t, json.Unmarshal(new, v))
newV := []byte(`{"publickey":"02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62","votes":42}`)
require.NoError(t, json.Unmarshal(newV, v))
require.Equal(t, int64(42), v.Votes)
bad := []byte(`{"publickey":"02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62","votes":"notanumber"}`)

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

@ -90,7 +90,10 @@ var errConnClosedByUser = errors.New("connection closed by user")
// operating on.
func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) {
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
ws, _, err := dialer.Dial(endpoint, nil)
ws, resp, err := dialer.DialContext(ctx, endpoint, nil)
if resp != nil && resp.Body != nil { // Can be non-nil even with error returned.
defer resp.Body.Close() // Not exactly required by websocket, but let's do this for bodyclose checker.
}
if err != nil {
return nil, err
}

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

@ -40,7 +40,7 @@ func TestDefaultClient_RestrictedRedirectErr(t *testing.T) {
}
for _, c := range testCases {
t.Run(c, func(t *testing.T) {
_, err := cl.Get(c)
_, err := cl.Get(c) //nolint:bodyclose // It errors out and it's a test.
require.Error(t, err)
require.True(t, errors.Is(err, ErrRestrictedRedirect), err)
require.True(t, strings.Contains(err.Error(), "IP is not global unicast"), err)

View file

@ -138,6 +138,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err), zap.Stringer("code", resp.Code))
break
}
defer r.Body.Close()
switch r.StatusCode {
case http.StatusOK:
if !checkMediaType(r.Header.Get("Content-Type"), o.MainCfg.AllowedContentTypes) {

View file

@ -67,11 +67,9 @@ func (o *Oracle) AddResponse(pub *keys.PublicKey, reqID uint64, txSig []byte) {
var ErrResponseTooLarge = errors.New("too big response")
func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) {
defer rc.Close()
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

@ -2633,8 +2633,9 @@ func checkErrGetBatchResult(t *testing.T, body []byte, expectingFail bool) json.
func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte {
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
url = "ws" + strings.TrimPrefix(url, "http")
c, _, err := dialer.Dial(url+"/ws", nil)
c, r, err := dialer.Dial(url+"/ws", nil)
require.NoError(t, err)
defer r.Body.Close()
err = c.SetWriteDeadline(time.Now().Add(time.Second))
require.NoError(t, err)
require.NoError(t, c.WriteMessage(1, []byte(rpcCall)))
@ -2651,6 +2652,7 @@ func doRPCCallOverHTTP(rpcCall string, url string, t *testing.T) []byte {
resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall))
require.NoErrorf(t, err, "could not make a POST request")
body, err := gio.ReadAll(resp.Body)
resp.Body.Close()
assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall)
return bytes.TrimSpace(body)
}

View file

@ -59,8 +59,9 @@ func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *webso
dialer := websocket.Dialer{HandshakeTimeout: time.Second}
url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws"
ws, _, err := dialer.Dial(url, nil)
ws, r, err := dialer.Dial(url, nil)
require.NoError(t, err)
defer r.Body.Close()
// Use buffered channel to read server's messages and then read expected
// responses from it.
@ -520,7 +521,10 @@ func TestWSClientsLimit(t *testing.T) {
wss := make([]*websocket.Conn, maxSubscribers)
for i := 0; i < len(wss)+1; i++ {
ws, _, err := dialer.Dial(url, nil)
ws, r, err := dialer.Dial(url, nil)
if r != nil && r.Body != nil {
defer r.Body.Close()
}
if i < maxSubscribers {
require.NoError(t, err)
wss[i] = ws

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