From 2755c66545407d06743d706bdd0cd9c24826a487 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Thu, 28 Jun 2018 09:08:29 -0700 Subject: [PATCH] Merge PR #1424: tools: add unconvert linter unconvert checks for unnecessary type conversions --- CHANGELOG.md | 1 + Makefile | 2 +- client/context/helpers.go | 4 ++-- client/lcd/version.go | 2 +- tests/util.go | 2 +- tools/Makefile | 16 ++++++++++++++++ types/coin.go | 4 ++-- x/ibc/mapper.go | 2 +- x/slashing/tick.go | 4 ++-- 9 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd11eff37071..488ce17edc79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ FEATURES * misspell * gofmt * go vet -composites=false + * unconvert * [server] Default config now creates a profiler at port 6060, and increase p2p send/recv rates * [tests] Add WaitForNextNBlocksTM helper method * [types] Switches internal representation of Int/Uint/Rat to use pointers diff --git a/Makefile b/Makefile index f2fbdd2b8f86..8316955f33b2 100644 --- a/Makefile +++ b/Makefile @@ -108,7 +108,7 @@ test_cover: @bash tests/test_cover.sh test_lint: - gometalinter.v2 --disable-all --enable='golint' --enable='misspell' --linter='vet:go vet -composites=false:PATH:LINE:MESSAGE' --enable='vet' --vendor ./... + gometalinter.v2 --disable-all --enable='golint' --enable='misspell' --enable='unconvert' --linter='vet:go vet -composites=false:PATH:LINE:MESSAGE' --enable='vet' --vendor ./... find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s benchmark: diff --git a/client/context/helpers.go b/client/context/helpers.go index c782503b09b9..fa46c73fdf64 100644 --- a/client/context/helpers.go +++ b/client/context/helpers.go @@ -127,8 +127,8 @@ func (ctx CoreContext) SignAndBuild(name, passphrase string, msgs []sdk.Msg, cdc signMsg := auth.StdSignMsg{ ChainID: chainID, - AccountNumber: int64(accnum), - Sequence: int64(sequence), + AccountNumber: accnum, + Sequence: sequence, Msgs: msgs, Memo: memo, Fee: auth.NewStdFee(ctx.Gas, sdk.Coin{}), // TODO run simulate to estimate gas? diff --git a/client/lcd/version.go b/client/lcd/version.go index 0c8ef4ef6e77..be097393e07e 100644 --- a/client/lcd/version.go +++ b/client/lcd/version.go @@ -24,6 +24,6 @@ func NodeVersionRequestHandler(cdc *wire.Codec, ctx context.CoreContext) http.Ha w.Write([]byte(fmt.Sprintf("Could't query version. Error: %s", err.Error()))) return } - w.Write([]byte(version)) + w.Write(version) } } diff --git a/tests/util.go b/tests/util.go index cc95f1e24197..1b68a9fd9847 100644 --- a/tests/util.go +++ b/tests/util.go @@ -104,7 +104,7 @@ func waitForHeight(height int64, url string) { res.Body.Close() var resultBlock ctypes.ResultBlock - err = cdc.UnmarshalJSON([]byte(body), &resultBlock) + err = cdc.UnmarshalJSON(body, &resultBlock) if err != nil { fmt.Println("RES", res) fmt.Println("BODY", string(body)) diff --git a/tools/Makefile b/tools/Makefile index c623ba46ff24..bfa6a790db51 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -7,9 +7,12 @@ all: get_tools DEP = github.com/golang/dep/cmd/dep GOLINT = github.com/tendermint/lint/golint GOMETALINTER = gopkg.in/alecthomas/gometalinter.v2 +UNCONVERT = github.com/mdempsky/unconvert + DEP_CHECK := $(shell command -v dep 2> /dev/null) GOLINT_CHECK := $(shell command -v golint 2> /dev/null) GOMETALINTER_CHECK := $(shell command -v gometalinter.v2 2> /dev/null) +UNCONVERT_CHECK := $(shell command -v unconvert 2> /dev/null) check_tools: ifndef DEP_CHECK @@ -27,6 +30,11 @@ ifndef GOMETALINTER_CHECK else @echo "Found gometalinter in path." endif +ifndef UNCONVERT_CHECK + @echo "No unconvert in path. Install with 'make get_tools'." +else + @echo "Found unconvert in path." +endif get_tools: ifdef DEP_CHECK @@ -47,6 +55,12 @@ else @echo "Installing gometalinter.v2" go get -v $(GOMETALINTER) endif +ifdef UNCONVERT_CHECK + @echo "Unconvert is already installed. Run 'make update_tools' to update." +else + @echo "Installing unconvert" + go get -v $(UNCONVERT) +endif update_tools: @echo "Updating dep" @@ -55,6 +69,8 @@ update_tools: go get -u -v $(GOLINT) @echo "Updating gometalinter.v2" go get -u -v $(GOMETALINTER) + @echo "Updating unconvert" + go get -u -v $(UNCONVERT) # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. diff --git a/types/coin.go b/types/coin.go index 30f84abdf651..eba645932146 100644 --- a/types/coin.go +++ b/types/coin.go @@ -244,11 +244,11 @@ func (coins Coins) AmountOf(denom string) Int { midIdx := len(coins) / 2 // 2:1, 3:1, 4:2 coin := coins[midIdx] if denom < coin.Denom { - return Coins(coins[:midIdx]).AmountOf(denom) + return coins[:midIdx].AmountOf(denom) } else if denom == coin.Denom { return coin.Amount } else { - return Coins(coins[midIdx+1:]).AmountOf(denom) + return coins[midIdx+1:].AmountOf(denom) } } } diff --git a/x/ibc/mapper.go b/x/ibc/mapper.go index 06631179b5d5..25169961708d 100644 --- a/x/ibc/mapper.go +++ b/x/ibc/mapper.go @@ -39,7 +39,7 @@ func (ibcm Mapper) PostIBCPacket(ctx sdk.Context, packet IBCPacket) sdk.Error { } store.Set(EgressKey(packet.DestChain, index), bz) - bz, err = ibcm.cdc.MarshalBinary(int64(index + 1)) + bz, err = ibcm.cdc.MarshalBinary(index + 1) if err != nil { panic(err) } diff --git a/x/slashing/tick.go b/x/slashing/tick.go index 6eb6eeb32bb1..eed083b9a958 100644 --- a/x/slashing/tick.go +++ b/x/slashing/tick.go @@ -22,11 +22,11 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, sk Keeper) (tags if err != nil { panic(err) } - switch string(evidence.Type) { + switch evidence.Type { case tmtypes.ABCIEvidenceTypeDuplicateVote: sk.handleDoubleSign(ctx, evidence.Height, evidence.Time, pk) default: - ctx.Logger().With("module", "x/slashing").Error(fmt.Sprintf("ignored unknown evidence type: %s", string(evidence.Type))) + ctx.Logger().With("module", "x/slashing").Error(fmt.Sprintf("ignored unknown evidence type: %s", evidence.Type)) } }