Skip to content

Commit 28b03a4

Browse files
authored
GODRIVER-2156 Enable prealloc linter. (#799)
1 parent c610988 commit 28b03a4

File tree

7 files changed

+10
-9
lines changed

7 files changed

+10
-9
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ linters:
1616
- makezero
1717
# - misspell
1818
- nakedret
19+
- prealloc
1920
- revive
2021
- staticcheck
2122
- structcheck

internal/string_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func StringSliceFromRawValue(name string, val bson.RawValue) ([]string, error) {
3333
return nil, err
3434
}
3535

36-
var strs []string
36+
strs := make([]string, 0, len(arrayValues))
3737
for _, arrayVal := range arrayValues {
3838
str, ok := arrayVal.StringValueOK()
3939
if !ok {

internal/testutil/ops.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
// AutoCreateIndexes creates an index in the test cluster.
2626
func AutoCreateIndexes(t *testing.T, keys []string) {
27-
var elems [][]byte
27+
elems := make([][]byte, 0, len(keys))
2828
for _, k := range keys {
2929
elems = append(elems, bsoncore.AppendInt32Element(nil, k, 1))
3030
}

mongo/integration/gridfs_spec_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func arrangeGridfsCollections(mt *mtest.T, arrange gridfsArrange) {
194194
return
195195
}
196196

197-
var arrangeCmds []interface{}
197+
arrangeCmds := make([]interface{}, 0, len(arrange.Data))
198198
for _, cmd := range arrange.Data {
199199
if cmd[0].Key != "update" {
200200
arrangeCmds = append(arrangeCmds, cmd)
@@ -230,7 +230,7 @@ func arrangeGridfsCollections(mt *mtest.T, arrange gridfsArrange) {
230230
func executeUploadAssert(mt *mtest.T, fileID primitive.ObjectID, assert gridfsAssert) {
231231
fileIDVal := bsonx.ObjectID(fileID)
232232

233-
var assertCommands []interface{}
233+
assertCommands := make([]interface{}, 0, len(assert.Data))
234234
for _, data := range assert.Data {
235235
documentsIdx := data.IndexOf("documents")
236236
if documentsIdx == -1 {
@@ -412,7 +412,7 @@ func executeGridfsDelete(mt *mtest.T, test gridfsTest, bucket *gridfs.Bucket) {
412412
compareGridfsAssertError(mt, test.Assert.Error, err)
413413
return
414414
}
415-
var cmds []interface{}
415+
cmds := make([]interface{}, 0, len(test.Assert.Data))
416416
for _, cmd := range test.Assert.Data {
417417
cmds = append(cmds, cmd)
418418
}
@@ -432,7 +432,7 @@ func setupGridfsTest(mt *mtest.T, data gridfsData) int32 {
432432
break
433433
}
434434
}
435-
var chunksDocs []interface{}
435+
chunksDocs := make([]interface{}, 0, len(data.Chunks))
436436
for _, chunk := range data.Chunks {
437437
if hexStr, err := chunk.LookupErr("data", "$hex"); err == nil {
438438
hexBytes := hexStringToBytes(mt, hexStr.StringValue())

mongo/integration/mtest/mongotest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ func (t *T) verifyConstraints() error {
794794

795795
// Stop once we find a RunOnBlock that matches the current environment. Record all errors as we go because if we
796796
// don't find any matching blocks, we want to report the comparison errors for each block.
797-
var runOnErrors []error
797+
runOnErrors := make([]error, 0, len(t.runOn))
798798
for _, runOn := range t.runOn {
799799
err := verifyRunOnBlockConstraint(runOn)
800800
if err == nil {

x/mongo/driver/auth/internal/awsv4/signer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (ctx *signingCtx) buildCredentialString() {
234234
}
235235

236236
func (ctx *signingCtx) buildCanonicalHeaders(r rule, header http.Header) {
237-
var headers []string
237+
headers := make([]string, 0, len(header))
238238
headers = append(headers, "host")
239239
for k, v := range header {
240240
if !r.IsValid(k) {

x/mongo/driver/dns/dns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (r *Resolver) fetchSeedlistFromSRV(host string, srvName string, stopOnErr b
8686

8787
trimmedHost := strings.TrimSuffix(host, ".")
8888

89-
var parsedHosts []string
89+
parsedHosts := make([]string, 0, len(addresses))
9090
for _, address := range addresses {
9191
trimmedAddressTarget := strings.TrimSuffix(address.Target, ".")
9292
err := validateSRVResult(trimmedAddressTarget, trimmedHost)

0 commit comments

Comments
 (0)