Skip to content

Commit a69ce3b

Browse files
committed
Clean code and fix issues reported by staticcheck
Signed-off-by: Sergio Arroutbi <[email protected]>
1 parent 68e77ee commit a69ce3b

File tree

21 files changed

+71
-106
lines changed

21 files changed

+71
-106
lines changed

augmentedtree/atree_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ func TestTraverse(t *testing.T) {
639639
found[id.ID()] = true
640640
})
641641
for i := 0; i <= top; i++ {
642-
if found, _ := found[uint64(i)]; !found {
642+
if found := found[uint64(i)]; !found {
643643
t.Errorf("could not find expected interval %d", i)
644644
}
645645
}

bitarray/bitarray.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,7 @@ func (ba *bitArray) Equals(other BitArray) bool {
282282
}
283283

284284
lastIndex, _ := getIndexAndRemainder(ba.highest)
285-
if lastIndex >= selfIndex {
286-
return false
287-
}
288-
289-
return true
285+
return lastIndex < selfIndex
290286
}
291287

292288
// Intersects returns a bool indicating if the supplied bitarray intersects
@@ -371,7 +367,7 @@ func newBitArray(size uint64, args ...bool) *bitArray {
371367
anyset: false,
372368
}
373369

374-
if len(args) > 0 && args[0] == true {
370+
if len(args) > 0 && args[0] {
375371
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
376372
ba.blocks[i] = maximumBlock
377373
}

bitarray/block_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestBlockToNums(t *testing.T) {
3030

3131
expected := []uint64{s - 6, s - 2}
3232

33-
result := make([]uint64, 0, 0)
33+
result := make([]uint64, 0)
3434
b.toNums(0, &result)
3535
assert.Equal(t, expected, result)
3636
}
@@ -41,7 +41,7 @@ func BenchmarkBlockToNums(b *testing.B) {
4141
block = block.insert(i)
4242
}
4343

44-
nums := make([]uint64, 0, 0)
44+
nums := make([]uint64, 0)
4545
b.ResetTimer()
4646

4747
for i := 0; i < b.N; i++ {

bitarray/encoding.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
122122
var bytesRead int
123123
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
124124
if bytesRead < 0 {
125-
return errors.New("Invalid data for BitArray")
125+
return errors.New("invalid data for BitArray")
126126
}
127127
curLoc += intsize
128128

@@ -131,15 +131,15 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
131131
for i := uint64(0); i < intsToRead; i++ {
132132
nextblock, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
133133
if bytesRead < 0 {
134-
return errors.New("Invalid data for BitArray")
134+
return errors.New("invalid data for BitArray")
135135
}
136136
ret.blocks[i] = block(nextblock)
137137
curLoc += intsize
138138
}
139139

140140
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
141141
if bytesRead < 0 {
142-
return errors.New("Invalid data for BitArray")
142+
return errors.New("invalid data for BitArray")
143143
}
144144
curLoc += intsize
145145

@@ -148,7 +148,7 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
148148
for i := uint64(0); i < intsToRead; i++ {
149149
nextuint, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
150150
if bytesRead < 0 {
151-
return errors.New("Invalid data for BitArray")
151+
return errors.New("invalid data for BitArray")
152152
}
153153
ret.indices[i] = nextuint
154154
curLoc += intsize

bitarray/encoding_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestUnmarshalErrors(t *testing.T) {
124124
}
125125
}
126126

127-
outputBytes, err := Marshal(input)
127+
outputBytes, _ := Marshal(input)
128128

129129
outputBytes[0] = 'C'
130130

btree/immutable/delete.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (t *Tr) delete(keys Keys) error {
6363

6464
pb := path.peek()
6565
node := pb.n
66-
isRoot := bytes.Compare(node.ID, t.Root) == 0
66+
isRoot := bytes.Equal(node.ID, t.Root)
6767
if !t.context.nodeExists(node.ID) {
6868
cp := node.copy()
6969
t.context.addNode(cp)
@@ -103,7 +103,7 @@ func (t *Tr) delete(keys Keys) error {
103103
for pb.prev != nil {
104104
parentBundle := pb.prev
105105
parent := parentBundle.n
106-
isRoot := bytes.Compare(parent.ID, t.Root) == 0
106+
isRoot := bytes.Equal(parent.ID, t.Root)
107107
if !t.context.nodeExists(parent.ID) {
108108
cp := parent.copy()
109109
t.context.addNode(cp)

btree/immutable/rt_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ func TestNodeSplit(t *testing.T) {
413413
rt, err = mutable.Commit()
414414
require.NoError(t, err)
415415
rt, err = Load(cfg.Persister, rt.ID(), comparator)
416+
require.NoError(t, err)
416417

417418
result, err = mutable.(*Tr).toList(itemsToValues(items...)...)
418419
require.NoError(t, err)
@@ -505,6 +506,7 @@ func TestRandom(t *testing.T) {
505506
require.NoError(t, err)
506507
expected := toOrdered(items).toItems()
507508
result, err := mutable.(*Tr).toList(itemsToValues(expected...)...)
509+
require.NoError(t, err)
508510
if !assert.Equal(t, expected, result) {
509511
assert.Equal(t, len(expected), len(result))
510512
for i, c := range expected {
@@ -763,10 +765,11 @@ func TestSecondCommitMultipleSplits(t *testing.T) {
763765
mutable.AddItems(items[:25]...)
764766
mutable.(*Tr).verify(mutable.(*Tr).Root, t)
765767
rt, err := mutable.Commit()
768+
require.NoError(t, err)
766769
rt.(*Tr).verify(rt.(*Tr).Root, t)
767770

768-
result, err := rt.(*Tr).toList(itemsToValues(items...)...)
769-
require.Nil(t, err)
771+
result, errToList := rt.(*Tr).toList(itemsToValues(items...)...)
772+
require.Nil(t, errToList)
770773
assert.Equal(t, items[:25], result)
771774

772775
mutable = rt.AsMutable()

btree/palm/action.go

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func (ga *getAction) keys() common.Comparators {
5252
}
5353

5454
func (ga *getAction) addNode(i int64, n *node) {
55-
return // not necessary for gets
5655
}
5756

5857
func (ga *getAction) nodes() []*node {

fibheap/fibheap.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ func (heap *FloatingFibonacciHeap) DequeueMin() (*Entry, error) {
251251
func (heap *FloatingFibonacciHeap) DecreaseKey(node *Entry, newPriority float64) (*Entry, error) {
252252

253253
if heap.IsEmpty() {
254-
return nil, EmptyHeapError("Cannot decrease key in an empty heap")
254+
return nil, EmptyHeapError("cannot decrease key in an empty heap")
255255
}
256256

257257
if node == nil {
258-
return nil, NilError("Cannot decrease key: given node is nil")
258+
return nil, NilError("cannot decrease key: given node is nil")
259259
}
260260

261261
if newPriority >= node.Priority {
262-
return nil, fmt.Errorf("The given new priority: %v, is larger than or equal to the old: %v",
262+
return nil, fmt.Errorf("the given new priority: %v, is larger than or equal to the old: %v",
263263
newPriority, node.Priority)
264264
}
265265

@@ -271,11 +271,11 @@ func (heap *FloatingFibonacciHeap) DecreaseKey(node *Entry, newPriority float64)
271271
func (heap *FloatingFibonacciHeap) Delete(node *Entry) error {
272272

273273
if heap.IsEmpty() {
274-
return EmptyHeapError("Cannot delete element from an empty heap")
274+
return EmptyHeapError("cannot delete element from an empty heap")
275275
}
276276

277277
if node == nil {
278-
return NilError("Cannot delete node: given node is nil")
278+
return NilError("cannot delete node: given node is nil")
279279
}
280280

281281
decreaseKeyUnchecked(heap, node, -math.MaxFloat64)
@@ -291,7 +291,7 @@ func (heap *FloatingFibonacciHeap) Delete(node *Entry) error {
291291
func (heap *FloatingFibonacciHeap) Merge(other *FloatingFibonacciHeap) (FloatingFibonacciHeap, error) {
292292

293293
if heap == nil || other == nil {
294-
return FloatingFibonacciHeap{}, NilError("One of the heaps to merge is nil. Cannot merge")
294+
return FloatingFibonacciHeap{}, NilError("one of the heaps to merge is nil. Cannot merge")
295295
}
296296

297297
resultSize := heap.size + other.size

fibheap/fibheap_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ func TestFibHeap_Min_EmptyHeap(t *testing.T) {
164164
heap := NewFloatFibHeap()
165165

166166
heap.Enqueue(0)
167-
min, err := heap.DequeueMin()
167+
_, err := heap.DequeueMin()
168168
require.NoError(t, err)
169169

170170
// Heap should be empty at this point
171171

172-
min, err = heap.Min()
172+
min, err := heap.Min()
173173

174174
assert.EqualError(t, err, "Trying to get minimum element of empty heap")
175175
assert.Nil(t, min)
@@ -228,7 +228,7 @@ func TestFibHeap_DecreaseKey_EmptyHeap(t *testing.T) {
228228
min, err := heap.DecreaseKey(elem, 0)
229229

230230
assert.IsType(t, EmptyHeapError(""), err)
231-
assert.EqualError(t, err, "Cannot decrease key in an empty heap")
231+
assert.EqualError(t, err, "cannot decrease key in an empty heap")
232232
assert.Nil(t, min)
233233
}
234234

@@ -238,7 +238,7 @@ func TestFibHeap_DecreaseKey_NilNode(t *testing.T) {
238238
min, err := heap.DecreaseKey(nil, 0)
239239

240240
assert.IsType(t, NilError(""), err)
241-
assert.EqualError(t, err, "Cannot decrease key: given node is nil")
241+
assert.EqualError(t, err, "cannot decrease key: given node is nil")
242242
assert.Nil(t, min)
243243
}
244244

@@ -247,7 +247,7 @@ func TestFibHeap_DecreaseKey_LargerNewPriority(t *testing.T) {
247247
node := heap.Enqueue(1)
248248
min, err := heap.DecreaseKey(node, 20)
249249

250-
assert.EqualError(t, err, "The given new priority: 20, is larger than or equal to the old: 1")
250+
assert.EqualError(t, err, "the given new priority: 20, is larger than or equal to the old: 1")
251251
assert.Nil(t, min)
252252
}
253253

@@ -296,15 +296,15 @@ func TestFibHeap_Delete_EmptyHeap(t *testing.T) {
296296
// Heap should be empty at this point
297297
err := heap.Delete(elem)
298298
assert.IsType(t, EmptyHeapError(""), err)
299-
assert.EqualError(t, err, "Cannot delete element from an empty heap")
299+
assert.EqualError(t, err, "cannot delete element from an empty heap")
300300
}
301301

302302
func TestFibHeap_Delete_NilNode(t *testing.T) {
303303
heap := NewFloatFibHeap()
304304
heap.Enqueue(1)
305305
err := heap.Delete(nil)
306306
assert.IsType(t, NilError(""), err)
307-
assert.EqualError(t, err, "Cannot delete node: given node is nil")
307+
assert.EqualError(t, err, "cannot delete node: given node is nil")
308308
}
309309

310310
func TestMerge(t *testing.T) {
@@ -330,11 +330,10 @@ func TestMerge(t *testing.T) {
330330
}
331331

332332
func TestFibHeap_Merge_NilHeap(t *testing.T) {
333-
var heap FloatingFibonacciHeap
334-
heap = NewFloatFibHeap()
333+
heap := NewFloatFibHeap()
335334
newHeap, err := heap.Merge(nil)
336335
assert.IsType(t, NilError(""), err)
337-
assert.EqualError(t, err, "One of the heaps to merge is nil. Cannot merge")
336+
assert.EqualError(t, err, "one of the heaps to merge is nil. Cannot merge")
338337
assert.Equal(t, newHeap, FloatingFibonacciHeap{})
339338
}
340339

graph/simple.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ func (g *SimpleGraph) Degree(v interface{}) (int, error) {
118118
}
119119

120120
func (g *SimpleGraph) addVertex(v interface{}) {
121-
mm, ok := g.adjacencyList[v]
121+
_, ok := g.adjacencyList[v]
122122
if !ok {
123-
mm = make(map[interface{}]struct{})
123+
mm := make(map[interface{}]struct{})
124124
g.adjacencyList[v] = mm
125125
g.v++
126126
}

hashmap/fastinteger/hashmap_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,11 @@ func BenchmarkGoMapExists(b *testing.B) {
197197

198198
b.ResetTimer()
199199

200-
var ok bool
201200
for i := 0; i < b.N; i++ {
202-
for _, key := range keys {
203-
_, ok = hm[key] // or the compiler complains
204-
}
201+
for range keys { }
205202
}
206203

207204
b.StopTimer()
208-
if ok { // or the compiler complains
209-
}
210205
}
211206

212207
func BenchmarkDelete(b *testing.B) {

list/persistent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828

2929
// ErrEmptyList is returned when an invalid operation is performed on an
3030
// empty list.
31-
ErrEmptyList = errors.New("Empty list")
31+
ErrEmptyList = errors.New("empty list")
3232
)
3333

3434
// PersistentList is an immutable, persistent linked list.

rangetree/immutable.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,11 @@ func (irt *immutableRangeTree) apply(list orderedNodes, interval Interval,
203203
low, high := interval.LowAtDimension(dimension), interval.HighAtDimension(dimension)
204204

205205
if isLastDimension(irt.dimensions, dimension) {
206-
if !list.apply(low, high, fn) {
207-
return false
208-
}
206+
return list.apply(low, high, fn)
209207
} else {
210-
if !list.apply(low, high, func(n *node) bool {
211-
if !irt.apply(n.orderedNodes, interval, dimension+1, fn) {
212-
return false
213-
}
214-
return true
215-
}) {
216-
return false
217-
}
218-
return true
208+
return list.apply(low, high, func(n *node) bool {
209+
return irt.apply(n.orderedNodes, interval, dimension+1, fn)
210+
})
219211
}
220212

221213
return true

rangetree/orderedtree.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,11 @@ func (ot *orderedTree) apply(list orderedNodes, interval Interval,
183183
low, high := interval.LowAtDimension(dimension), interval.HighAtDimension(dimension)
184184

185185
if isLastDimension(ot.dimensions, dimension) {
186-
if !list.apply(low, high, fn) {
187-
return false
188-
}
186+
return list.apply(low, high, fn)
189187
} else {
190-
if !list.apply(low, high, func(n *node) bool {
191-
if !ot.apply(n.orderedNodes, interval, dimension+1, fn) {
192-
return false
193-
}
194-
return true
195-
}) {
196-
return false
197-
}
198-
return true
188+
return list.apply(low, high, func(n *node) bool {
189+
return ot.apply(n.orderedNodes, interval, dimension+1, fn)
190+
})
199191
}
200192

201193
return true

rtree/hilbert/action.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ func (ga *getAction) keys() hilberts {
5353
return nil
5454
}
5555

56-
func (ga *getAction) addNode(i int64, n *node) {
57-
return // not necessary for gets
58-
}
56+
func (ga *getAction) addNode(i int64, n *node) {}
5957

6058
func (ga *getAction) nodes() []*node {
6159
return nil

slice/skip/iterator.go

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package skip
1818

1919
import "github.com/Workiva/go-datastructures/common"
2020

21-
const iteratorExhausted = -2
22-
2321
// iterator represents an object that can be iterated. It will
2422
// return false on Next and nil on Value if there are no further
2523
// values to be iterated.

0 commit comments

Comments
 (0)