Skip to content

Commit

Permalink
Fix Empty slice literal used to declare a variable
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 23, 2023
1 parent 68c49ae commit 212b186
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 53 deletions.
4 changes: 1 addition & 3 deletions dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func TestChildStateModule_GetKeys(t *testing.T) {
Hash: &common.Hash{},
},
},
exp: []string{},
expErr: errors.New("GetStorageChild error"),
},
{
Expand All @@ -138,7 +137,6 @@ func TestChildStateModule_GetKeys(t *testing.T) {
Key: []byte(":child_storage_key"),
},
},
exp: []string{},
expErr: errors.New("GetStateRootFromBlock error"),
},
}
Expand All @@ -148,7 +146,7 @@ func TestChildStateModule_GetKeys(t *testing.T) {
storageAPI: tt.fields.storageAPI,
blockAPI: tt.fields.blockAPI,
}
res := []string{}
var res []string
err := cs.GetKeys(tt.args.in0, tt.args.req, &res)
if tt.expErr != nil {
assert.EqualError(t, err, tt.expErr.Error())
Expand Down
3 changes: 1 addition & 2 deletions dot/rpc/modules/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,13 @@ func TestSystemModule_LocalListenAddresses(t *testing.T) {
args: args{
req: &EmptyRequest{},
},
exp: []string{},
expErr: errors.New("multiaddress list is empty"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := tt.sysModule
res := []string{}
var res []string
err := sm.LocalListenAddresses(tt.args.r, tt.args.req, &res)
if tt.expErr != nil {
assert.EqualError(t, err, tt.expErr.Error())
Expand Down
14 changes: 6 additions & 8 deletions dot/types/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ func NewBody(e []Extrinsic) *Body {

// NewBodyFromBytes returns a Body from a SCALE encoded byte array.
func NewBodyFromBytes(b []byte) (*Body, error) {
exts := [][]byte{}

if len(b) == 0 {
return NewBody([]Extrinsic{}), nil
}

var exts [][]byte
err := scale.Unmarshal(b, &exts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -59,16 +58,15 @@ func NewBodyFromEncodedBytes(exts [][]byte) (*Body, error) {

// NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded
// 0x-prefixed strings.
func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) {
exts := []Extrinsic{}
for _, s := range ss {
b, err := common.HexToBytes(s)
func NewBodyFromExtrinsicStrings(ss []string) (body *Body, err error) {
exts := make([]Extrinsic, len(ss))
for i, s := range ss {
exts[i], err = common.HexToBytes(s)
if errors.Is(err, common.ErrNoPrefix) {
b = []byte(s)
exts[i] = []byte(s)
} else if err != nil {
return nil, err
}
exts = append(exts, b)
}

return NewBody(exts), nil
Expand Down
10 changes: 3 additions & 7 deletions dot/types/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package types

import (
"fmt"
"testing"

"github.com/ChainSafe/gossamer/lib/common"
Expand Down Expand Up @@ -48,14 +47,11 @@ func TestBodyFromEncodedBytes(t *testing.T) {
}

func TestBodyFromExtrinsicStrings(t *testing.T) {
extStrings := []string{}

for _, ext := range exts {
extStrings = append(extStrings, common.BytesToHex(ext))
extStrings := make([]string, len(exts))
for i := range exts {
extStrings[i] = common.BytesToHex(exts[i])
}

fmt.Println(extStrings)

bodyFromByteExtrinsics := NewBody(exts)
bodyFromStringExtrinsics, err := NewBodyFromExtrinsicStrings(extStrings)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dot/types/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func EncodeGrandpaVoters(voters GrandpaVoters) ([]byte, error) {

// DecodeGrandpaVoters returns a decoded GrandpaVoters
func DecodeGrandpaVoters(in []byte) (GrandpaVoters, error) {
dec := []voter{}
var dec []voter
err := scale.Unmarshal(in, &dec)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion dot/types/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestGrandpaAuthoritiesRawToAuthorities(t *testing.T) {
require.NoError(t, err)
require.Equal(t, exp, enc)

dec := []GrandpaAuthoritiesRaw{}
var dec []GrandpaAuthoritiesRaw
err = scale.Unmarshal(enc, &dec)
require.NoError(t, err)
require.Equal(t, auths, dec)
Expand Down
2 changes: 1 addition & 1 deletion dot/types/inherents.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (d *InherentData) Encode() ([]byte, error) {
return nil, err
}

keys := [][8]byte{}
keys := make([][8]byte, 0, len(d.Data))
for key := range d.Data {
keys = append(keys, key)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/blocktree/leaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (lm *leafMap) nodes() []*node {
lm.RLock()
defer lm.RUnlock()

nodes := []*node{}
var nodes []*node

lm.smap.Range(func(h, n interface{}) bool {
node := n.(*node)
Expand Down
2 changes: 1 addition & 1 deletion lib/blocktree/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNode_GetLeaves(t *testing.T) {
testNode := bt.getNode(branches[0].hash).children[0]
leaves := testNode.getLeaves(nil)

expected := []*node{}
var expected []*node
for _, lf := range bt.leaves.toMap() {
if lf.isDescendantOf(testNode) {
expected = append(expected, lf)
Expand Down
15 changes: 7 additions & 8 deletions lib/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import (
var ErrNoPrefix = errors.New("could not byteify non 0x prefixed string")

// StringToInts turns a string consisting of ints separated by commas into an int array
func StringToInts(in string) ([]int, error) {
func StringToInts(in string) (res []int, err error) {
intstrs := strings.Split(in, ",")
res := []int{}
for _, intstr := range intstrs {
i, err := strconv.Atoi(intstr)
if err != nil {
Expand All @@ -32,18 +31,18 @@ func StringToInts(in string) ([]int, error) {

// StringArrayToBytes turns an array of strings into an array of byte arrays
func StringArrayToBytes(in []string) [][]byte {
b := [][]byte{}
for _, str := range in {
b = append(b, []byte(str))
b := make([][]byte, len(in))
for i := range in {
b[i] = []byte(in[i])
}
return b
}

// BytesToStringArray turns an array of byte arrays into an array strings
func BytesToStringArray(in [][]byte) []string {
strs := []string{}
for _, b := range in {
strs = append(strs, string(b))
strs := make([]string, len(in))
for i := range in {
strs[i] = string(in[i])
}
return strs
}
Expand Down
6 changes: 3 additions & 3 deletions lib/common/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestBlake2b218_EmptyHash(t *testing.T) {
// test case from https://github.com/noot/blake2b_test which uses the blake2-rfp rust crate
// also see https://github.com/paritytech/substrate/blob/master/core/primitives/src/hashing.rs
in := []byte{}
var in []byte
h, err := common.Blake2b128(in)
require.NoError(t, err)

Expand Down Expand Up @@ -42,7 +42,7 @@ func Test_MustBlake2b8(t *testing.T) {
func TestBlake2bHash_EmptyHash(t *testing.T) {
// test case from https://github.com/noot/blake2b_test which uses the blake2-rfp rust crate
// also see https://github.com/paritytech/substrate/blob/master/core/primitives/src/hashing.rs
in := []byte{}
var in []byte
h, err := common.Blake2bHash(in)
require.NoError(t, err)

Expand All @@ -53,7 +53,7 @@ func TestBlake2bHash_EmptyHash(t *testing.T) {

func TestKeccak256_EmptyHash(t *testing.T) {
// test case from https://github.com/debris/tiny-keccak/blob/master/tests/keccak.rs#L4
in := []byte{}
var in []byte
h, err := common.Keccak256(in)
require.NoError(t, err)

Expand Down
3 changes: 1 addition & 2 deletions lib/keystore/basic_keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func (ks *BasicKeystore) GetKeypairFromAddress(pub common.Address) KeyPair {
}

// PublicKeys returns all public keys in the keystore
func (ks *BasicKeystore) PublicKeys() []crypto.PublicKey {
srkeys := []crypto.PublicKey{}
func (ks *BasicKeystore) PublicKeys() (srkeys []crypto.PublicKey) {
if ks.keys == nil {
return srkeys
}
Expand Down
2 changes: 1 addition & 1 deletion lib/keystore/basic_keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBasicKeystore(t *testing.T) {
func TestBasicKeystore_PublicKeys(t *testing.T) {
ks := NewBasicKeystore("test", crypto.Sr25519Type)

expectedPubkeys := []crypto.PublicKey{}
var expectedPubkeys []crypto.PublicKey
numKps := 12

for i := 0; i < numKps; i++ {
Expand Down
12 changes: 4 additions & 8 deletions lib/keystore/generic_keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ func (ks *GenericKeystore) GetKeypairFromAddress(pub common.Address) KeyPair {
}

// PublicKeys returns all public keys in the keystore
func (ks *GenericKeystore) PublicKeys() []crypto.PublicKey {
srkeys := []crypto.PublicKey{}
func (ks *GenericKeystore) PublicKeys() (srkeys []crypto.PublicKey) {
if ks.keys == nil {
return srkeys
}
Expand Down Expand Up @@ -109,8 +108,7 @@ func (ks *GenericKeystore) NumEd25519Keys() int {
}

// Ed25519PublicKeys keys
func (ks *GenericKeystore) Ed25519PublicKeys() []crypto.PublicKey {
edkeys := []crypto.PublicKey{}
func (ks *GenericKeystore) Ed25519PublicKeys() (edkeys []crypto.PublicKey) {
if ks.keys == nil {
return edkeys
}
Expand Down Expand Up @@ -138,8 +136,7 @@ func (ks *GenericKeystore) Ed25519Keypairs() (edkeys []KeyPair) {
}

// Sr25519PublicKeys PublicKey
func (ks *GenericKeystore) Sr25519PublicKeys() []crypto.PublicKey {
srkeys := []crypto.PublicKey{}
func (ks *GenericKeystore) Sr25519PublicKeys() (srkeys []crypto.PublicKey) {
if ks.keys == nil {
return srkeys
}
Expand Down Expand Up @@ -167,8 +164,7 @@ func (ks *GenericKeystore) Sr25519Keypairs() (srkeys []KeyPair) {
}

// Secp256k1PublicKeys PublicKey
func (ks *GenericKeystore) Secp256k1PublicKeys() []crypto.PublicKey {
sckeys := []crypto.PublicKey{}
func (ks *GenericKeystore) Secp256k1PublicKeys() (sckeys []crypto.PublicKey) {
if ks.keys == nil {
return sckeys
}
Expand Down
6 changes: 3 additions & 3 deletions lib/keystore/generic_keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestGetSr25519PublicKeys(t *testing.T) {
ks := NewGenericKeystore("test")

expectedPubkeys := []crypto.PublicKey{}
var expectedPubkeys []crypto.PublicKey
numKps := 12

for i := 0; i < numKps; i++ {
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestGetSr25519PublicKeys(t *testing.T) {
func TestGetEd25519PublicKeys(t *testing.T) {
ks := NewGenericKeystore("test")

expectedPubkeys := []crypto.PublicKey{}
var expectedPubkeys []crypto.PublicKey
numKps := 10

for i := 0; i < numKps; i++ {
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestGetEd25519PublicKeys(t *testing.T) {
func TestGetSecp256k1PublicKeys(t *testing.T) {
ks := NewGenericKeystore("test")

expectedPubkeys := []crypto.PublicKey{}
var expectedPubkeys []crypto.PublicKey
numKps := 10

for i := 0; i < numKps; i++ {
Expand Down
2 changes: 1 addition & 1 deletion lib/keystore/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestImportKey(t *testing.T) {
func TestListKeys(t *testing.T) {
testdir := t.TempDir()

expected := []string{}
var expected []string

for i := 0; i < 5; i++ {
var err error
Expand Down
3 changes: 1 addition & 2 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1865,8 +1865,7 @@ func Test_ext_trie_blake2_256_verify_proof_version_1(t *testing.T) {
hashEnc, err := scale.Marshal(testcase.root)
require.NoError(t, err)

args := []byte{}
args = append(args, hashEnc...)
args := hashEnc

encProof, err := scale.Marshal(testcase.proof)
require.NoError(t, err)
Expand Down

0 comments on commit 212b186

Please sign in to comment.