From 359632a2f4ea6863efcaed481e67e3b413e41ea8 Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Tue, 2 Sep 2025 04:05:15 +0300 Subject: [PATCH] lint: enable prealloc (#7493) --- .golangci.yml | 1 + plugin/header/header.go | 2 +- plugin/kubernetes/external.go | 6 ++++-- plugin/kubernetes/xfr.go | 3 +-- plugin/pkg/parse/host.go | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 6d7e36e74..f3ac5654a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,6 +14,7 @@ linters: - ineffassign - intrange - nolintlint + - prealloc - protogetter - staticcheck - thelper diff --git a/plugin/header/header.go b/plugin/header/header.go index 830587d02..785d7ff08 100644 --- a/plugin/header/header.go +++ b/plugin/header/header.go @@ -63,7 +63,7 @@ func newRules(key string, args []string) ([]Rule, error) { return nil, fmt.Errorf("unknown flag action=%s, should be set or clear", action) } - var rules []Rule + rules := make([]Rule, 0, len(args)) for _, arg := range args { flag := strings.ToLower(arg) switch flag { diff --git a/plugin/kubernetes/external.go b/plugin/kubernetes/external.go index ecba577f6..83f796928 100644 --- a/plugin/kubernetes/external.go +++ b/plugin/kubernetes/external.go @@ -224,8 +224,10 @@ func (k *Kubernetes) ExternalReverse(ip string) ([]msg.Service, error) { } func (k *Kubernetes) serviceRecordForExternalIP(ip string) []msg.Service { - var svcs []msg.Service - for _, service := range k.APIConn.SvcExtIndexReverse(ip) { + svcList := k.APIConn.SvcExtIndexReverse(ip) + svcLen := len(svcList) + svcs := make([]msg.Service, 0, svcLen) + for _, service := range svcList { if len(k.Namespaces) > 0 && !k.namespaceExposed(service.Namespace) { continue } diff --git a/plugin/kubernetes/xfr.go b/plugin/kubernetes/xfr.go index 0ed999ed5..3d3cedc2b 100644 --- a/plugin/kubernetes/xfr.go +++ b/plugin/kubernetes/xfr.go @@ -257,8 +257,7 @@ func emitAddressRecord(c chan<- []dns.RR, s msg.Service) string { // calcSRVWeight borrows the logic implemented in plugin.SRV for dynamically // calculating the srv weight and priority func calcSRVWeight(numservices int) uint16 { - var services []msg.Service - + services := make([]msg.Service, 0, numservices) for range numservices { services = append(services, msg.Service{}) } diff --git a/plugin/pkg/parse/host.go b/plugin/pkg/parse/host.go index 78f7cd93b..2b87c58fe 100644 --- a/plugin/pkg/parse/host.go +++ b/plugin/pkg/parse/host.go @@ -30,7 +30,7 @@ func stripZone(host string) string { // and in case of filename a resolv.conf like file is (assumed) and parsed and // the nameservers found are returned. func HostPortOrFile(s ...string) ([]string, error) { - var servers []string + var servers []string //nolint:prealloc // impossible to know the final length upfront for _, h := range s { trans, host := Transport(h) if len(host) == 0 {