From 6d3e127caedd2556576151725eaf8cb6e47b1794 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 7 Aug 2025 20:52:16 +0200 Subject: [PATCH] chore: enable ptrToRefParam from go-critic (#7131) #### Description Enable and fixes [ptrToRefParam](https://go-critic.com/overview.html#ptrtorefparam) rule from go-critic Signed-off-by: Matthieu MOREL Co-authored-by: Tyler Yahn --- .golangci.yml | 1 - bridge/opentracing/bridge_test.go | 16 ++++++++-------- .../internal/aggregate/exponential_histogram.go | 8 ++++++-- sdk/metric/internal/aggregate/histogram.go | 8 ++++++-- sdk/metric/internal/aggregate/lastvalue.go | 16 ++++++++++++---- sdk/metric/internal/aggregate/sum.go | 16 ++++++++++++---- sdk/metric/pipeline_test.go | 4 +++- 7 files changed, 47 insertions(+), 22 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index efcbfaa99..3aff2d957 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -94,7 +94,6 @@ linters: - dupArg - hugeParam - importShadow - - ptrToRefParam - preferDecodeRune - rangeValCopy - unnamedResult diff --git a/bridge/opentracing/bridge_test.go b/bridge/opentracing/bridge_test.go index bab6b5a23..f27f6b22e 100644 --- a/bridge/opentracing/bridge_test.go +++ b/bridge/opentracing/bridge_test.go @@ -214,15 +214,15 @@ func (t *textMapCarrier) Keys() []string { // testTextMapReader only implemented opentracing.TextMapReader interface. type testTextMapReader struct { - m *map[string]string + m map[string]string } -func newTestTextMapReader(m *map[string]string) *testTextMapReader { +func newTestTextMapReader(m map[string]string) *testTextMapReader { return &testTextMapReader{m: m} } func (t *testTextMapReader) ForeachKey(handler func(key, val string) error) error { - for key, val := range *t.m { + for key, val := range t.m { if err := handler(key, val); err != nil { return err } @@ -233,15 +233,15 @@ func (t *testTextMapReader) ForeachKey(handler func(key, val string) error) erro // testTextMapWriter only implemented opentracing.TextMapWriter interface. type testTextMapWriter struct { - m *map[string]string + m map[string]string } -func newTestTextMapWriter(m *map[string]string) *testTextMapWriter { +func newTestTextMapWriter(m map[string]string) *testTextMapWriter { return &testTextMapWriter{m: m} } func (t *testTextMapWriter) Set(key, val string) { - (*t.m)[key] = val + t.m[key] = val } type samplable interface { @@ -290,9 +290,9 @@ func TestBridgeTracer_ExtractAndInject(t *testing.T) { { name: "support for opentracing.TextMapReader and opentracing.TextMapWriter,non-same instance", injectCarrierType: ot.TextMap, - injectCarrier: newTestTextMapWriter(&shareMap), + injectCarrier: newTestTextMapWriter(shareMap), extractCarrierType: ot.TextMap, - extractCarrier: newTestTextMapReader(&shareMap), + extractCarrier: newTestTextMapReader(shareMap), }, { name: "inject: format type is HTTPHeaders, but carrier is not HTTPHeadersCarrier", diff --git a/sdk/metric/internal/aggregate/exponential_histogram.go b/sdk/metric/internal/aggregate/exponential_histogram.go index b3266bd12..857eddf30 100644 --- a/sdk/metric/internal/aggregate/exponential_histogram.go +++ b/sdk/metric/internal/aggregate/exponential_histogram.go @@ -350,7 +350,9 @@ func (e *expoHistogram[N]) measure( v.res.Offer(ctx, value, droppedAttr) } -func (e *expoHistogram[N]) delta(dest *metricdata.Aggregation) int { +func (e *expoHistogram[N]) delta( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // If *dest is not a metricdata.ExponentialHistogram, memory reuse is missed. @@ -411,7 +413,9 @@ func (e *expoHistogram[N]) delta(dest *metricdata.Aggregation) int { return n } -func (e *expoHistogram[N]) cumulative(dest *metricdata.Aggregation) int { +func (e *expoHistogram[N]) cumulative( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // If *dest is not a metricdata.ExponentialHistogram, memory reuse is missed. diff --git a/sdk/metric/internal/aggregate/histogram.go b/sdk/metric/internal/aggregate/histogram.go index d3068484c..736287e73 100644 --- a/sdk/metric/internal/aggregate/histogram.go +++ b/sdk/metric/internal/aggregate/histogram.go @@ -140,7 +140,9 @@ type histogram[N int64 | float64] struct { start time.Time } -func (s *histogram[N]) delta(dest *metricdata.Aggregation) int { +func (s *histogram[N]) delta( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // If *dest is not a metricdata.Histogram, memory reuse is missed. In that @@ -190,7 +192,9 @@ func (s *histogram[N]) delta(dest *metricdata.Aggregation) int { return n } -func (s *histogram[N]) cumulative(dest *metricdata.Aggregation) int { +func (s *histogram[N]) cumulative( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // If *dest is not a metricdata.Histogram, memory reuse is missed. In that diff --git a/sdk/metric/internal/aggregate/lastvalue.go b/sdk/metric/internal/aggregate/lastvalue.go index 350ccebdc..4bbe624c7 100644 --- a/sdk/metric/internal/aggregate/lastvalue.go +++ b/sdk/metric/internal/aggregate/lastvalue.go @@ -55,7 +55,9 @@ func (s *lastValue[N]) measure(ctx context.Context, value N, fltrAttr attribute. s.values[attr.Equivalent()] = d } -func (s *lastValue[N]) delta(dest *metricdata.Aggregation) int { +func (s *lastValue[N]) delta( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of // the DataPoints is missed (better luck next time). @@ -75,7 +77,9 @@ func (s *lastValue[N]) delta(dest *metricdata.Aggregation) int { return n } -func (s *lastValue[N]) cumulative(dest *metricdata.Aggregation) int { +func (s *lastValue[N]) cumulative( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of // the DataPoints is missed (better luck next time). @@ -126,7 +130,9 @@ type precomputedLastValue[N int64 | float64] struct { *lastValue[N] } -func (s *precomputedLastValue[N]) delta(dest *metricdata.Aggregation) int { +func (s *precomputedLastValue[N]) delta( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of // the DataPoints is missed (better luck next time). @@ -146,7 +152,9 @@ func (s *precomputedLastValue[N]) delta(dest *metricdata.Aggregation) int { return n } -func (s *precomputedLastValue[N]) cumulative(dest *metricdata.Aggregation) int { +func (s *precomputedLastValue[N]) cumulative( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of // the DataPoints is missed (better luck next time). diff --git a/sdk/metric/internal/aggregate/sum.go b/sdk/metric/internal/aggregate/sum.go index 612cde432..1b4b2304c 100644 --- a/sdk/metric/internal/aggregate/sum.go +++ b/sdk/metric/internal/aggregate/sum.go @@ -70,7 +70,9 @@ type sum[N int64 | float64] struct { start time.Time } -func (s *sum[N]) delta(dest *metricdata.Aggregation) int { +func (s *sum[N]) delta( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // If *dest is not a metricdata.Sum, memory reuse is missed. In that case, @@ -105,7 +107,9 @@ func (s *sum[N]) delta(dest *metricdata.Aggregation) int { return n } -func (s *sum[N]) cumulative(dest *metricdata.Aggregation) int { +func (s *sum[N]) cumulative( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // If *dest is not a metricdata.Sum, memory reuse is missed. In that case, @@ -165,7 +169,9 @@ type precomputedSum[N int64 | float64] struct { reported map[attribute.Distinct]N } -func (s *precomputedSum[N]) delta(dest *metricdata.Aggregation) int { +func (s *precomputedSum[N]) delta( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() newReported := make(map[attribute.Distinct]N) @@ -206,7 +212,9 @@ func (s *precomputedSum[N]) delta(dest *metricdata.Aggregation) int { return n } -func (s *precomputedSum[N]) cumulative(dest *metricdata.Aggregation) int { +func (s *precomputedSum[N]) cumulative( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { t := now() // If *dest is not a metricdata.Sum, memory reuse is missed. In that case, diff --git a/sdk/metric/pipeline_test.go b/sdk/metric/pipeline_test.go index 25eeda58b..1f90a6431 100644 --- a/sdk/metric/pipeline_test.go +++ b/sdk/metric/pipeline_test.go @@ -32,7 +32,9 @@ import ( "go.opentelemetry.io/otel/trace" ) -func testSumAggregateOutput(dest *metricdata.Aggregation) int { +func testSumAggregateOutput( + dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface +) int { *dest = metricdata.Sum[int64]{ Temporality: metricdata.CumulativeTemporality, IsMonotonic: false,