Skip to content

Commit 9895e2e

Browse files
authored
chore: enable prealloc linter and address issues (testcontainers#3458)
* chore: enable prealloc linter and address issues * lint: remove redundant zero allocations
1 parent 16dfc65 commit 9895e2e

File tree

13 files changed

+21
-21
lines changed

13 files changed

+21
-21
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ linters:
1717
- nakedret
1818
- nolintlint
1919
- perfsprint
20+
- prealloc
2021
- revive
2122
- testifylint
2223
- thelper

docker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,13 @@ func (c *DockerContainer) ContainerIP(ctx context.Context) (string, error) {
513513

514514
// ContainerIPs gets the IP addresses of all the networks within the container.
515515
func (c *DockerContainer) ContainerIPs(ctx context.Context) ([]string, error) {
516-
ips := make([]string, 0)
517-
518516
inspect, err := c.Inspect(ctx)
519517
if err != nil {
520518
return nil, err
521519
}
522520

523521
networks := inspect.NetworkSettings.Networks
522+
ips := make([]string, 0, len(networks))
524523
for _, nw := range networks {
525524
ips = append(ips, nw.IPAddress)
526525
}

internal/core/images.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func ExtractImagesFromDockerfile(dockerfile string, buildArgs map[string]*string
3939

4040
// ExtractImagesFromReader extracts images from the Dockerfile sourced from r.
4141
func ExtractImagesFromReader(r io.Reader, buildArgs map[string]*string) ([]string, error) {
42-
var images []string
4342
var lines []string
4443
scanner := bufio.NewScanner(r)
4544
for scanner.Scan() {
@@ -49,6 +48,8 @@ func ExtractImagesFromReader(r io.Reader, buildArgs map[string]*string) ([]strin
4948
return nil, scanner.Err()
5049
}
5150

51+
images := make([]string, 0, len(lines))
52+
5253
// extract images from dockerfile
5354
for _, line := range lines {
5455
line = strings.TrimSpace(line)

modulegen/internal/context/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (ctx Context) getModulesByBaseDir(baseDir string) ([]string, error) {
6969
return nil, err
7070
}
7171

72-
dirs := make([]string, 0)
72+
var dirs []string
7373

7474
for _, f := range allFiles {
7575
if f.IsDir() {
@@ -90,7 +90,7 @@ func (ctx Context) getMarkdownsFromDir(baseDir string) ([]string, error) {
9090
return nil, err
9191
}
9292

93-
dirs := make([]string, 0)
93+
var dirs []string
9494

9595
for _, f := range allFiles {
9696
if !f.IsDir() && filepath.Ext(f.Name()) == ".md" {

modulegen/main_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,12 @@ func TestRefresh(t *testing.T) {
297297
err := internal.Refresh(testProject.ctx)
298298
require.NoError(t, err)
299299

300-
var modulesAndExamples []context.TestcontainersModule
301-
302300
examples, err := testProject.ctx.GetExamples()
303301
require.NoError(t, err)
302+
modules, err := testProject.ctx.GetModules()
303+
require.NoError(t, err)
304+
305+
modulesAndExamples := make([]context.TestcontainersModule, 0, len(examples)+len(modules))
304306

305307
for _, example := range examples {
306308
modulesAndExamples = append(modulesAndExamples, context.TestcontainersModule{
@@ -309,9 +311,6 @@ func TestRefresh(t *testing.T) {
309311
})
310312
}
311313

312-
modules, err := testProject.ctx.GetModules()
313-
require.NoError(t, err)
314-
315314
for _, module := range modules {
316315
modulesAndExamples = append(modulesAndExamples, context.TestcontainersModule{
317316
Name: module,

modules/cassandra/cassandra.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
4545
// WithInitScripts sets the init cassandra queries to be run when the container starts
4646
func WithInitScripts(scripts ...string) testcontainers.CustomizeRequestOption {
4747
return func(req *testcontainers.GenericContainerRequest) error {
48-
var initScripts []testcontainers.ContainerFile
49-
var execs []testcontainers.Executable
48+
initScripts := make([]testcontainers.ContainerFile, 0, len(scripts))
49+
execs := make([]testcontainers.Executable, 0, len(scripts))
5050
for _, script := range scripts {
5151
cf := testcontainers.ContainerFile{
5252
HostFilePath: script,

modules/couchbase/couchbase.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
6666
// Run creates an instance of the Couchbase container type
6767
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*CouchbaseContainer, error) {
6868
config := &Config{
69-
enabledServices: make([]Service, 0),
69+
enabledServices: []Service{},
7070
username: "Administrator",
7171
password: "password",
7272
indexStorageMode: MemoryOptimized,

modules/dolt/dolt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func WithCredsFile(credsFile string) testcontainers.CustomizeRequestOption {
259259

260260
func WithScripts(scripts ...string) testcontainers.CustomizeRequestOption {
261261
return func(req *testcontainers.GenericContainerRequest) error {
262-
var initScripts []testcontainers.ContainerFile
262+
initScripts := make([]testcontainers.ContainerFile, 0, len(scripts))
263263
for _, script := range scripts {
264264
cf := testcontainers.ContainerFile{
265265
HostFilePath: script,

modules/mariadb/mariadb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
9494

9595
func WithScripts(scripts ...string) testcontainers.CustomizeRequestOption {
9696
return func(req *testcontainers.GenericContainerRequest) error {
97-
var initScripts []testcontainers.ContainerFile
97+
initScripts := make([]testcontainers.ContainerFile, 0, len(scripts))
9898
for _, script := range scripts {
9999
cf := testcontainers.ContainerFile{
100100
HostFilePath: script,

modules/mysql/mysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
186186

187187
func WithScripts(scripts ...string) testcontainers.CustomizeRequestOption {
188188
return func(req *testcontainers.GenericContainerRequest) error {
189-
var initScripts []testcontainers.ContainerFile
189+
initScripts := make([]testcontainers.ContainerFile, 0, len(scripts))
190190
for _, script := range scripts {
191191
cf := testcontainers.ContainerFile{
192192
HostFilePath: script,

0 commit comments

Comments
 (0)