linter: prealloc (#2315)

This commit is contained in:
Travis Turner 2023-03-11 21:19:05 -06:00 committed by GitHub
parent d2856bfeee
commit c79cc3b7db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 23 additions and 20 deletions

View file

@ -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)

View file

@ -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))
}

View file

@ -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)
}

View file

@ -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)

View file

@ -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

View file

@ -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 {

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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 {

View file

@ -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

View file

@ -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()

View file

@ -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:] {

View file

@ -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)