Skip to content

Commit

Permalink
Merge PR cosmos#5055: Added Prealloc, Gosec, Golint linters
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored and alexanderbez committed Sep 17, 2019
1 parent 943cc54 commit 936cffe
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 30 deletions.
15 changes: 12 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down
2 changes: 1 addition & 1 deletion server/mock/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
10 changes: 5 additions & 5 deletions types/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
9 changes: 3 additions & 6 deletions types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion x/auth/types/stdtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/client/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions x/genutil/client/cli/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "", " ")
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions x/genutil/client/cli/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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,
Run: func(_ *cobra.Command, args []string) {},
}

c.Flags().String(flagGenesisTime, genesisTime, "")
c.Flags().String(flagChainId, chainId, "")
c.Flags().String(flagChainID, chainID, "")

return c
}
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down

0 comments on commit 936cffe

Please sign in to comment.