diff --git a/.golangci.yml b/.golangci.yml index a39132ee0..4880eff2d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,7 +25,7 @@ linters: - errname - gofmt # - misspell (lots to fix, but we should) - # - prealloc (15 to fix) + - prealloc # - predeclared (20 to fix) # - stylecheck (quite a lot to fix, but we should definitely work on this) # - unconvert (not at all critical, but makes for cleaner code) diff --git a/cache_test.go b/cache_test.go index 8f19a44d3..7dd5c958e 100644 --- a/cache_test.go +++ b/cache_test.go @@ -6,7 +6,7 @@ import ( "reflect" "testing" - "github.com/featurebasedb/featurebase/v3" + pilosa "github.com/featurebasedb/featurebase/v3" ) // Ensure cache stays constrained to its configured size. @@ -62,7 +62,7 @@ func TestCache_Rank_Dirty(t *testing.T) { cache.Add(v.ID, v.Count) } - var got []pair + var got []pair //nolint:prealloc for _, p := range cache.Top() { got = append(got, pair(p)) } diff --git a/dax/table.go b/dax/table.go index e3e078426..24dd0020e 100644 --- a/dax/table.go +++ b/dax/table.go @@ -459,7 +459,7 @@ func (t *Table) HasValidPrimaryKey() bool { // FieldNames returns the list of field names associated with the table. func (t *Table) FieldNames() []FieldName { - var ret []FieldName + ret := make([]FieldName, 0, len(t.Fields)) for _, f := range t.Fields { ret = append(ret, f.Name) } diff --git a/debugstats/stats.go b/debugstats/stats.go index 686f13af0..886a39e2a 100644 --- a/debugstats/stats.go +++ b/debugstats/stats.go @@ -57,7 +57,7 @@ func (c *CallStats) Report(title string) (r string) { r = fmt.Sprintf("CallStats: (%v)\n", title) c.mu.Lock() defer c.mu.Unlock() - var lines []*LineSorter + lines := make([]*LineSorter, 0, len(c.elap)) for id, elap := range c.elap { slc := elap.dur n := len(slc) diff --git a/etcd/embed.go b/etcd/embed.go index 3eefab6ec..8feec9bfd 100644 --- a/etcd/embed.go +++ b/etcd/embed.go @@ -134,8 +134,9 @@ func (e *EmbeddedEtcd) Shutdown() { } func (e *EmbeddedEtcd) Peers() []*disco.Peer { - var peers []*disco.Peer - for _, member := range e.e.Server.Cluster().Members() { + members := e.e.Server.Cluster().Members() + peers := make([]*disco.Peer, 0, len(members)) + for _, member := range members { peers = append(peers, &disco.Peer{ID: member.ID.String(), URL: member.PickPeerURL()}) } return peers diff --git a/executor.go b/executor.go index 48ab171e2..6f44885d3 100644 --- a/executor.go +++ b/executor.go @@ -2321,7 +2321,7 @@ func (e *executor) executeTopKShardTime(ctx context.Context, tx Tx, filter *Row, } // Fetch fragments. - var fragments []*fragment + fragments := make([]*fragment, 0, len(views)) for _, view := range views { f := e.Holder.fragment(index, field, view, shard) if f == nil { diff --git a/executor_test.go b/executor_test.go index 309d5baa7..b3d5ce248 100644 --- a/executor_test.go +++ b/executor_test.go @@ -7620,7 +7620,7 @@ func variousQueriesOnPercentiles(t *testing.T, c *test.Cluster) { } // generate string-set entries for index - var stringEntries [][2]string + stringEntries := make([][2]string, 0, len(testValues)) for _, v := range testValues { stringEntries = append(stringEntries, [2]string{v.rowKey, v.colKey}) @@ -7657,7 +7657,7 @@ func variousQueriesOnPercentiles(t *testing.T, c *test.Cluster) { // generate test cases per each nth argument nthsFloat := []float64{0, 10, 25, 50, 75, 90, 99} - var tests []testCase + tests := make([]testCase, 0, len(nthsFloat)) for _, nth := range nthsFloat { query := fmt.Sprintf(`Percentile(field="net_worth", filter=Row(val="foo"), nth=%f)`, nth) expectedPercentile := getExpectedPercentile(nums, nth) diff --git a/field.go b/field.go index 4647aaa5c..67b96b40f 100644 --- a/field.go +++ b/field.go @@ -1018,8 +1018,9 @@ func (f *Field) viewsByTimeRange(from, to time.Time) (views []string, err error) } // Get min/max based on existing views. - var vs []string - for _, v := range f.views() { + fv := f.views() + vs := make([]string, 0, len(fv)) + for _, v := range fv { vs = append(vs, v.name) } min, max := minMaxViews(vs, q) diff --git a/http_handler.go b/http_handler.go index cdfc4fe09..5d7eb79c2 100644 --- a/http_handler.go +++ b/http_handler.go @@ -2907,8 +2907,9 @@ const ( // parseUint64Slice returns a slice of uint64s from a comma-delimited string. func parseUint64Slice(s string) ([]uint64, error) { - var a []uint64 - for _, str := range strings.Split(s, ",") { + ss := strings.Split(s, ",") + a := make([]uint64, 0, len(ss)) + for _, str := range ss { // Ignore blanks. if str == "" { continue diff --git a/rbf/cursor.go b/rbf/cursor.go index 0b92b062f..829d70391 100644 --- a/rbf/cursor.go +++ b/rbf/cursor.go @@ -493,7 +493,7 @@ func (c *Cursor) putLeafCell(in leafCell) (err error) { // Write each group to a separate page. newRoot := (len(groups) > 1) && (c.stack.top == 0) - var parents []branchCell + parents := make([]branchCell, 0, len(groups)) origPgno := elem.pgno // newRoot if split occured and bottom of the stack for i, group := range groups { @@ -724,7 +724,7 @@ func (c *Cursor) putBranchCells(stackIndex int, newCells []branchCell) (err erro } // Write each group to a separate page. - var parents []branchCell + parents := make([]branchCell, 0, len(groups)) origPgno := readPageNo(page) newRoot := len(groups) > 1 && stackIndex == 0 for i, group := range groups { diff --git a/rbf/cursor_internal_test.go b/rbf/cursor_internal_test.go index f05f0ff90..b7866e14e 100644 --- a/rbf/cursor_internal_test.go +++ b/rbf/cursor_internal_test.go @@ -332,7 +332,7 @@ func TestCursor_putBranchCellsHandlesLotsOfNewBranchesAtTheRoot(t *testing.T) { groups := splitLeafCells(leafcells) - var branches []branchCell + branches := make([]branchCell, 0, len(groups)) for i, group := range groups { _ = i diff --git a/sql/select.go b/sql/select.go index a3ad269dd..7b253ba8e 100644 --- a/sql/select.go +++ b/sql/select.go @@ -206,7 +206,7 @@ func (h handlerSelectFieldsFromTableWhere) Apply(stmt *sqlparser.Select, qm Quer return nil, errors.Wrap(err, "extracting select fields") } - var fields []string + var fields []string //nolint:prealloc for _, fld := range selectFields { if _, ok := fld.(*StarColumn); ok { pflds := index.Fields() diff --git a/sql3/planner/inbuiltfunctionsstring.go b/sql3/planner/inbuiltfunctionsstring.go index 865ab42af..535760800 100644 --- a/sql3/planner/inbuiltfunctionsstring.go +++ b/sql3/planner/inbuiltfunctionsstring.go @@ -706,7 +706,7 @@ func (n *callPlanExpression) EvaluateFormat(currentRow []interface{}) (interface return nil, sql3.NewErrUnexpectedTypeConversion(0, 0, argEval) } - var args []interface{} + args := make([]interface{}, 0, len(n.args)-1) // loop, since args can be of any length. for _, arg := range n.args[1:] { diff --git a/view.go b/view.go index c71d5cea6..d73caf530 100644 --- a/view.go +++ b/view.go @@ -134,7 +134,7 @@ func (v *view) openWithShardSet(ss *shardSet) error { shards := ss.CloneMaybe() - var frags []*fragment + frags := make([]*fragment, 0, len(shards)) for shard := range shards { frag := v.newFragment(shard) frags = append(frags, frag)