From e0f1a495f7878436cef25fbdf041a156a3348dfd Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 9 Jan 2025 20:16:37 +0100 Subject: [PATCH] chore: enable early-return from revive Signed-off-by: Matthieu MOREL --- .golangci.yml | 5 +++++ confmap/provider/aesprovider/provider_test.go | 5 ++--- internal/aws/metrics/metric_calculator.go | 5 ++--- internal/sqlquery/scraper.go | 5 ++--- pkg/stanza/entry/entry.go | 6 +++--- .../internal/stores/podstore.go | 17 ++++++++--------- receiver/awss3receiver/receiver.go | 8 ++++---- .../internal/kubelet/metadata.go | 12 ++++++------ receiver/saphanareceiver/queries.go | 9 ++++----- 9 files changed, 36 insertions(+), 36 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 27206273d148..53d3ba663ade 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -89,6 +89,9 @@ linters-settings: - name: context-keys-type # Importing with `.` makes the programs much harder to understand - name: dot-imports + - name: early-return + arguments: + - "preserveScope" # Empty blocks make code less readable and could be a symptom of a bug or unfinished refactoring. - name: empty-block # for better readability, variables of type `error` must be named with the prefix `err`. @@ -111,6 +114,8 @@ linters-settings: - name: redefines-builtin-id # redundant else-blocks that can be eliminated from the code. - name: superfluous-else + arguments: + - "preserveScope" # prevent confusing name for variables when using `time` package - name: time-naming # warns when an exported function or method returns a value of an un-exported type. diff --git a/confmap/provider/aesprovider/provider_test.go b/confmap/provider/aesprovider/provider_test.go index cc8e62489246..d81aee471564 100644 --- a/confmap/provider/aesprovider/provider_test.go +++ b/confmap/provider/aesprovider/provider_test.go @@ -104,13 +104,12 @@ func TestAESCredentialProvider(t *testing.T) { p := NewFactory().Create(confmap.ProviderSettings{}) retrieved, err := p.Retrieve(context.Background(), tt.configValue, nil) - if tt.expectedError == "" { - require.NoError(t, err) - } else { + if tt.expectedError != "" { require.Error(t, err) require.Equal(t, tt.expectedError, err.Error()) return } + require.NoError(t, err) require.NotNil(t, retrieved) stringValue, err := retrieved.AsString() require.NoError(t, err) diff --git a/internal/aws/metrics/metric_calculator.go b/internal/aws/metrics/metric_calculator.go index 16435ef7f754..979bb91a6c50 100644 --- a/internal/aws/metrics/metric_calculator.go +++ b/internal/aws/metrics/metric_calculator.go @@ -26,11 +26,10 @@ func NewFloat64DeltaCalculator() MetricCalculator { func calculateDelta(prev *MetricValue, val any, _ time.Time) (any, bool) { var deltaValue float64 - if prev != nil { - deltaValue = val.(float64) - prev.RawValue.(float64) - } else { + if prev == nil { return deltaValue, false } + deltaValue = val.(float64) - prev.RawValue.(float64) return deltaValue, true } diff --git a/internal/sqlquery/scraper.go b/internal/sqlquery/scraper.go index 680a6164fa0a..0de61c0559e9 100644 --- a/internal/sqlquery/scraper.go +++ b/internal/sqlquery/scraper.go @@ -74,11 +74,10 @@ func (s *Scraper) ScrapeMetrics(ctx context.Context) (pmetric.Metrics, error) { out := pmetric.NewMetrics() rows, err := s.Client.QueryRows(ctx) if err != nil { - if errors.Is(err, ErrNullValueWarning) { - s.Logger.Warn("problems encountered getting metric rows", zap.Error(err)) - } else { + if !errors.Is(err, ErrNullValueWarning) { return out, fmt.Errorf("Scraper: %w", err) } + s.Logger.Warn("problems encountered getting metric rows", zap.Error(err)) } ts := pcommon.NewTimestampFromTime(time.Now()) rms := out.ResourceMetrics() diff --git a/pkg/stanza/entry/entry.go b/pkg/stanza/entry/entry.go index 882f22695f41..caf8876644e2 100644 --- a/pkg/stanza/entry/entry.go +++ b/pkg/stanza/entry/entry.go @@ -136,11 +136,11 @@ func (entry *Entry) readToStringMap(field FieldInterface, dest *map[string]strin case map[string]any: newDest := make(map[string]string) for k, v := range m { - if vStr, ok := v.(string); ok { - newDest[k] = vStr - } else { + vStr, ok := v.(string) + if !ok { return fmt.Errorf("can not cast map members '%s' of type '%s' to string", k, v) } + newDest[k] = vStr } *dest = newDest case map[any]any: diff --git a/receiver/awscontainerinsightreceiver/internal/stores/podstore.go b/receiver/awscontainerinsightreceiver/internal/stores/podstore.go index 16350c21fe04..71396090b20e 100644 --- a/receiver/awscontainerinsightreceiver/internal/stores/podstore.go +++ b/receiver/awscontainerinsightreceiver/internal/stores/podstore.go @@ -206,18 +206,17 @@ func (p *PodStore) Decorate(ctx context.Context, metric CIMetric, kubernetesBlob } // If the entry is not a placeholder, decorate the pod - if entry.pod.Name != "" { - p.decorateCPU(metric, &entry.pod) - p.decorateMem(metric, &entry.pod) - p.addStatus(metric, &entry.pod) - addContainerCount(metric, &entry.pod) - addContainerID(&entry.pod, metric, kubernetesBlob, p.logger) - p.addPodOwnersAndPodName(metric, &entry.pod, kubernetesBlob) - addLabels(&entry.pod, kubernetesBlob) - } else { + if entry.pod.Name == "" { p.logger.Warn("no pod information is found in podstore for pod " + podKey) return false } + p.decorateCPU(metric, &entry.pod) + p.decorateMem(metric, &entry.pod) + p.addStatus(metric, &entry.pod) + addContainerCount(metric, &entry.pod) + addContainerID(&entry.pod, metric, kubernetesBlob, p.logger) + p.addPodOwnersAndPodName(metric, &entry.pod, kubernetesBlob) + addLabels(&entry.pod, kubernetesBlob) } return true } diff --git a/receiver/awss3receiver/receiver.go b/receiver/awss3receiver/receiver.go index 89e67c4f525c..74f7a481d9eb 100644 --- a/receiver/awss3receiver/receiver.go +++ b/receiver/awss3receiver/receiver.go @@ -251,11 +251,11 @@ func newEncodingExtensions(encodingsConfig []Encoding, host component.Host) (enc encodings := make(encodingExtensions, 0) extensions := host.GetExtensions() for _, configItem := range encodingsConfig { - if e, ok := extensions[configItem.Extension]; ok { - encodings = append(encodings, encodingExtension{extension: e, suffix: configItem.Suffix}) - } else { + e, ok := extensions[configItem.Extension]; + if !ok { return nil, fmt.Errorf("extension %q not found", configItem.Extension) - } + } + encodings = append(encodings, encodingExtension{extension: e, suffix: configItem.Suffix}) } return encodings, nil } diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go b/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go index a2ffdb452dc4..5824b46a2b48 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go @@ -34,14 +34,14 @@ var supportedLabels = map[MetadataLabel]bool{ func ValidateMetadataLabelsConfig(labels []MetadataLabel) error { labelsFound := map[MetadataLabel]bool{} for _, label := range labels { - if _, supported := supportedLabels[label]; supported { - if _, duplicate := labelsFound[label]; duplicate { - return fmt.Errorf("duplicate metadata label: %q", label) - } - labelsFound[label] = true - } else { + _, supported := supportedLabels[label] + if !supported { return fmt.Errorf("label %q is not supported", label) } + if _, duplicate := labelsFound[label]; duplicate { + return fmt.Errorf("duplicate metadata label: %q", label) + } + labelsFound[label] = true } return nil } diff --git a/receiver/saphanareceiver/queries.go b/receiver/saphanareceiver/queries.go index ec565dcea8f7..c87a718920a6 100644 --- a/receiver/saphanareceiver/queries.go +++ b/receiver/saphanareceiver/queries.go @@ -37,13 +37,12 @@ func (q *queryStat) collectStat(s *sapHanaScraper, m *monitoringQuery, now pcomm return fmt.Errorf("unable to parse metric for key %s: %w", q.key, err) } - if q.addMetricFunction != nil { - if err = q.addMetricFunction(mb, now, val, row); err != nil { - return fmt.Errorf("failed to record metric for key %s: %w", q.key, err) - } - } else { + if q.addMetricFunction == nil { return errors.New("incorrectly configured query, addMetricFunction must be provided") } + if err = q.addMetricFunction(mb, now, val, row); err != nil { + return fmt.Errorf("failed to record metric for key %s: %w", q.key, err) + } } return nil }