Merge PR #5055: Added Prealloc, Gosec, Golint linters

This commit is contained in:
Marko 2019-09-17 18:13:26 +02:00 committed by Alexander Bezobchuk
parent 943cc54811
commit 936cffef40
13 changed files with 36 additions and 30 deletions

View File

@ -6,16 +6,25 @@ linters:
disable:
- gocyclo
- gochecknoinits
- golint
- gochecknoglobals
- dupl
- interfacer
- gosec
- unparam
- lll
- maligned
- nakedret
- errcheck
- scopelint
- prealloc
- varcheck
issues:
exclude-rules:
- text: "Use of weak random number generator"
linters:
- gosec
- text: "comment on exported var"
linters:
- golint
- text: "don't use an underscore in package name"
linters:
- golint

View File

@ -631,8 +631,8 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (result sdk.Result) {
msgLogs := make(sdk.ABCIMessageLogs, 0, len(msgs))
data := make([]byte, 0, len(msgs))
var (
data []byte
code sdk.CodeType
codespace sdk.CodespaceType
)

View File

@ -540,7 +540,7 @@ func (msg msgCounter) ValidateBasic() sdk.Error {
}
func newTxCounter(txInt int64, msgInts ...int64) *txTest {
var msgs []sdk.Msg
msgs := make([]sdk.Msg, 0, len(msgInts))
for _, msgInt := range msgInts {
msgs = append(msgs, msgCounter{msgInt, false})
}

View File

@ -19,7 +19,7 @@ func (ms multiStore) CacheMultiStore() sdk.CacheMultiStore {
panic("not implemented")
}
func (kv multiStore) CacheMultiStoreWithVersion(_ int64) (sdk.CacheMultiStore, error) {
func (ms multiStore) CacheMultiStoreWithVersion(_ int64) (sdk.CacheMultiStore, error) {
panic("not implemented")
}

View File

@ -153,23 +153,23 @@ func (e Error) Codespace() string {
// Is check if given error instance is of a given kind/type. This involves
// unwrapping given error using the Cause method if available.
func (kind *Error) Is(err error) bool {
func (e *Error) Is(err error) bool {
// Reflect usage is necessary to correctly compare with
// a nil implementation of an error.
if kind == nil {
if e == nil {
return isNilErr(err)
}
for {
if err == kind {
if err == e {
return true
}
// If this is a collection of errors, this function must return
// true if at least one from the group match.
if u, ok := err.(unpacker); ok {
for _, e := range u.Unpack() {
if kind.Is(e) {
for _, er := range u.Unpack() {
if e.Is(er) {
return true
}
}

View File

@ -174,11 +174,8 @@ func (se StringEvents) Flatten() StringEvents {
for _, e := range se {
flatEvents[e.Type] = append(flatEvents[e.Type], e.Attributes...)
}
var (
res StringEvents
keys []string
)
keys := make([]string, 0, len(flatEvents))
res := make(StringEvents, 0, len(flatEvents)) // appeneded to keys, same length of what is allocated to keys
for ty := range flatEvents {
keys = append(keys, ty)
@ -209,7 +206,7 @@ func StringifyEvent(e abci.Event) StringEvent {
// StringifyEvents converts a slice of Event objects into a slice of StringEvent
// objects.
func StringifyEvents(events []abci.Event) StringEvents {
var res StringEvents
res := make(StringEvents, 0, len(events))
for _, e := range events {
res = append(res, StringifyEvent(e))

View File

@ -201,7 +201,7 @@ type Manager struct {
func NewManager(modules ...AppModule) *Manager {
moduleMap := make(map[string]AppModule)
var modulesStr []string
modulesStr := make([]string, 0, len(modules))
for _, module := range modules {
moduleMap[module.Name()] = module
modulesStr = append(modulesStr, module.Name())

View File

@ -168,7 +168,7 @@ type StdSignDoc struct {
// StdSignBytes returns the bytes to sign for a transaction.
func StdSignBytes(chainID string, accnum uint64, sequence uint64, fee StdFee, msgs []sdk.Msg, memo string) []byte {
var msgsBytes []json.RawMessage
msgsBytes := make([]json.RawMessage, 0, len(msgs))
for _, msg := range msgs {
msgsBytes = append(msgsBytes, json.RawMessage(msg.GetSignBytes()))
}

View File

@ -108,7 +108,7 @@ func WithdrawAllDelegatorRewards(cliCtx context.CLIContext, queryRoute string, d
}
// build multi-message transaction
var msgs []sdk.Msg
msgs := make([]sdk.Msg, 0, len(validators))
for _, valAddr := range validators {
msg := types.NewMsgWithdrawDelegatorReward(delegatorAddr, valAddr)
if err := msg.ValidateBasic(); err != nil {

View File

@ -28,7 +28,7 @@ var migrationMap = extypes.MigrationMap{
const (
flagGenesisTime = "genesis-time"
flagChainId = "chain-id"
flagChainID = "chain-id"
)
func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command {
@ -72,9 +72,9 @@ $ %s migrate v0.36 /path/to/genesis.json --chain-id=cosmoshub-3 --genesis-time=2
genDoc.GenesisTime = t
}
chainId := cmd.Flag(flagChainId).Value.String()
if chainId != "" {
genDoc.ChainID = chainId
chainID := cmd.Flag(flagChainID).Value.String()
if chainID != "" {
genDoc.ChainID = chainID
}
out, err := cdc.MarshalJSONIndent(genDoc, "", " ")
@ -88,7 +88,7 @@ $ %s migrate v0.36 /path/to/genesis.json --chain-id=cosmoshub-3 --genesis-time=2
}
cmd.Flags().String(flagGenesisTime, "", "Override genesis_time with this flag")
cmd.Flags().String(flagChainId, "", "Override chain_id with this flag")
cmd.Flags().String(flagChainID, "", "Override chain_id with this flag")
return cmd
}

View File

@ -17,7 +17,7 @@ import (
"github.com/cosmos/cosmos-sdk/tests"
)
func setupCmd(genesisTime string, chainId string) *cobra.Command {
func setupCmd(genesisTime string, chainID string) *cobra.Command {
c := &cobra.Command{
Use: "c",
Args: cobra.ArbitraryArgs,
@ -25,7 +25,7 @@ func setupCmd(genesisTime string, chainId string) *cobra.Command {
}
c.Flags().String(flagGenesisTime, genesisTime, "")
c.Flags().String(flagChainId, chainId, "")
c.Flags().String(flagChainID, chainID, "")
return c
}

View File

@ -22,7 +22,7 @@ func SetGenTxsInAppGenesisState(cdc *codec.Codec, appGenesisState map[string]jso
genesisState := GetGenesisStateFromAppState(cdc, appGenesisState)
// convert all the GenTxs to JSON
var genTxsBz []json.RawMessage
genTxsBz := make([]json.RawMessage, 0, len(genTxs))
for _, genTx := range genTxs {
txBz, err := cdc.MarshalJSON(genTx)
if err != nil {

View File

@ -69,7 +69,7 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
// RegisterRESTRoutes registers the REST routes for the gov module.
func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
var proposalRESTHandlers []rest.ProposalRESTHandler
proposalRESTHandlers := make([]rest.ProposalRESTHandler, 0, len(a.proposalHandlers))
for _, proposalHandler := range a.proposalHandlers {
proposalRESTHandlers = append(proposalRESTHandlers, proposalHandler.RESTHandler(ctx))
}
@ -80,7 +80,7 @@ func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Rout
// GetTxCmd returns the root tx command for the gov module.
func (a AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
var proposalCLIHandlers []*cobra.Command
proposalCLIHandlers := make([]*cobra.Command, 0, len(a.proposalHandlers))
for _, proposalHandler := range a.proposalHandlers {
proposalCLIHandlers = append(proposalCLIHandlers, proposalHandler.CLIHandler(cdc))
}