Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more linting #296

Merged
merged 4 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ version: 2
jobs:
build:
docker:
- image: circleci/golang:1.12.0
- image: circleci/golang:1.13

steps:
- checkout
- restore_cache:
keys:
- go-mod-v1-{{ checksum "go.sum" }}
keys:
- go-mod-v1-{{ checksum "go.sum" }}
- run:
name: test
command: |
export PATH="$GOBIN:$PATH"
go env
go version
make get_tools && make && make test
name: test
command: |
export PATH="$GOBIN:$PATH"
go env
go version
make get_tools && make && make test
- save_cache:
key: go-mod-v1-{{ checksum "go.sum" }}
paths:
Expand Down
58 changes: 48 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
run:
deadline: 1m

linters:
enable-all: true
disable:
- gocyclo
- maligned
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
# - funlen
# - gochecknoglobals
# - gochecknoinits
- goconst
- gocritic
# - gocyclo
# - godox
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- unparam
- interfacer
- lll
- misspell
# - maligned
- nakedret
- gochecknoglobals
- gochecknoinits
- prealloc
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
# - unparam
- unused
- varcheck
# - whitespace
# - wsl
# - gocognit

issues:
exclude-rules:
- linters:
- lll
source: "https://"

linters-settings:
dogsled:
max-blank-identifiers: 3
govet:
check-shadowing: true
18 changes: 13 additions & 5 deletions amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ func (cdc *Codec) MarshalBinaryLengthPrefixed(o interface{}) ([]byte, error) {
// MarshalBinaryLengthPrefixedWriter writes the bytes as would be returned from
// MarshalBinaryLengthPrefixed to the writer w.
func (cdc *Codec) MarshalBinaryLengthPrefixedWriter(w io.Writer, o interface{}) (n int64, err error) {
var bz, _n = []byte(nil), int(0)
var (
bz []byte
_n int
)
bz, err = cdc.MarshalBinaryLengthPrefixed(o)
if err != nil {
return 0, err
Expand Down Expand Up @@ -216,7 +219,7 @@ func (cdc *Codec) MarshalBinaryBare(o interface{}) ([]byte, error) {
writeEmpty := false
typ3 := typeToTyp3(info.Type, FieldOptions{})
bare := typ3 != Typ3ByteLength
if err := cdc.writeFieldIfNotEmpty(buf, 1, info, FieldOptions{}, FieldOptions{}, rv, writeEmpty, bare); err != nil {
if err = cdc.writeFieldIfNotEmpty(buf, 1, info, FieldOptions{}, FieldOptions{}, rv, writeEmpty, bare); err != nil {
return nil, err
}
bz = buf.Bytes()
Expand Down Expand Up @@ -343,7 +346,7 @@ func (cdc *Codec) UnmarshalBinaryLengthPrefixedReader(r io.Reader, ptr interface

// Decode.
err = cdc.UnmarshalBinaryBare(bz, ptr)
return
return n, err
}

// Panics if error.
Expand Down Expand Up @@ -395,7 +398,12 @@ func (cdc *Codec) UnmarshalBinaryBare(bz []byte, ptr interface{}) error {
len(bz) > 0 &&
(rv.Kind() != reflect.Interface) &&
isKnownType {
fnum, typ, nFnumTyp3, err := decodeFieldNumberAndTyp3(bz)
var (
fnum uint32
typ Typ3
nFnumTyp3 int
)
fnum, typ, nFnumTyp3, err = decodeFieldNumberAndTyp3(bz)
if err != nil {
return errors.Wrap(err, "could not decode field number and type")
}
Expand Down Expand Up @@ -505,7 +513,7 @@ func (cdc *Codec) MarshalJSON(o interface{}) ([]byte, error) {
}

// Write the rest from rv.
if err := cdc.encodeReflectJSON(w, info, rv, FieldOptions{}); err != nil {
if err = cdc.encodeReflectJSON(w, info, rv, FieldOptions{}); err != nil {
return nil, err
}

Expand Down
81 changes: 56 additions & 25 deletions binary-decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
// CONTRACT: rv.CanAddr() is true.
func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo,
rv reflect.Value, fopts FieldOptions, bare bool) (n int, err error) {

if !rv.CanAddr() {
panic("rv not addressable")
}
Expand Down Expand Up @@ -60,7 +61,8 @@ func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo,
// Handle override if a pointer to rv implements UnmarshalAmino.
if info.IsAminoUnmarshaler {
// First, decode repr instance from bytes.
rrv, rinfo := reflect.New(info.AminoUnmarshalReprType).Elem(), (*TypeInfo)(nil)
rrv := reflect.New(info.AminoUnmarshalReprType).Elem()
var rinfo *TypeInfo
rinfo, err = cdc.getTypeInfoWlock(info.AminoUnmarshalReprType)
if err != nil {
return
Expand Down Expand Up @@ -330,7 +332,10 @@ func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv re

if !bare {
// Read byte-length prefixed byteslice.
var buf, _n = []byte(nil), int(0)
var (
buf []byte
_n int
)
buf, _n, err = DecodeByteSlice(bz)
if slide(&bz, nil, _n) && err != nil {
return
Expand All @@ -343,7 +348,7 @@ func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv re
// Consume disambiguation / prefix bytes.
disamb, hasDisamb, prefix, hasPrefix, _n, err := DecodeDisambPrefixBytes(bz)
if slide(&bz, &n, _n) && err != nil {
return
return n, err
}

// Get concrete type info from disfix/prefix.
Expand All @@ -369,7 +374,12 @@ func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv re
(crv.Kind() != reflect.Interface) &&
isKnownType &&
fopts.BinFieldNum == 1 {
fnum, typ, nFnumTyp3, err := decodeFieldNumberAndTyp3(bz)
var (
fnum uint32
typ Typ3
nFnumTyp3 int
)
fnum, typ, nFnumTyp3, err = decodeFieldNumberAndTyp3(bz)
if err != nil {
return n, errors.Wrap(err, "could not decode field number and type")
}
Expand Down Expand Up @@ -404,7 +414,7 @@ func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv re
// NOTE: rv.Set() should succeed because it was validated
// already during Register[Interface/Concrete].
rv.Set(irvSet)
return
return n, err
}

// CONTRACT: rv.CanAddr() is true.
Expand All @@ -429,8 +439,7 @@ func (cdc *Codec) decodeReflectBinaryByteArray(bz []byte, info *TypeInfo, rv ref
}

// Read byte-length prefixed byteslice.
var byteslice, _n = []byte(nil), int(0)
byteslice, _n, err = DecodeByteSlice(bz)
byteslice, _n, err := DecodeByteSlice(bz)
if slide(&bz, &n, _n) && err != nil {
return
}
Expand All @@ -442,7 +451,7 @@ func (cdc *Codec) decodeReflectBinaryByteArray(bz []byte, info *TypeInfo, rv ref

// Copy read byteslice to rv array.
reflect.Copy(rv, reflect.ValueOf(byteslice))
return
return n, err
}

// CONTRACT: rv.CanAddr() is true.
Expand Down Expand Up @@ -470,7 +479,10 @@ func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect

if !bare {
// Read byte-length prefixed byteslice.
var buf, _n = []byte(nil), int(0)
var (
buf []byte
_n int
)
buf, _n, err = DecodeByteSlice(bz)
if slide(&bz, nil, _n) && err != nil {
return
Expand All @@ -487,7 +499,8 @@ func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect
if typ3 != Typ3ByteLength {
// Read elements in packed form.
for i := 0; i < length; i++ {
var erv, _n = rv.Index(i), int(0)
erv := rv.Index(i)
var _n int
_n, err = cdc.decodeReflectBinary(bz, einfo, erv, fopts, false)
if slide(&bz, &n, _n) && err != nil {
err = fmt.Errorf("error reading array contents: %v", err)
Expand All @@ -514,7 +527,11 @@ func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect
// Read elements in unpacked form.
for i := 0; i < length; i++ {
// Read field key (number and type).
var fnum, typ, _n = uint32(0), Typ3(0x00), int(0)
var (
fnum uint32
typ Typ3
_n int
)
fnum, typ, _n, err = decodeFieldNumberAndTyp3(bz)
// Validate field number and typ3.
if fnum != fopts.BinFieldNum {
Expand Down Expand Up @@ -556,7 +573,7 @@ func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect
// and no field number regression either.
// This is to provide better error messages.
if len(bz) > 0 {
var fnum = uint32(0)
var fnum uint32
fnum, _, _, err = decodeFieldNumberAndTyp3(bz)
if err != nil {
return
Expand All @@ -567,7 +584,7 @@ func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect
}
}
}
return
return n, err
}

// CONTRACT: rv.CanAddr() is true.
Expand All @@ -593,7 +610,10 @@ func (cdc *Codec) decodeReflectBinaryByteSlice(bz []byte, info *TypeInfo, rv ref
}

// Read byte-length prefixed byteslice.
var byteslice, _n = []byte(nil), int(0)
var (
byteslice []byte
_n int
)
byteslice, _n, err = DecodeByteSlice(bz)
if slide(&bz, &n, _n) && err != nil {
return
Expand All @@ -605,7 +625,7 @@ func (cdc *Codec) decodeReflectBinaryByteSlice(bz []byte, info *TypeInfo, rv ref
} else {
rv.Set(reflect.ValueOf(byteslice))
}
return
return n, err
}

// CONTRACT: rv.CanAddr() is true.
Expand Down Expand Up @@ -637,7 +657,10 @@ func (cdc *Codec) decodeReflectBinarySlice(bz []byte, info *TypeInfo, rv reflect

if !bare {
// Read byte-length prefixed byteslice.
var buf, _n = []byte(nil), int(0)
var (
buf []byte
_n int
)
buf, _n, err = DecodeByteSlice(bz)
if slide(&bz, nil, _n) && err != nil {
return
Expand Down Expand Up @@ -684,7 +707,11 @@ func (cdc *Codec) decodeReflectBinarySlice(bz []byte, info *TypeInfo, rv reflect
break
}
// Read field key (number and type).
var fnum, typ, _n = uint32(0), Typ3(0x00), int(0)
var (
typ Typ3
_n int
fnum uint32
)
fnum, typ, _n, err = decodeFieldNumberAndTyp3(bz)
// Validate field number and typ3.
if fnum < fopts.BinFieldNum {
Expand Down Expand Up @@ -729,7 +756,7 @@ func (cdc *Codec) decodeReflectBinarySlice(bz []byte, info *TypeInfo, rv reflect
}
}
rv.Set(srv)
return
return n, err
}

// CONTRACT: rv.CanAddr() is true.
Expand All @@ -751,7 +778,7 @@ func (cdc *Codec) decodeReflectBinaryStruct(bz []byte, info *TypeInfo, rv reflec

if !bare {
// Read byte-length prefixed byteslice.
var buf, _n = []byte(nil), int(0)
var buf []byte
buf, _n, err = DecodeByteSlice(bz)
if slide(&bz, nil, _n) && err != nil {
return
Expand Down Expand Up @@ -800,7 +827,10 @@ func (cdc *Codec) decodeReflectBinaryStruct(bz []byte, info *TypeInfo, rv reflec
}
} else {
// Read field key (number and type).
var fnum, typ = uint32(0), Typ3(0x00)
var (
fnum uint32
typ Typ3
)
fnum, typ, _n, err = decodeFieldNumberAndTyp3(bz)
if field.BinFieldNum < fnum {
// Set zero field value.
Expand Down Expand Up @@ -842,8 +872,10 @@ func (cdc *Codec) decodeReflectBinaryStruct(bz []byte, info *TypeInfo, rv reflec
}

// Consume any remaining fields.
var _n, fnum = 0, uint32(0)
var typ3 Typ3
var (
fnum uint32
typ3 Typ3
)
for len(bz) > 0 {
fnum, typ3, _n, err = decodeFieldNumberAndTyp3(bz)
if slide(&bz, &n, _n) && err != nil {
Expand All @@ -862,7 +894,7 @@ func (cdc *Codec) decodeReflectBinaryStruct(bz []byte, info *TypeInfo, rv reflec
}
}
}
return
return n, err
}

//----------------------------------------
Expand Down Expand Up @@ -927,8 +959,7 @@ func DecodeDisambPrefixBytes(bz []byte) (db DisambBytes, hasDb bool, pb PrefixBy
func decodeFieldNumberAndTyp3(bz []byte) (num uint32, typ Typ3, n int, err error) {

// Read uvarint value.
var value64 = uint64(0)
value64, n, err = DecodeUvarint(bz)
value64, n, err := DecodeUvarint(bz)
if err != nil {
return
}
Expand Down
Loading