Merge PR #1424: tools: add unconvert linter

unconvert checks for unnecessary type conversions
This commit is contained in:
Dev Ojha 2018-06-28 09:08:29 -07:00 committed by Christopher Goes
parent 473ac4a38e
commit 2755c66545
9 changed files with 27 additions and 10 deletions

View File

@ -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

View File

@ -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:

View File

@ -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?

View File

@ -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)
}
}

View File

@ -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))

View File

@ -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.

View File

@ -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)
}
}
}

View File

@ -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)
}

View File

@ -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))
}
}