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

Clean code and fix issues reported by staticcheck #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion augmentedtree/atree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func TestTraverse(t *testing.T) {
found[id.ID()] = true
})
for i := 0; i <= top; i++ {
if found, _ := found[uint64(i)]; !found {
if found := found[uint64(i)]; !found {
t.Errorf("could not find expected interval %d", i)
}
}
Expand Down
8 changes: 2 additions & 6 deletions bitarray/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,7 @@ func (ba *bitArray) Equals(other BitArray) bool {
}

lastIndex, _ := getIndexAndRemainder(ba.highest)
if lastIndex >= selfIndex {
return false
}

return true
return lastIndex < selfIndex
}

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

if len(args) > 0 && args[0] == true {
if len(args) > 0 && args[0] {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = maximumBlock
}
Expand Down
4 changes: 2 additions & 2 deletions bitarray/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBlockToNums(t *testing.T) {

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

result := make([]uint64, 0, 0)
result := make([]uint64, 0)
b.toNums(0, &result)
assert.Equal(t, expected, result)
}
Expand All @@ -41,7 +41,7 @@ func BenchmarkBlockToNums(b *testing.B) {
block = block.insert(i)
}

nums := make([]uint64, 0, 0)
nums := make([]uint64, 0)
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
8 changes: 4 additions & 4 deletions bitarray/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
var bytesRead int
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
return errors.New("invalid data for BitArray")
}
curLoc += intsize

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

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

Expand All @@ -148,7 +148,7 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
for i := uint64(0); i < intsToRead; i++ {
nextuint, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
return errors.New("invalid data for BitArray")
}
ret.indices[i] = nextuint
curLoc += intsize
Expand Down
2 changes: 1 addition & 1 deletion bitarray/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestUnmarshalErrors(t *testing.T) {
}
}

outputBytes, err := Marshal(input)
outputBytes, _ := Marshal(input)

outputBytes[0] = 'C'

