Skip to content

Commit 2e544d6

Browse files
committed
cli/command: fix prealloc linting
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 12a0b0b commit 2e544d6

6 files changed

Lines changed: 21 additions & 22 deletions

File tree

cli/command/image/build_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (f *fakeBuild) build(_ context.Context, buildContext io.Reader, options cli
190190

191191
func (f *fakeBuild) headers(t *testing.T) []*tar.Header {
192192
t.Helper()
193-
headers := []*tar.Header{}
193+
var headers []*tar.Header
194194
for {
195195
hdr, err := f.context.Next()
196196
switch err {
@@ -206,8 +206,9 @@ func (f *fakeBuild) headers(t *testing.T) []*tar.Header {
206206

207207
func (f *fakeBuild) filenames(t *testing.T) []string {
208208
t.Helper()
209-
names := []string{}
210-
for _, header := range f.headers(t) {
209+
h := f.headers(t)
210+
names := make([]string, 0, len(h))
211+
for _, header := range h {
211212
names = append(names, header.Name)
212213
}
213214
sort.Strings(names)

cli/command/service/list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestServiceListServiceStatus(t *testing.T) {
108108
},
109109
}
110110

111-
matrix := make([]testCase, 0)
111+
matrix := make([]testCase, 0, len(tests))
112112
for _, quiet := range []bool{false, true} {
113113
for _, tc := range tests {
114114
if quiet {

cli/command/service/progress/progress_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestReplicatedProgressUpdaterOneReplica(t *testing.T) {
8080
service: service,
8181
}
8282

83-
tasks := []swarm.Task{}
83+
var tasks []swarm.Task //nolint:prealloc // no need to over-complicate things.
8484

8585
ut.testUpdater(tasks, false,
8686
[]progress.Progress{
@@ -193,7 +193,7 @@ func TestReplicatedProgressUpdaterManyReplicas(t *testing.T) {
193193
service: service,
194194
}
195195

196-
tasks := []swarm.Task{}
196+
var tasks []swarm.Task
197197

198198
// No per-task progress bars because there are too many replicas
199199
ut.testUpdater(tasks, false,
@@ -248,7 +248,7 @@ func TestGlobalProgressUpdaterOneNode(t *testing.T) {
248248
service: service,
249249
}
250250

251-
tasks := []swarm.Task{}
251+
var tasks []swarm.Task //nolint:prealloc // no need to over-complicate things.
252252

253253
ut.testUpdater(tasks, false,
254254
[]progress.Progress{
@@ -362,7 +362,7 @@ func TestGlobalProgressUpdaterManyNodes(t *testing.T) {
362362
ut.activeNodes[strconv.Itoa(i)] = struct{}{}
363363
}
364364

365-
tasks := []swarm.Task{}
365+
var tasks []swarm.Task
366366

367367
ut.testUpdater(tasks, false,
368368
[]progress.Progress{
@@ -459,25 +459,24 @@ func TestReplicatedJobProgressUpdaterSmall(t *testing.T) {
459459
})
460460

461461
// wipe the old tasks out of the list
462-
tasks = []swarm.Task{}
463-
tasks = append(tasks,
464-
swarm.Task{
462+
tasks = []swarm.Task{ //nolint:prealloc // no need to over-complicate things.
463+
{
465464
ID: "task1",
466465
Slot: 0,
467466
NodeID: "",
468467
DesiredState: swarm.TaskStateComplete,
469468
Status: swarm.TaskStatus{State: swarm.TaskStateNew},
470469
JobIteration: &swarm.Version{Index: service.JobStatus.JobIteration.Index},
471470
},
472-
swarm.Task{
471+
{
473472
ID: "task2",
474473
Slot: 1,
475474
NodeID: "",
476475
DesiredState: swarm.TaskStateComplete,
477476
Status: swarm.TaskStatus{State: swarm.TaskStateNew},
478477
JobIteration: &swarm.Version{Index: service.JobStatus.JobIteration.Index},
479478
},
480-
)
479+
}
481480
ut.testUpdater(tasks, false, []progress.Progress{
482481
{ID: "1/5", Action: "new ", Current: 1, Total: 10, HideCounts: true},
483482
{ID: "2/5", Action: "new ", Current: 1, Total: 10, HideCounts: true},
@@ -644,7 +643,7 @@ func TestReplicatedJobProgressUpdaterLarge(t *testing.T) {
644643
service: service,
645644
}
646645

647-
tasks := []swarm.Task{}
646+
var tasks []swarm.Task
648647

649648
// see the comments in TestReplicatedJobProgressUpdaterSmall for why
650649
// we write this out twice.
@@ -741,7 +740,7 @@ func TestGlobalJobProgressUpdaterSmall(t *testing.T) {
741740
service: service,
742741
}
743742

744-
tasks := []swarm.Task{
743+
tasks := []swarm.Task{ //nolint:prealloc // no need to over-complicate things.
745744
{
746745
ID: "oldtask1",
747746
DesiredState: swarm.TaskStateComplete,
@@ -871,7 +870,7 @@ func TestGlobalJobProgressUpdaterLarge(t *testing.T) {
871870
service: service,
872871
}
873872

874-
tasks := []swarm.Task{}
873+
var tasks []swarm.Task //nolint:prealloc // no need to over-complicate things.
875874
for nodeID := range activeNodes {
876875
tasks = append(tasks, swarm.Task{
877876
ID: "task" + nodeID,

cli/command/stack/loader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func loadComposeFile(streams command.Streams, opts deployOptions) (*composetypes
2727
return nil, err
2828
}
2929

30-
dicts := getDictsFrom(configDetails.ConfigFiles)
3130
config, err := loader.Load(configDetails)
3231
if err != nil {
3332
var fpe *loader.ForbiddenPropertiesError
@@ -39,6 +38,7 @@ func loadComposeFile(streams command.Streams, opts deployOptions) (*composetypes
3938
return nil, err
4039
}
4140

41+
dicts := getDictsFrom(configDetails.ConfigFiles)
4242
unsupportedProperties := loader.GetUnsupportedProperties(dicts...)
4343
if len(unsupportedProperties) > 0 {
4444
_, _ = fmt.Fprintf(streams.Err(), "Ignoring unsupported options: %s\n\n",
@@ -65,8 +65,7 @@ func loadComposeFile(streams command.Streams, opts deployOptions) (*composetypes
6565
}
6666

6767
func getDictsFrom(configFiles []composetypes.ConfigFile) []map[string]any {
68-
dicts := []map[string]any{}
69-
68+
dicts := make([]map[string]any, 0, len(configFiles))
7069
for _, configFile := range configFiles {
7170
dicts = append(dicts, configFile.Config)
7271
}

cli/command/swarm/opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (*ExternalCAOption) Type() string {
110110

111111
// String returns a string repr of this option.
112112
func (m *ExternalCAOption) String() string {
113-
externalCAs := []string{}
113+
externalCAs := make([]string, 0, len(m.values))
114114
for _, externalCA := range m.values {
115115
repr := fmt.Sprintf("%s: %s", externalCA.Protocol, externalCA.URL)
116116
externalCAs = append(externalCAs, repr)

cli/command/system/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ func prettyPrintEvent(out io.Writer, event events.Message) error {
133133
_, _ = fmt.Fprintf(out, "%s %s %s", event.Type, event.Action, event.Actor.ID)
134134

135135
if len(event.Actor.Attributes) > 0 {
136-
var attrs []string
137-
var keys []string
136+
keys := make([]string, 0, len(event.Actor.Attributes))
138137
for k := range event.Actor.Attributes {
139138
keys = append(keys, k)
140139
}
141140
sort.Strings(keys)
141+
attrs := make([]string, 0, len(keys))
142142
for _, k := range keys {
143143
v := event.Actor.Attributes[k]
144144
attrs = append(attrs, k+"="+v)

0 commit comments

Comments
 (0)