From deed9eb18e1556c3f81e21df80e46516ed632c50 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Wed, 4 Oct 2023 20:57:35 +0800 Subject: [PATCH] Remove redundant `len` check From the Go specification [1]: "3. If the map is nil, the number of iterations is 0." `len` returns 0 if the map is nil [2]. Therefore, checking `len(v) > 0` before a loop is unnecessary. [1]: https://go.dev/ref/spec#For_range [2]: https://pkg.go.dev/builtin#len Signed-off-by: Eng Zer Jun --- .../hosting_service/hosting_service.go | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/pkg/commands/hosting_service/hosting_service.go b/pkg/commands/hosting_service/hosting_service.go index 5744680abe7..9cb997a0ec8 100644 --- a/pkg/commands/hosting_service/hosting_service.go +++ b/pkg/commands/hosting_service/hosting_service.go @@ -99,32 +99,30 @@ func (self *HostingServiceMgr) getCandidateServiceDomains() []ServiceDomain { serviceDomains := slices.Clone(defaultServiceDomains) - if len(self.configServiceDomains) > 0 { - for gitDomain, typeAndDomain := range self.configServiceDomains { - provider, webDomain, success := strings.Cut(typeAndDomain, ":") - - // we allow for one ':' for specifying the TCP port - if !success || strings.Count(webDomain, ":") > 1 { - self.log.Errorf("Unexpected format for git service: '%s'. Expected something like 'github.com:github.com'", typeAndDomain) - continue - } - - serviceDefinition, ok := serviceDefinitionByProvider[provider] - if !ok { - providerNames := lo.Map(serviceDefinitions, func(serviceDefinition ServiceDefinition, _ int) string { - return serviceDefinition.provider - }) - - self.log.Errorf("Unknown git service type: '%s'. Expected one of %s", provider, strings.Join(providerNames, ", ")) - continue - } - - serviceDomains = append(serviceDomains, ServiceDomain{ - gitDomain: gitDomain, - webDomain: webDomain, - serviceDefinition: serviceDefinition, + for gitDomain, typeAndDomain := range self.configServiceDomains { + provider, webDomain, success := strings.Cut(typeAndDomain, ":") + + // we allow for one ':' for specifying the TCP port + if !success || strings.Count(webDomain, ":") > 1 { + self.log.Errorf("Unexpected format for git service: '%s'. Expected something like 'github.com:github.com'", typeAndDomain) + continue + } + + serviceDefinition, ok := serviceDefinitionByProvider[provider] + if !ok { + providerNames := lo.Map(serviceDefinitions, func(serviceDefinition ServiceDefinition, _ int) string { + return serviceDefinition.provider }) + + self.log.Errorf("Unknown git service type: '%s'. Expected one of %s", provider, strings.Join(providerNames, ", ")) + continue } + + serviceDomains = append(serviceDomains, ServiceDomain{ + gitDomain: gitDomain, + webDomain: webDomain, + serviceDefinition: serviceDefinition, + }) } return serviceDomains