Skip to content

Commit

Permalink
Merge PR cosmos#1424: tools: add unconvert linter
Browse files Browse the repository at this point in the history
unconvert checks for unnecessary type conversions
  • Loading branch information
ValarDragon authored and cwgoes committed Jun 28, 2018
1 parent 473ac4a commit 2755c66
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions client/context/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion client/lcd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 2755c66

Please sign in to comment.