From 4ffabb65a5c07dbb7010da397535d10927d298c1 Mon Sep 17 00:00:00 2001 From: Marko Date: Fri, 21 Jun 2019 17:18:01 +0200 Subject: [PATCH] lint add deadcode unused (#4606) Added deadcode and unused linters to the repo, it helped find some unused code. Ref #4589 --- .golangci.yml | 2 ++ client/keys/errors.go | 31 --------------------------- client/lcd/root.go | 5 ++--- store/cachekv/memiterator.go | 15 ------------- store/list/list.go | 7 ------ store/prefix/store.go | 28 ------------------------ store/types/gas.go | 5 ----- store/wire.go | 7 ------ types/coin.go | 6 ------ types/errors.go | 1 + types/int.go | 3 --- types/uint.go | 3 --- x/auth/keeper.go | 14 ------------ x/distribution/keeper/test_common.go | 1 + x/distribution/types/test_common.go | 1 + x/genutil/collect.go | 2 -- x/genutil/gentx.go | 9 -------- x/gov/test_common.go | 1 + x/mint/internal/keeper/test_common.go | 1 + x/params/test_common.go | 1 + x/slashing/test_common.go | 1 + x/staking/client/cli/flags.go | 1 - x/staking/keeper/test_common.go | 8 +++---- x/staking/test_common.go | 1 + x/staking/types/test_utils.go | 1 + 25 files changed, 17 insertions(+), 138 deletions(-) delete mode 100644 client/keys/errors.go delete mode 100644 store/wire.go diff --git a/.golangci.yml b/.golangci.yml index 70dede1be9..b29f15e40a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,6 +7,8 @@ linters: - unconvert - misspell - govet + - unused + - deadcode - goconst linters-settings: gocyclo: diff --git a/client/keys/errors.go b/client/keys/errors.go deleted file mode 100644 index a603b1d1a6..0000000000 --- a/client/keys/errors.go +++ /dev/null @@ -1,31 +0,0 @@ -package keys - -import "fmt" - -func errKeyNameConflict(name string) error { - return fmt.Errorf("account with name %s already exists", name) -} - -func errMissingName() error { - return fmt.Errorf("you have to specify a name for the locally stored account") -} - -func errMissingPassword() error { - return fmt.Errorf("you have to specify a password for the locally stored account") -} - -func errMissingMnemonic() error { - return fmt.Errorf("you have to specify a mnemonic for key recovery") -} - -func errInvalidMnemonic() error { - return fmt.Errorf("the mnemonic is invalid") -} - -func errInvalidAccountNumber() error { - return fmt.Errorf("the account number is invalid") -} - -func errInvalidIndexNumber() error { - return fmt.Errorf("the index number is invalid") -} diff --git a/client/lcd/root.go b/client/lcd/root.go index 8f64ad7ae9..56d5d35a41 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -30,9 +30,8 @@ type RestServer struct { CliCtx context.CLIContext KeyBase keybase.Keybase - log log.Logger - listener net.Listener - fingerprint string + log log.Logger + listener net.Listener } // NewRestServer creates a new rest server instance diff --git a/store/cachekv/memiterator.go b/store/cachekv/memiterator.go index 9e5a6ac331..bfc9b06136 100644 --- a/store/cachekv/memiterator.go +++ b/store/cachekv/memiterator.go @@ -1,7 +1,6 @@ package cachekv import ( - "bytes" "container/list" cmn "github.com/tendermint/tendermint/libs/common" @@ -84,17 +83,3 @@ func (mi *memIterator) Close() { mi.end = nil mi.items = nil } - -//---------------------------------------- -// Misc. - -// bytes.Compare but bounded on both sides by nil. -// both (k1, nil) and (nil, k2) return -1 -func keyCompare(k1, k2 []byte) int { - if k1 == nil && k2 == nil { - return 0 - } else if k1 == nil || k2 == nil { - return -1 - } - return bytes.Compare(k1, k2) -} diff --git a/store/list/list.go b/store/list/list.go index 934dfaa0f1..96193adf85 100644 --- a/store/list/list.go +++ b/store/list/list.go @@ -103,10 +103,3 @@ func (m List) Iterate(ptr interface{}, fn func(uint64) bool) { } } } - -func subspace(prefix []byte) (start, end []byte) { - end = make([]byte, len(prefix)) - copy(end, prefix) - end[len(end)-1]++ - return prefix, end -} diff --git a/store/prefix/store.go b/store/prefix/store.go index a04995273b..1b69787f72 100644 --- a/store/prefix/store.go +++ b/store/prefix/store.go @@ -188,31 +188,3 @@ func stripPrefix(key []byte, prefix []byte) []byte { func cpIncr(bz []byte) []byte { return types.PrefixEndBytes(bz) } - -// copied from github.com/tendermint/tendermint/libs/db/util.go -func cpDecr(bz []byte) (ret []byte) { - if len(bz) == 0 { - panic("cpDecr expects non-zero bz length") - } - ret = make([]byte, len(bz)) - copy(ret, bz) - for i := len(bz) - 1; i >= 0; i-- { - if ret[i] > byte(0x00) { - ret[i]-- - return - } - ret[i] = byte(0xFF) - if i == 0 { - return nil - } - } - return nil -} - -func skipOne(iter types.Iterator, skipKey []byte) { - if iter.Valid() { - if bytes.Equal(iter.Key(), skipKey) { - iter.Next() - } - } -} diff --git a/store/types/gas.go b/store/types/gas.go index 0acbcd7e22..a5a2c5e1d1 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -14,11 +14,6 @@ const ( GasDeleteDesc = "Delete" ) -var ( - cachedKVGasConfig = KVGasConfig() - cachedTransientGasConfig = TransientGasConfig() -) - // Gas measured by the SDK type Gas = uint64 diff --git a/store/wire.go b/store/wire.go deleted file mode 100644 index 5724c8e548..0000000000 --- a/store/wire.go +++ /dev/null @@ -1,7 +0,0 @@ -package store - -import ( - "github.com/cosmos/cosmos-sdk/codec" -) - -var cdc = codec.New() diff --git a/types/coin.go b/types/coin.go index 055f2ea51d..84a0e60369 100644 --- a/types/coin.go +++ b/types/coin.go @@ -569,12 +569,6 @@ func removeZeroCoins(coins Coins) Coins { return coins[:i] } -func copyCoins(coins Coins) Coins { - copyCoins := make(Coins, len(coins)) - copy(copyCoins, coins) - return copyCoins -} - //----------------------------------------------------------------------------- // Sort interface diff --git a/types/errors.go b/types/errors.go index 82f400c54f..e5c3b1a26b 100644 --- a/types/errors.go +++ b/types/errors.go @@ -301,6 +301,7 @@ func AppendMsgToErr(msg string, err string) string { } // returns the index of the message in the ABCI Log +// nolint: deadcode unused func mustGetMsgIndex(abciLog string) int { msgIdx := strings.Index(abciLog, "message\":\"") if msgIdx == -1 { diff --git a/types/int.go b/types/int.go index 98770b4bdf..41a6efef74 100644 --- a/types/int.go +++ b/types/int.go @@ -6,7 +6,6 @@ import ( "testing" "math/big" - "math/rand" ) const maxBitLen = 255 @@ -37,8 +36,6 @@ func mod(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Mod(i, i2) } func neg(i *big.Int) *big.Int { return new(big.Int).Neg(i) } -func random(i *big.Int) *big.Int { return new(big.Int).Rand(rand.New(rand.NewSource(rand.Int63())), i) } - func min(i *big.Int, i2 *big.Int) *big.Int { if i.Cmp(i2) == 1 { return new(big.Int).Set(i2) diff --git a/types/uint.go b/types/uint.go index d6e129e696..4bb71fff04 100644 --- a/types/uint.go +++ b/types/uint.go @@ -106,9 +106,6 @@ func MaxUint(u1, u2 Uint) Uint { return NewUintFromBigInt(max(u1.i, u2.i)) } // Human readable string func (u Uint) String() string { return u.i.String() } -// Testing purpose random Uint generator -func randomUint(u Uint) Uint { return NewUintFromBigInt(random(u.i)) } - // MarshalAmino defines custom encoding scheme func (u Uint) MarshalAmino() (string, error) { if u.i == nil { // Necessary since default Uint initialization has i.i as nil diff --git a/x/auth/keeper.go b/x/auth/keeper.go index 7d7741054e..daa646fc33 100644 --- a/x/auth/keeper.go +++ b/x/auth/keeper.go @@ -142,20 +142,6 @@ func (ak AccountKeeper) GetSequence(ctx sdk.Context, addr sdk.AccAddress) (uint6 return acc.GetSequence(), nil } -func (ak AccountKeeper) setSequence(ctx sdk.Context, addr sdk.AccAddress, newSequence uint64) sdk.Error { - acc := ak.GetAccount(ctx, addr) - if acc == nil { - return sdk.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", addr)) - } - - if err := acc.SetSequence(newSequence); err != nil { - panic(err) - } - - ak.SetAccount(ctx, acc) - return nil -} - // GetNextAccountNumber Returns and increments the global account number counter func (ak AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 { var accNumber uint64 diff --git a/x/distribution/keeper/test_common.go b/x/distribution/keeper/test_common.go index 80653ac780..5393a81a36 100644 --- a/x/distribution/keeper/test_common.go +++ b/x/distribution/keeper/test_common.go @@ -22,6 +22,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/types" ) +//nolint: deadcode unused var ( delPk1 = ed25519.GenPrivKey().PubKey() delPk2 = ed25519.GenPrivKey().PubKey() diff --git a/x/distribution/types/test_common.go b/x/distribution/types/test_common.go index 480244ac36..2b36925a23 100644 --- a/x/distribution/types/test_common.go +++ b/x/distribution/types/test_common.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// nolint: deadcode unused var ( delPk1 = ed25519.GenPrivKey().PubKey() delPk2 = ed25519.GenPrivKey().PubKey() diff --git a/x/genutil/collect.go b/x/genutil/collect.go index 01636f6561..fe2d81531f 100644 --- a/x/genutil/collect.go +++ b/x/genutil/collect.go @@ -22,8 +22,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking" ) -const flagGenTxDir = "gentx-dir" - // common config options for init type InitConfig struct { ChainID string diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index d6a8cece47..82c89f2db9 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -14,15 +14,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking" ) -var ( - defaultTokens = sdk.TokensFromConsensusPower(100) - defaultAmount = defaultTokens.String() + sdk.DefaultBondDenom - defaultCommissionRate = "0.1" - defaultCommissionMaxRate = "0.2" - defaultCommissionMaxChangeRate = "0.01" - defaultMinSelfDelegation = "1" -) - // ValidateAccountInGenesis checks that the provided key has sufficient // coins in the genesis accounts func ValidateAccountInGenesis(appGenesisState map[string]json.RawMessage, diff --git a/x/gov/test_common.go b/x/gov/test_common.go index ff66bdb626..861e9f66bf 100644 --- a/x/gov/test_common.go +++ b/x/gov/test_common.go @@ -1,3 +1,4 @@ +// nolint:deadcode unused package gov import ( diff --git a/x/mint/internal/keeper/test_common.go b/x/mint/internal/keeper/test_common.go index 3d3e24061b..3aa27cff0f 100644 --- a/x/mint/internal/keeper/test_common.go +++ b/x/mint/internal/keeper/test_common.go @@ -1,3 +1,4 @@ +// nolint:deadcode unused package keeper import ( diff --git a/x/params/test_common.go b/x/params/test_common.go index 3cbd5db837..4d17460df0 100644 --- a/x/params/test_common.go +++ b/x/params/test_common.go @@ -1,3 +1,4 @@ +// nolint: deadcode unused package params import ( diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index 25e158df46..b9f22bcb30 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -1,3 +1,4 @@ +// nolint:deadcode unused package slashing import ( diff --git a/x/staking/client/cli/flags.go b/x/staking/client/cli/flags.go index 761d533ee1..563598409c 100644 --- a/x/staking/client/cli/flags.go +++ b/x/staking/client/cli/flags.go @@ -43,7 +43,6 @@ var ( FsMinSelfDelegation = flag.NewFlagSet("", flag.ContinueOnError) fsDescriptionEdit = flag.NewFlagSet("", flag.ContinueOnError) fsValidator = flag.NewFlagSet("", flag.ContinueOnError) - fsDelegator = flag.NewFlagSet("", flag.ContinueOnError) fsRedelegation = flag.NewFlagSet("", flag.ContinueOnError) ) diff --git a/x/staking/keeper/test_common.go b/x/staking/keeper/test_common.go index 6b5cc33aa6..fd18d07c01 100644 --- a/x/staking/keeper/test_common.go +++ b/x/staking/keeper/test_common.go @@ -26,11 +26,10 @@ import ( ) // dummy addresses used for testing +// nolint: unused deadcode var ( - Addrs = createTestAddrs(500) - PKs = createTestPubKeys(500) - emptyAddr sdk.AccAddress - emptyPubkey crypto.PubKey + Addrs = createTestAddrs(500) + PKs = createTestPubKeys(500) addrDels = []sdk.AccAddress{ Addrs[0], @@ -258,6 +257,7 @@ func TestingUpdateValidator(keeper Keeper, ctx sdk.Context, validator types.Vali return validator } +// nolint: deadcode unused func validatorByPowerIndexExists(k Keeper, ctx sdk.Context, power []byte) bool { store := ctx.KVStore(k.storeKey) return store.Has(power) diff --git a/x/staking/test_common.go b/x/staking/test_common.go index c5f604909a..94f0f9a9aa 100644 --- a/x/staking/test_common.go +++ b/x/staking/test_common.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) +// nolint: deadcode unused var ( priv1 = secp256k1.GenPrivKey() addr1 = sdk.AccAddress(priv1.PubKey().Address()) diff --git a/x/staking/types/test_utils.go b/x/staking/types/test_utils.go index 377d7f8844..e5c84b4144 100644 --- a/x/staking/types/test_utils.go +++ b/x/staking/types/test_utils.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// nolint: deadcode unused var ( pk1 = ed25519.GenPrivKey().PubKey() pk2 = ed25519.GenPrivKey().PubKey()