Skip to content

Commit 4ea0a70

Browse files
authored
fix(ci): fix all Deepsource issues (#3046)
1 parent 2d5ead1 commit 4ea0a70

31 files changed

+91
-107
lines changed

dot/rpc/json2/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (c *CodecRequest) WriteError(w http.ResponseWriter, _ int, err error) {
219219
c.writeServerResponse(w, res)
220220
}
221221

222-
func (c CodecRequest) tryToMapIfNotAnErrorAlready(err error) error {
222+
func (c *CodecRequest) tryToMapIfNotAnErrorAlready(err error) error {
223223
if _, ok := err.(*json2.Error); ok || c.errorMapper == nil {
224224
return err
225225
}

dot/rpc/modules/childstate_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ func TestChildStateModule_GetKeys(t *testing.T) {
124124
Hash: &common.Hash{},
125125
},
126126
},
127-
exp: []string{},
128127
expErr: errors.New("GetStorageChild error"),
129128
},
130129
{
@@ -138,7 +137,6 @@ func TestChildStateModule_GetKeys(t *testing.T) {
138137
Key: []byte(":child_storage_key"),
139138
},
140139
},
141-
exp: []string{},
142140
expErr: errors.New("GetStateRootFromBlock error"),
143141
},
144142
}
@@ -148,7 +146,7 @@ func TestChildStateModule_GetKeys(t *testing.T) {
148146
storageAPI: tt.fields.storageAPI,
149147
blockAPI: tt.fields.blockAPI,
150148
}
151-
res := []string{}
149+
var res []string
152150
err := cs.GetKeys(tt.args.in0, tt.args.req, &res)
153151
if tt.expErr != nil {
154152
assert.EqualError(t, err, tt.expErr.Error())

dot/rpc/modules/system_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,13 @@ func TestSystemModule_LocalListenAddresses(t *testing.T) {
446446
args: args{
447447
req: &EmptyRequest{},
448448
},
449-
exp: []string{},
450449
expErr: errors.New("multiaddress list is empty"),
451450
},
452451
}
453452
for _, tt := range tests {
454453
t.Run(tt.name, func(t *testing.T) {
455454
sm := tt.sysModule
456-
res := []string{}
455+
var res []string
457456
err := sm.LocalListenAddresses(tt.args.r, tt.args.req, &res)
458457
if tt.expErr != nil {
459458
assert.EqualError(t, err, tt.expErr.Error())

dot/types/babe_digest.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func NewBabePrimaryPreDigest(authorityIndex uint32,
5757
}
5858

5959
// ToPreRuntimeDigest returns the BabePrimaryPreDigest as a PreRuntimeDigest
60-
func (d *BabePrimaryPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
61-
return toPreRuntimeDigest(*d)
60+
func (d BabePrimaryPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
61+
return toPreRuntimeDigest(d)
6262
}
6363

6464
// Index returns VDT index
@@ -85,8 +85,8 @@ func NewBabeSecondaryPlainPreDigest(authorityIndex uint32, slotNumber uint64) *B
8585
}
8686

8787
// ToPreRuntimeDigest returns the BabeSecondaryPlainPreDigest as a PreRuntimeDigest
88-
func (d *BabeSecondaryPlainPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
89-
return toPreRuntimeDigest(*d)
88+
func (d BabeSecondaryPlainPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
89+
return toPreRuntimeDigest(d)
9090
}
9191

9292
// Index returns VDT index
@@ -118,8 +118,8 @@ func NewBabeSecondaryVRFPreDigest(authorityIndex uint32,
118118
}
119119

120120
// ToPreRuntimeDigest returns the BabeSecondaryVRFPreDigest as a PreRuntimeDigest
121-
func (d *BabeSecondaryVRFPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
122-
return toPreRuntimeDigest(*d)
121+
func (d BabeSecondaryVRFPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
122+
return toPreRuntimeDigest(d)
123123
}
124124

125125
// Index returns VDT index

dot/types/body.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ func NewBody(e []Extrinsic) *Body {
2424

2525
// NewBodyFromBytes returns a Body from a SCALE encoded byte array.
2626
func NewBodyFromBytes(b []byte) (*Body, error) {
27-
exts := [][]byte{}
28-
2927
if len(b) == 0 {
3028
return NewBody([]Extrinsic{}), nil
3129
}
3230

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

6059
// NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded
6160
// 0x-prefixed strings.
62-
func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) {
63-
exts := []Extrinsic{}
64-
for _, s := range ss {
65-
b, err := common.HexToBytes(s)
61+
func NewBodyFromExtrinsicStrings(ss []string) (body *Body, err error) {
62+
exts := make([]Extrinsic, len(ss))
63+
for i, s := range ss {
64+
exts[i], err = common.HexToBytes(s)
6665
if errors.Is(err, common.ErrNoPrefix) {
67-
b = []byte(s)
66+
exts[i] = []byte(s)
6867
} else if err != nil {
6968
return nil, err
7069
}
71-
exts = append(exts, b)
7270
}
7371

7472
return NewBody(exts), nil

dot/types/body_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package types
55

66
import (
7-
"fmt"
87
"testing"
98

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

5049
func TestBodyFromExtrinsicStrings(t *testing.T) {
51-
extStrings := []string{}
52-
53-
for _, ext := range exts {
54-
extStrings = append(extStrings, common.BytesToHex(ext))
50+
extStrings := make([]string, len(exts))
51+
for i := range exts {
52+
extStrings[i] = common.BytesToHex(exts[i])
5553
}
5654

57-
fmt.Println(extStrings)
58-
5955
bodyFromByteExtrinsics := NewBody(exts)
6056
bodyFromStringExtrinsics, err := NewBodyFromExtrinsicStrings(extStrings)
6157
require.NoError(t, err)

dot/types/consensus_digest.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ type NextEpochData struct {
9595
}
9696

9797
// Index returns VDT index
98-
func (NextEpochData) Index() uint { return 1 }
98+
func (NextEpochData) Index() uint { return 1 } //skipcq: GO-W1029
9999

100-
func (d NextEpochData) String() string {
100+
func (d NextEpochData) String() string { //skipcq: GO-W1029
101101
return fmt.Sprintf("NextEpochData Authorities=%v Randomness=%v", d.Authorities, d.Randomness)
102102
}
103103

104104
// ToEpochData returns the NextEpochData as EpochData
105-
func (d *NextEpochData) ToEpochData() (*EpochData, error) {
105+
func (d *NextEpochData) ToEpochData() (*EpochData, error) { //skipcq: GO-W1029
106106
auths, err := BABEAuthorityRawToAuthority(d.Authorities)
107107
if err != nil {
108108
return nil, err
@@ -135,15 +135,15 @@ type NextConfigData struct {
135135
}
136136

137137
// Index returns VDT index
138-
func (NextConfigData) Index() uint { return 3 }
138+
func (NextConfigData) Index() uint { return 3 } //skipcq: GO-W1029
139139

140-
func (d NextConfigData) String() string {
140+
func (d NextConfigData) String() string { //skipcq: GO-W1029
141141
return fmt.Sprintf("NextConfigData{C1=%d, C2=%d, SecondarySlots=%d}",
142142
d.C1, d.C2, d.SecondarySlots)
143143
}
144144

145145
// ToConfigData returns the NextConfigData as ConfigData
146-
func (d *NextConfigData) ToConfigData() *ConfigData {
146+
func (d *NextConfigData) ToConfigData() *ConfigData { //skipcq: GO-W1029
147147
return &ConfigData{
148148
C1: d.C1,
149149
C2: d.C2,

dot/types/grandpa.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func EncodeGrandpaVoters(voters GrandpaVoters) ([]byte, error) {
149149

150150
// DecodeGrandpaVoters returns a decoded GrandpaVoters
151151
func DecodeGrandpaVoters(in []byte) (GrandpaVoters, error) {
152-
dec := []voter{}
152+
var dec []voter
153153
err := scale.Unmarshal(in, &dec)
154154
if err != nil {
155155
return nil, err

dot/types/grandpa_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestGrandpaAuthoritiesRawToAuthorities(t *testing.T) {
6868
require.NoError(t, err)
6969
require.Equal(t, exp, enc)
7070

71-
dec := []GrandpaAuthoritiesRaw{}
71+
var dec []GrandpaAuthoritiesRaw
7272
err = scale.Unmarshal(enc, &dec)
7373
require.NoError(t, err)
7474
require.Equal(t, auths, dec)

dot/types/inherents.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (d *InherentData) Encode() ([]byte, error) {
9898
return nil, err
9999
}
100100

101-
keys := [][8]byte{}
101+
keys := make([][8]byte, 0, len(d.Data))
102102
for key := range d.Data {
103103
keys = append(keys, key)
104104
}

internal/pprof/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (s *Settings) setDefaults() {
2424
}
2525
}
2626

27-
func (s Settings) String() string {
27+
func (s *Settings) String() string {
2828
return fmt.Sprintf(
2929
"listening on %s and setting block profile rate to %d, mutex profile rate to %d",
3030
s.ListeningAddress, s.BlockProfileRate, s.MutexProfileRate)

internal/trie/node/copy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type CopySettings struct {
5353
// Copy deep copies the node.
5454
// Setting copyChildren to true will deep copy
5555
// children as well.
56-
func (n *Node) Copy(settings CopySettings) *Node { //skipcq: GO-W1029
56+
func (n *Node) Copy(settings CopySettings) *Node {
5757
cpy := &Node{
5858
Dirty: n.Dirty,
5959
Generation: n.Generation,

internal/trie/node/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (n *Node) String() string {
5252
}
5353

5454
// StringNode returns a gotree compatible node for String methods.
55-
func (n Node) StringNode() (stringNode *gotree.Node) {
55+
func (n *Node) StringNode() (stringNode *gotree.Node) {
5656
caser := cases.Title(language.BritishEnglish)
5757
stringNode = gotree.New(caser.String(n.Kind().String()))
5858
stringNode.Appendf("Generation: %d", n.Generation)

internal/trie/node/subvalue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "bytes"
88
// StorageValueEqual returns true if the node storage value is equal to the
99
// storage value given as argument. In particular, it returns false
1010
// if one storage value is nil and the other storage value is the empty slice.
11-
func (n Node) StorageValueEqual(stoageValue []byte) (equal bool) {
11+
func (n *Node) StorageValueEqual(stoageValue []byte) (equal bool) {
1212
if len(stoageValue) == 0 && len(n.StorageValue) == 0 {
1313
return (stoageValue == nil && n.StorageValue == nil) ||
1414
(stoageValue != nil && n.StorageValue != nil)

lib/blocktree/leaves.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (lm *leafMap) nodes() []*node {
111111
lm.RLock()
112112
defer lm.RUnlock()
113113

114-
nodes := []*node{}
114+
var nodes []*node
115115

116116
lm.smap.Range(func(h, n interface{}) bool {
117117
node := n.(*node)

lib/blocktree/node_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestNode_GetLeaves(t *testing.T) {
2323
testNode := bt.getNode(branches[0].hash).children[0]
2424
leaves := testNode.getLeaves(nil)
2525

26-
expected := []*node{}
26+
var expected []*node
2727
for _, lf := range bt.leaves.toMap() {
2828
if lf.isDescendantOf(testNode) {
2929
expected = append(expected, lf)

lib/common/common.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import (
1717
var ErrNoPrefix = errors.New("could not byteify non 0x prefixed string")
1818

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

3332
// StringArrayToBytes turns an array of strings into an array of byte arrays
3433
func StringArrayToBytes(in []string) [][]byte {
35-
b := [][]byte{}
36-
for _, str := range in {
37-
b = append(b, []byte(str))
34+
b := make([][]byte, len(in))
35+
for i := range in {
36+
b[i] = []byte(in[i])
3837
}
3938
return b
4039
}
4140

4241
// BytesToStringArray turns an array of byte arrays into an array strings
4342
func BytesToStringArray(in [][]byte) []string {
44-
strs := []string{}
45-
for _, b := range in {
46-
strs = append(strs, string(b))
43+
strs := make([]string, len(in))
44+
for i := range in {
45+
strs[i] = string(in[i])
4746
}
4847
return strs
4948
}

lib/common/hash.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewHash(in []byte) (res Hash) {
3030
}
3131

3232
// ToBytes turns a hash to a byte array
33-
func (h Hash) ToBytes() []byte {
33+
func (h Hash) ToBytes() []byte { //skipcq: GO-W1029
3434
b := [32]byte(h)
3535
return b[:]
3636
}
@@ -49,24 +49,24 @@ func HashValidator(field reflect.Value) interface{} {
4949
}
5050

5151
// IsEmpty returns true if the hash is empty, false otherwise.
52-
func (h Hash) IsEmpty() bool {
52+
func (h Hash) IsEmpty() bool { //skipcq: GO-W1029
5353
return h == Hash{}
5454
}
5555

5656
// String returns the hex string for the hash
57-
func (h Hash) String() string {
57+
func (h Hash) String() string { //skipcq: GO-W1029
5858
return fmt.Sprintf("0x%x", h[:])
5959
}
6060

6161
// Short returns the first 4 bytes and the last 4 bytes of the hex string for the hash
62-
func (h Hash) Short() string {
62+
func (h Hash) Short() string { //skipcq: GO-W1029
6363
const nBytes = 4
6464
return fmt.Sprintf("0x%x...%x", h[:nBytes], h[len(h)-nBytes:])
6565
}
6666

6767
// SetBytes sets the hash to the value of b.
6868
// If b is larger than len(h), b will be cropped from the left.
69-
func (h *Hash) SetBytes(b []byte) {
69+
func (h *Hash) SetBytes(b []byte) { //skipcq: GO-W1029
7070
if len(b) > len(h) {
7171
b = b[len(b)-HashLength:]
7272
}
@@ -95,7 +95,7 @@ func BytesToHash(b []byte) Hash {
9595
}
9696

9797
// UnmarshalJSON converts hex data to hash
98-
func (h *Hash) UnmarshalJSON(data []byte) error {
98+
func (h *Hash) UnmarshalJSON(data []byte) error { //skipcq: GO-W1029
9999
trimmedData := strings.Trim(string(data), "\"")
100100
if len(trimmedData) < 2 {
101101
return errors.New("invalid hash format")
@@ -109,7 +109,7 @@ func (h *Hash) UnmarshalJSON(data []byte) error {
109109
}
110110

111111
// MarshalJSON converts hash to hex data
112-
func (h Hash) MarshalJSON() ([]byte, error) {
112+
func (h Hash) MarshalJSON() ([]byte, error) { //skipcq: GO-W1029
113113
return json.Marshal(h.String())
114114
}
115115

lib/common/hasher_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func TestBlake2b218_EmptyHash(t *testing.T) {
1515
// test case from https://github.com/noot/blake2b_test which uses the blake2-rfp rust crate
1616
// also see https://github.com/paritytech/substrate/blob/master/core/primitives/src/hashing.rs
17-
in := []byte{}
17+
var in []byte
1818
h, err := common.Blake2b128(in)
1919
require.NoError(t, err)
2020

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

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

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

lib/keystore/basic_keystore.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ func (ks *BasicKeystore) GetKeypairFromAddress(pub common.Address) KeyPair {
8282
}
8383

8484
// PublicKeys returns all public keys in the keystore
85-
func (ks *BasicKeystore) PublicKeys() []crypto.PublicKey {
86-
srkeys := []crypto.PublicKey{}
85+
func (ks *BasicKeystore) PublicKeys() (srkeys []crypto.PublicKey) {
8786
if ks.keys == nil {
8887
return srkeys
8988
}

0 commit comments

Comments
 (0)