Expand Down
4 changes: 2 additions & 2 deletions btree/immutable/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (t *Tr) delete(keys Keys) error {

pb := path.peek()
node := pb.n
isRoot := bytes.Compare(node.ID, t.Root) == 0
isRoot := bytes.Equal(node.ID, t.Root)
if !t.context.nodeExists(node.ID) {
cp := node.copy()
t.context.addNode(cp)
Expand Down Expand Up @@ -103,7 +103,7 @@ func (t *Tr) delete(keys Keys) error {
for pb.prev != nil {
parentBundle := pb.prev
parent := parentBundle.n
isRoot := bytes.Compare(parent.ID, t.Root) == 0
isRoot := bytes.Equal(parent.ID, t.Root)
if !t.context.nodeExists(parent.ID) {
cp := parent.copy()
t.context.addNode(cp)
Expand Down
7 changes: 5 additions & 2 deletions btree/immutable/rt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ func TestNodeSplit(t *testing.T) {
rt, err = mutable.Commit()
require.NoError(t, err)
rt, err = Load(cfg.Persister, rt.ID(), comparator)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we require.NoError here instead of underscoring?

require.NoError(t, err)

result, err = mutable.(*Tr).toList(itemsToValues(items...)...)
require.NoError(t, err)
Expand Down Expand Up @@ -505,6 +506,7 @@ func TestRandom(t *testing.T) {
require.NoError(t, err)
expected := toOrdered(items).toItems()
result, err := mutable.(*Tr).toList(itemsToValues(expected...)...)
require.NoError(t, err)
if !assert.Equal(t, expected, result) {
assert.Equal(t, len(expected), len(result))
for i, c := range expected {
Expand Down Expand Up @@ -763,10 +765,11 @@ func TestSecondCommitMultipleSplits(t *testing.T) {
mutable.AddItems(items[:25]...)
mutable.(*Tr).verify(mutable.(*Tr).Root, t)
rt, err := mutable.Commit()
require.NoError(t, err)
rt.(*Tr).verify(rt.(*Tr).Root, t)

result, err := rt.(*Tr).toList(itemsToValues(items...)...)
require.Nil(t, err)
result, errToList := rt.(*Tr).toList(itemsToValues(items...)...)
require.Nil(t, errToList)
assert.Equal(t, items[:25], result)

mutable = rt.AsMutable()
Expand Down
1 change: 0 additions & 1 deletion btree/palm/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func (ga *getAction) keys() common.Comparators {
}

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

func (ga *getAction) nodes() []*node {
Expand Down
12 changes: 6 additions & 6 deletions fibheap/fibheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ func (heap *FloatingFibonacciHeap) DequeueMin() (*Entry, error) {
func (heap *FloatingFibonacciHeap) DecreaseKey(node *Entry, newPriority float64) (*Entry, error) {

if heap.IsEmpty() {
return nil, EmptyHeapError("Cannot decrease key in an empty heap")
return nil, EmptyHeapError("cannot decrease key in an empty heap")
}

if node == nil {
return nil, NilError("Cannot decrease key: given node is nil")
return nil, NilError("cannot decrease key: given node is nil")
}

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

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

if heap.IsEmpty() {
return EmptyHeapError("Cannot delete element from an empty heap")
return EmptyHeapError("cannot delete element from an empty heap")
}

if node == nil {
return NilError("Cannot delete node: given node is nil")
return NilError("cannot delete node: given node is nil")
}

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

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

resultSize := heap.size + other.size
Expand Down
19 changes: 9 additions & 10 deletions fibheap/fibheap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func TestFibHeap_Min_EmptyHeap(t *testing.T) {
heap := NewFloatFibHeap()

heap.Enqueue(0)
min, err := heap.DequeueMin()
_, err := heap.DequeueMin()
require.NoError(t, err)

// Heap should be empty at this point

min, err = heap.Min()
min, err := heap.Min()

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

assert.IsType(t, EmptyHeapError(""), err)
assert.EqualError(t, err, "Cannot decrease key in an empty heap")
assert.EqualError(t, err, "cannot decrease key in an empty heap")
assert.Nil(t, min)
}

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

assert.IsType(t, NilError(""), err)
assert.EqualError(t, err, "Cannot decrease key: given node is nil")
assert.EqualError(t, err, "cannot decrease key: given node is nil")
assert.Nil(t, min)
}

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

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

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

func TestFibHeap_Delete_NilNode(t *testing.T) {
heap := NewFloatFibHeap()
heap.Enqueue(1)
err := heap.Delete(nil)
assert.IsType(t, NilError(""), err)
assert.EqualError(t, err, "Cannot delete node: given node is nil")
assert.EqualError(t, err, "cannot delete node: given node is nil")
}

func TestMerge(t *testing.T) {
Expand All @@ -330,11 +330,10 @@ func TestMerge(t *testing.T) {
}

func TestFibHeap_Merge_NilHeap(t *testing.T) {
var heap FloatingFibonacciHeap
heap = NewFloatFibHeap()
heap := NewFloatFibHeap()
newHeap, err := heap.Merge(nil)
assert.IsType(t, NilError(""), err)
assert.EqualError(t, err, "One of the heaps to merge is nil. Cannot merge")
assert.EqualError(t, err, "one of the heaps to merge is nil. Cannot merge")
assert.Equal(t, newHeap, FloatingFibonacciHeap{})
}

Expand Down
4 changes: 2 additions & 2 deletions graph/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func (g *SimpleGraph) Degree(v interface{}) (int, error) {
}

func (g *SimpleGraph) addVertex(v interface{}) {
mm, ok := g.adjacencyList[v]
_, ok := g.adjacencyList[v]
if !ok {
mm = make(map[interface{}]struct{})
mm := make(map[interface{}]struct{})
g.adjacencyList[v] = mm
g.v++
}
Expand Down
7 changes: 1 addition & 6 deletions hashmap/fastinteger/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,11 @@ func BenchmarkGoMapExists(b *testing.B) {

b.ResetTimer()

var ok bool
for i := 0; i < b.N; i++ {
for _, key := range keys {
_, ok = hm[key] // or the compiler complains
}
for range keys { }
}

b.StopTimer()
if ok { // or the compiler complains
}
}

func BenchmarkDelete(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion list/persistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (

// ErrEmptyList is returned when an invalid operation is performed on an
// empty list.
ErrEmptyList = errors.New("Empty list")
ErrEmptyList = errors.New("empty list")
)

// PersistentList is an immutable, persistent linked list.
Expand Down
16 changes: 4 additions & 12 deletions rangetree/immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,11 @@ func (irt *immutableRangeTree) apply(list orderedNodes, interval Interval,
low, high := interval.LowAtDimension(dimension), interval.HighAtDimension(dimension)

if isLastDimension(irt.dimensions, dimension) {
if !list.apply(low, high, fn) {
return false
}
return list.apply(low, high, fn)
} else {
if !list.apply(low, high, func(n *node) bool {
if !irt.apply(n.orderedNodes, interval, dimension+1, fn) {
return false
}
return true
}) {
return false
}
return true
return list.apply(low, high, func(n *node) bool {
return irt.apply(n.orderedNodes, interval, dimension+1, fn)
})
}

return true
Expand Down
16 changes: 4 additions & 12 deletions rangetree/orderedtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,11 @@ func (ot *orderedTree) apply(list orderedNodes, interval Interval,
low, high := interval.LowAtDimension(dimension), interval.HighAtDimension(dimension)

if isLastDimension(ot.dimensions, dimension) {
if !list.apply(low, high, fn) {
return false
}
return list.apply(low, high, fn)
} else {
if !list.apply(low, high, func(n *node) bool {
if !ot.apply(n.orderedNodes, interval, dimension+1, fn) {
return false
}
return true
}) {
return false
}
return true
return list.apply(low, high, func(n *node) bool {
return ot.apply(n.orderedNodes, interval, dimension+1, fn)
})
}

return true
Expand Down
4 changes: 1 addition & 3 deletions rtree/hilbert/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ func (ga *getAction) keys() hilberts {
return nil
}

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

func (ga *getAction) nodes() []*node {
return nil
Expand Down
2 changes: 0 additions & 2 deletions slice/skip/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package skip

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

const iteratorExhausted = -2

// iterator represents an object that can be iterated. It will
// return false on Next and nil on Value if there are no further
// values to be iterated.
Expand Down
Loading