Skip to content

Commit

Permalink
Lint Galore (#281)
Browse files Browse the repository at this point in the history
* Lint Galore

- added a few linters

ref #258

Signed-off-by: Marko Baricevic <[email protected]>

* Apply suggestions from code review

Co-Authored-By: Anton Kaliaev <[email protected]>

* ci fix
  • Loading branch information
tac0turtle authored Aug 16, 2019
1 parent b2c4ad6 commit 9dc1454
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 97 deletions.
9 changes: 0 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,10 @@ linters:
enable-all: true
disable:
- gocyclo
- gosimple
- deadcode
- structcheck
- maligned
- dupl
- ineffassign
- interfacer
- unconvert
- goconst
- unparam
- nakedret
- lll
- gochecknoglobals
- gochecknoinits
- scopelint
- stylecheck
29 changes: 22 additions & 7 deletions amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ func (cdc *Codec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) erro
// Like UnmarshalBinaryBare, but will first read the byte-length prefix.
// UnmarshalBinaryLengthPrefixedReader will panic if ptr is a nil-pointer.
// If maxSize is 0, there is no limit (not recommended).
func (cdc *Codec) UnmarshalBinaryLengthPrefixedReader(r io.Reader, ptr interface{}, maxSize int64) (n int64, err error) {
func (cdc *Codec) UnmarshalBinaryLengthPrefixedReader(r io.Reader, ptr interface{},
maxSize int64) (n int64, err error) {
if maxSize < 0 {
panic("maxSize cannot be negative.")
}
Expand All @@ -302,7 +303,10 @@ func (cdc *Codec) UnmarshalBinaryLengthPrefixedReader(r io.Reader, ptr interface
break
}
if n >= maxSize {
err = errors.Errorf("read overflow, maxSize is %v but uvarint(length-prefix) is itself greater than maxSize", maxSize)
err = errors.Errorf(
"read overflow, maxSize is %v but uvarint(length-prefix) is itself greater than maxSize",
maxSize,
)
}
}
u64, _ := binary.Uvarint(buf[:])
Expand All @@ -315,17 +319,22 @@ func (cdc *Codec) UnmarshalBinaryLengthPrefixedReader(r io.Reader, ptr interface
return
}
if (maxSize - n) < int64(u64) {
err = errors.Errorf("read overflow, maxSize is %v but this length-prefixed amino binary object is %v+%v bytes", maxSize, n, u64)
err = errors.Errorf(
"read overflow, maxSize is %v but this length-prefixed amino binary object is %v+%v bytes",
maxSize, n, u64,
)
return
}
}
l = int64(u64)
if l < 0 {
_ = errors.Errorf("read overflow, this implementation can't read this because, why would anyone have this much data? Hello from 2018")
_ = errors.Errorf(
"read overflow, this implementation can't read this because, why would anyone have this much data? Hello from 2018",
)
}

// Read that many bytes.
var bz = make([]byte, l, l)
var bz = make([]byte, l)
_, err = io.ReadFull(r, bz)
if err != nil {
return
Expand Down Expand Up @@ -364,9 +373,15 @@ func (cdc *Codec) UnmarshalBinaryBare(bz []byte, ptr interface{}) error {
// TODO: https://github.com/tendermint/go-amino/issues/267
pb := info.Prefix.Bytes()
if len(bz) < 4 {
return fmt.Errorf("unmarshalBinaryBare expected to read prefix bytes %X (since it is registered concrete) but got %X", pb, bz)
return fmt.Errorf(
"unmarshalBinaryBare expected to read prefix bytes %X (since it is registered concrete) but got %X",
pb, bz,
)
} else if !bytes.Equal(bz[:4], pb) {
return fmt.Errorf("unmarshalBinaryBare expected to read prefix bytes %X (since it is registered concrete) but got %X", pb, bz[:4])
return fmt.Errorf(
"unmarshalBinaryBare expected to read prefix bytes %X (since it is registered concrete) but got %X",
pb, bz[:4],
)
}
bz = bz[4:]
}
Expand Down
28 changes: 17 additions & 11 deletions binary-decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const (
// function calls decodeReflectBinary*, and generally those functions should
// only call this one, for the prefix bytes are consumed here when present.
// CONTRACT: rv.CanAddr() is true.
func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (n int, err error) {
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 @@ -223,7 +224,7 @@ func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo, rv reflect.Valu
if slide(&bz, &n, _n) && err != nil {
return
}
rv.SetUint(uint64(num))
rv.SetUint(num)
}
return

Expand Down Expand Up @@ -308,7 +309,8 @@ func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo, rv reflect.Valu
}

// CONTRACT: rv.CanAddr() is true.
func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (n int, err error) {
func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (n int, err error) {
if !rv.CanAddr() {
panic("rv not addressable")
}
Expand Down Expand Up @@ -406,7 +408,8 @@ func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv re
}

// CONTRACT: rv.CanAddr() is true.
func (cdc *Codec) decodeReflectBinaryByteArray(bz []byte, info *TypeInfo, rv reflect.Value, fopts FieldOptions) (n int, err error) {
func (cdc *Codec) decodeReflectBinaryByteArray(bz []byte, info *TypeInfo, rv reflect.Value,
fopts FieldOptions) (n int, err error) {
if !rv.CanAddr() {
panic("rv not addressable")
}
Expand All @@ -432,7 +435,7 @@ func (cdc *Codec) decodeReflectBinaryByteArray(bz []byte, info *TypeInfo, rv ref
return
}
if len(byteslice) != length {
err = fmt.Errorf("Mismatched byte array length: Expected %v, got %v",
err = fmt.Errorf("mismatched byte array length: Expected %v, got %v",
length, len(byteslice))
return
}
Expand All @@ -444,7 +447,8 @@ func (cdc *Codec) decodeReflectBinaryByteArray(bz []byte, info *TypeInfo, rv ref

// CONTRACT: rv.CanAddr() is true.
// NOTE: Keep the code structure similar to decodeReflectBinarySlice.
func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (n int, err error) {
func (cdc *Codec) decodeReflectBinaryArray(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 @@ -567,7 +571,8 @@ func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect
}

// CONTRACT: rv.CanAddr() is true.
func (cdc *Codec) decodeReflectBinaryByteSlice(bz []byte, info *TypeInfo, rv reflect.Value, fopts FieldOptions) (n int, err error) {
func (cdc *Codec) decodeReflectBinaryByteSlice(bz []byte, info *TypeInfo, rv reflect.Value,
fopts FieldOptions) (n int, err error) {
if !rv.CanAddr() {
panic("rv not addressable")
}
Expand Down Expand Up @@ -605,7 +610,8 @@ func (cdc *Codec) decodeReflectBinaryByteSlice(bz []byte, info *TypeInfo, rv ref

// CONTRACT: rv.CanAddr() is true.
// NOTE: Keep the code structure similar to decodeReflectBinaryArray.
func (cdc *Codec) decodeReflectBinarySlice(bz []byte, info *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (n int, err error) {
func (cdc *Codec) decodeReflectBinarySlice(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 @@ -727,7 +733,8 @@ func (cdc *Codec) decodeReflectBinarySlice(bz []byte, info *TypeInfo, rv reflect
}

// CONTRACT: rv.CanAddr() is true.
func (cdc *Codec) decodeReflectBinaryStruct(bz []byte, info *TypeInfo, rv reflect.Value, _ FieldOptions, bare bool) (n int, err error) {
func (cdc *Codec) decodeReflectBinaryStruct(bz []byte, info *TypeInfo, rv reflect.Value,
_ FieldOptions, bare bool) (n int, err error) {
if !rv.CanAddr() {
panic("rv not addressable")
}
Expand Down Expand Up @@ -930,8 +937,7 @@ func decodeFieldNumberAndTyp3(bz []byte) (num uint32, typ Typ3, n int, err error
typ = Typ3(value64 & 0x07)

// Decode num.
var num64 uint64
num64 = value64 >> 3
num64 := value64 >> 3
if num64 > (1<<29 - 1) {
err = fmt.Errorf("invalid field num %v", num64)
return
Expand Down
18 changes: 12 additions & 6 deletions binary-encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
// The following contracts apply to all similar encode methods.
// CONTRACT: rv is not a pointer
// CONTRACT: rv is valid.
func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (err error) {
func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if rv.Kind() == reflect.Ptr {
panic("not allowed to be called with a reflect.Ptr")
}
Expand Down Expand Up @@ -164,7 +165,8 @@ func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Va
return
}

func (cdc *Codec) encodeReflectBinaryInterface(w io.Writer, iinfo *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (err error) {
func (cdc *Codec) encodeReflectBinaryInterface(w io.Writer, iinfo *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryInterface")
defer func() {
Expand Down Expand Up @@ -240,7 +242,8 @@ func (cdc *Codec) encodeReflectBinaryInterface(w io.Writer, iinfo *TypeInfo, rv
return
}

func (cdc *Codec) encodeReflectBinaryByteArray(w io.Writer, info *TypeInfo, rv reflect.Value, fopts FieldOptions) (err error) {
func (cdc *Codec) encodeReflectBinaryByteArray(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions) (err error) {
ert := info.Type.Elem()
if ert.Kind() != reflect.Uint8 {
panic("should not happen")
Expand All @@ -261,7 +264,8 @@ func (cdc *Codec) encodeReflectBinaryByteArray(w io.Writer, info *TypeInfo, rv r
return
}

func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (err error) {
func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryList")
defer func() {
Expand Down Expand Up @@ -350,7 +354,8 @@ func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflec
}

// CONTRACT: info.Type.Elem().Kind() == reflect.Uint8
func (cdc *Codec) encodeReflectBinaryByteSlice(w io.Writer, info *TypeInfo, rv reflect.Value, fopts FieldOptions) (err error) {
func (cdc *Codec) encodeReflectBinaryByteSlice(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryByteSlice")
defer func() {
Expand All @@ -368,7 +373,8 @@ func (cdc *Codec) encodeReflectBinaryByteSlice(w io.Writer, info *TypeInfo, rv r
return
}

func (cdc *Codec) encodeReflectBinaryStruct(w io.Writer, info *TypeInfo, rv reflect.Value, fopts FieldOptions, bare bool) (err error) {
func (cdc *Codec) encodeReflectBinaryStruct(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryBinaryStruct")
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/aminoscan/aminoscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func scanVarint(bz []byte, indent string) (s string, n int, err error) {
}
// If neither work, return error.
if !okI64 && !okU64 {
err = fmt.Errorf("Invalid (u)varint")
err = fmt.Errorf("invalid (u)varint")
return
}
// s is the same either way.
Expand Down
12 changes: 5 additions & 7 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (cdc *Codec) getTypeInfoWlock(rt reflect.Type) (info *TypeInfo, err error)
info, ok := cdc.typeInfos[rt]
if !ok {
if rt.Kind() == reflect.Interface {
err = fmt.Errorf("Unregistered interface %v", rt)
err = fmt.Errorf("unregistered interface %v", rt)
cdc.mtx.Unlock()
return
}
Expand Down Expand Up @@ -599,7 +599,8 @@ func (cdc *Codec) newTypeInfoFromInterfaceType(rt reflect.Type, iopts *Interface
return info
}

func (cdc *Codec) newTypeInfoFromRegisteredConcreteType(rt reflect.Type, pointerPreferred bool, name string, copts *ConcreteOptions) *TypeInfo {
func (cdc *Codec) newTypeInfoFromRegisteredConcreteType(rt reflect.Type, pointerPreferred bool,
name string, copts *ConcreteOptions) *TypeInfo {
if rt.Kind() == reflect.Interface ||
rt.Kind() == reflect.Ptr {
panic(fmt.Sprintf("expected non-interface non-pointer concrete type, got %v", rt))
Expand Down Expand Up @@ -733,11 +734,8 @@ func isExported(field reflect.StructField) bool {
}
// TODO: JAE: I'm not sure that the unicode spec
// is the correct spec to use, so this might be wrong.
if !unicode.IsUpper(first) {
return false
}
// Ok, it's exported.
return true

return unicode.IsUpper(first)
}

func nameToDisamb(name string) (db DisambBytes) {
Expand Down
1 change: 0 additions & 1 deletion deep_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func _deepCopy(src, dst reflect.Value) {
panic(fmt.Sprintf("unsupported type %v", src.Kind()))
}

return
}

//----------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions deep_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type DCFoo4 struct{ a string }
func newDCFoo4(a string) *DCFoo4 { return &DCFoo4{a: a} }
func (dcf *DCFoo4) DeepCopy() *DCFoo4 { return &DCFoo4{"good"} }
func (dcf DCFoo4) MarshalAmino() (string, error) { return dcf.a, nil }
func (dcf *DCFoo4) UnmarshalAmino(s string) error { dcf.a = string(s); return nil } // mismatch type
func (dcf *DCFoo4) UnmarshalAmino(s string) error { dcf.a = s; return nil } // mismatch type

func TestDeepCopyFoo4(t *testing.T) {
dcf1 := newDCFoo4("foobar")
Expand All @@ -62,7 +62,7 @@ type DCFoo5 struct{ a string }
func newDCFoo5(a string) *DCFoo5 { return &DCFoo5{a: a} }
func (dcf DCFoo5) DeepCopy() DCFoo5 { return DCFoo5{"good"} }
func (dcf DCFoo5) MarshalAmino() (string, error) { return dcf.a, nil }
func (dcf *DCFoo5) UnmarshalAmino(s string) error { dcf.a = string(s); return nil } // mismatch type
func (dcf *DCFoo5) UnmarshalAmino(s string) error { dcf.a = s; return nil } // mismatch type

func TestDeepCopyFoo5(t *testing.T) {
dcf1 := newDCFoo5("foobar")
Expand Down Expand Up @@ -96,7 +96,7 @@ type DCFoo8 struct{ a string }

func newDCFoo8(a string) *DCFoo8 { return &DCFoo8{a: a} }
func (dcf DCFoo8) MarshalAmino() (string, error) { return "", errors.New("uh oh") } // error
func (dcf *DCFoo8) UnmarshalAmino(s string) error { dcf.a = string(s); return nil }
func (dcf *DCFoo8) UnmarshalAmino(s string) error { dcf.a = s; return nil }

func TestDeepCopyFoo8(t *testing.T) {
dcf1 := newDCFoo8("foobar")
Expand Down
56 changes: 12 additions & 44 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,23 @@ import (
)

func TestUvarintSize(t *testing.T) {
tests := []struct {
testCases := []struct {
name string
u uint64
want int
}{
{
"0 bit",
0,
1,
},
{
"1 bit",
1 << 0,
1,
},
{
"6 bits",
1 << 5,
1,
},
{
"7 bits",
1 << 6,
1,
},
{
"8 bits",
1 << 7,
2,
},
{
"62 bits",
1 << 61,
9,
},
{
"63 bits",
1 << 62,
9,
},
{
"64 bits",
1 << 63,
10,
},
{"0 bit", 0, 1},
{"1 bit", 1 << 0, 1},
{"6 bits", 1 << 5, 1},
{"7 bits", 1 << 6, 1},
{"8 bits", 1 << 7, 2},
{"62 bits", 1 << 61, 9},
{"63 bits", 1 << 62, 9},
{"64 bits", 1 << 63, 10},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, UvarintSize(tt.u), "failed on tc %d", i)
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.want, UvarintSize(tc.u), "#%d", i) // nolint:scopelint
})
}
}
Loading

0 comments on commit 9dc1454

Please sign in to comment.