Skip to content

Commit d0dee3c

Browse files
committed
linting: Consider pre-allocating sliceVar (prealloc)
While updating, also addressed some redundant fmt.Sprintf() opts/throttledevice.go:86:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:37:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:47:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ opts/weightdevice.go:68:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/context/store/metadatastore.go:96:2: Consider pre-allocating `res` (prealloc) var res []Metadata ^ cli/context/store/store.go:127:2: Consider pre-allocating `names` (prealloc) var names []string ^ cli/compose/loader/loader.go:223:2: Consider pre-allocating `keys` (prealloc) var keys []string ^ cli/compose/loader/loader.go:397:2: Consider pre-allocating `services` (prealloc) var services []types.ServiceConfig ^ cli/command/stack/loader/loader.go:63:2: Consider pre-allocating `msgs` (prealloc) var msgs []string ^ cli/command/stack/loader/loader.go:118:2: Consider pre-allocating `configFiles` (prealloc) var configFiles []composetypes.ConfigFile ^ cli/command/formatter/container.go:245:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/formatter/container.go:265:2: Consider pre-allocating `mounts` (prealloc) var mounts []string ^ cli/command/formatter/container.go:316:2: Consider pre-allocating `result` (prealloc) var result []string ^ cli/command/formatter/displayutils.go:43:2: Consider pre-allocating `display` (prealloc) var ( ^ cli/command/formatter/volume.go:103:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli-plugins/manager/manager_test.go:49:2: Consider pre-allocating `dirs` (prealloc) var dirs []string ^ cli/command/swarm/init.go:69:2: Consider pre-allocating `defaultAddrPool` (prealloc) var defaultAddrPool []string ^ cli/command/manifest/push.go:195:2: Consider pre-allocating `blobReqs` (prealloc) var blobReqs []manifestBlob ^ cli/command/secret/formatter.go:111:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/network/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/context/list.go:52:2: Consider pre-allocating `contexts` (prealloc) var contexts []*formatter.ClientContext ^ cli/command/config/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/trust/common_test.go:23:2: Consider pre-allocating `targetNames` (prealloc) var targetNames []string ^ cli/command/service/generic_resource_opts.go:55:2: Consider pre-allocating `generic` (prealloc) var generic []swarm.GenericResource ^ cli/command/service/generic_resource_opts.go:98:2: Consider pre-allocating `l` (prealloc) var l []swarm.GenericResource ^ cli/command/service/opts.go:378:2: Consider pre-allocating `netAttach` (prealloc) var netAttach []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:731:2: Consider pre-allocating `limits` (prealloc) var limits []*units.Ulimit ^ cli/command/service/update.go:1315:2: Consider pre-allocating `newNetworks` (prealloc) var newNetworks []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:1514:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/compose/convert/service.go:713:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ cli/compose/convert/volume.go:13:2: Consider pre-allocating `mounts` (prealloc) var mounts []mount.Mount ^ cli/command/stack/swarm/list.go:39:2: Consider pre-allocating `stacks` (prealloc) var stacks []*formatter.Stack ^ Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent efbcdce commit d0dee3c

26 files changed

Lines changed: 60 additions & 51 deletions

File tree

cli-plugins/manager/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestListPluginCandidates(t *testing.T) {
4646
)
4747
defer dir.Remove()
4848

49-
var dirs []string
49+
dirs := make([]string, 0, 6)
5050
for _, d := range []string{"plugins1", "nonexistent", "plugins2", "plugins3", "plugins4", "plugins5"} {
5151
dirs = append(dirs, dir.Join(d))
5252
}

cli/command/config/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (c *configContext) Labels() string {
101101
if mapLabels == nil {
102102
return ""
103103
}
104-
var joinLabels []string
104+
joinLabels := make([]string, 0, len(mapLabels))
105105
for k, v := range mapLabels {
106106
joinLabels = append(joinLabels, k+"="+v)
107107
}

cli/command/context/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
5151
var (
5252
curContext = dockerCli.CurrentContext()
5353
curFound bool
54-
contexts []*formatter.ClientContext
54+
contexts = make([]*formatter.ClientContext, 0, len(contextMap))
5555
)
5656
for _, rawMeta := range contextMap {
5757
isCurrent := rawMeta.Name == curContext

cli/command/formatter/container.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (c *ContainerContext) Labels() string {
245245
return ""
246246
}
247247

248-
var joinLabels []string
248+
joinLabels := make([]string, 0, len(c.c.Labels))
249249
for k, v := range c.c.Labels {
250250
joinLabels = append(joinLabels, k+"="+v)
251251
}
@@ -265,7 +265,7 @@ func (c *ContainerContext) Label(name string) string {
265265
// If the trunc option is set, names can be truncated (ellipsized).
266266
func (c *ContainerContext) Mounts() string {
267267
var name string
268-
var mounts []string
268+
mounts := make([]string, 0, len(c.c.Mounts))
269269
for _, m := range c.c.Mounts {
270270
if m.Name == "" {
271271
name = m.Source
@@ -289,7 +289,7 @@ func (c *ContainerContext) LocalVolumes() string {
289289
}
290290
}
291291

292-
return fmt.Sprintf("%d", count)
292+
return strconv.Itoa(count)
293293
}
294294

295295
// Networks returns a comma-separated string of networks that the container is
@@ -299,7 +299,7 @@ func (c *ContainerContext) Networks() string {
299299
return ""
300300
}
301301

302-
networks := []string{}
302+
networks := make([]string, 0, len(c.c.NetworkSettings.Networks))
303303
for k := range c.c.NetworkSettings.Networks {
304304
networks = append(networks, k)
305305
}
@@ -316,7 +316,7 @@ func DisplayablePorts(ports []types.Port) string {
316316
last uint16
317317
}
318318
groupMap := make(map[string]*portGroup)
319-
var result []string
319+
var result []string //nolint:prealloc
320320
var hostMappings []string
321321
var groupMapKeys []string
322322
sort.Slice(ports, func(i, j int) bool {
@@ -331,7 +331,7 @@ func DisplayablePorts(ports []types.Port) string {
331331
hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
332332
continue
333333
}
334-
portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
334+
portKey = port.IP + "/" + port.Type
335335
}
336336
group := groupMap[portKey]
337337

@@ -372,7 +372,7 @@ func formGroup(key string, start, last uint16) string {
372372
if ip != "" {
373373
group = fmt.Sprintf("%s:%s->%s", ip, group, group)
374374
}
375-
return fmt.Sprintf("%s/%s", group, groupType)
375+
return group + "/" + groupType
376376
}
377377

378378
func comparePorts(i, j types.Port) bool {

cli/command/formatter/displayutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Ellipsis(s string, maxDisplayWidth int) string {
4141
}
4242

4343
var (
44-
display []int
44+
display = make([]int, 0, len(rs))
4545
displayWidth int
4646
)
4747
for _, r := range rs {

cli/command/formatter/volume.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (c *volumeContext) Labels() string {
100100
return ""
101101
}
102102

103-
var joinLabels []string
103+
joinLabels := make([]string, 0, len(c.v.Labels))
104104
for k, v := range c.v.Labels {
105105
joinLabels = append(joinLabels, k+"="+v)
106106
}

cli/command/manifest/push.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ func buildManifestDescriptor(targetRepo *registry.RepositoryInfo, imageManifest
192192
}
193193

194194
func buildBlobRequestList(imageManifest types.ImageManifest, repoName reference.Named) ([]manifestBlob, error) {
195-
var blobReqs []manifestBlob
196-
197-
for _, blobDigest := range imageManifest.Blobs() {
195+
blobs := imageManifest.Blobs()
196+
blobReqs := make([]manifestBlob, 0, len(blobs))
197+
for _, blobDigest := range blobs {
198198
canonical, err := reference.WithDigest(repoName, blobDigest)
199199
if err != nil {
200200
return nil, err

cli/command/network/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (c *networkContext) Labels() string {
101101
return ""
102102
}
103103

104-
var joinLabels []string
104+
joinLabels := make([]string, 0, len(c.n.Labels))
105105
for k, v := range c.n.Labels {
106106
joinLabels = append(joinLabels, k+"="+v)
107107
}

cli/command/secret/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (c *secretContext) Labels() string {
108108
if mapLabels == nil {
109109
return ""
110110
}
111-
var joinLabels []string
111+
joinLabels := make([]string, 0, len(mapLabels))
112112
for k, v := range mapLabels {
113113
joinLabels = append(joinLabels, k+"="+v)
114114
}

cli/command/service/generic_resource_opts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func ParseGenericResources(value []string) ([]swarm.GenericResource, error) {
5252

5353
// genericResourcesFromGRPC converts a GRPC GenericResource to a GenericResource
5454
func genericResourcesFromGRPC(genericRes []*swarmapi.GenericResource) []swarm.GenericResource {
55-
var generic []swarm.GenericResource
55+
generic := make([]swarm.GenericResource, 0, len(genericRes))
5656
for _, res := range genericRes {
5757
var current swarm.GenericResource
5858

@@ -95,7 +95,7 @@ func buildGenericResourceMap(genericRes []swarm.GenericResource) (map[string]swa
9595
}
9696

9797
func buildGenericResourceList(genericRes map[string]swarm.GenericResource) []swarm.GenericResource {
98-
var l []swarm.GenericResource
98+
l := make([]swarm.GenericResource, 0, len(genericRes))
9999

100100
for _, res := range genericRes {
101101
l = append(l, res)

0 commit comments

Comments
 (0)