1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

refactor: modernize code (#7850)

Enable the
[`modernize`](https://golangci-lint.run/docs/linters/configuration/#modernize)
linter in the golangci-lint configuration and fix the reported issues by
running `golangci-lint run --enable-only modernize --fix`. The
`modernize` linter is internally the same as [gopls
modernize](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize).

Similar to #7089.
This commit is contained in:
Oleksandr Redko
2026-01-30 19:15:17 +02:00
committed by GitHub
parent fd5d030c0a
commit 3264bf171b
25 changed files with 101 additions and 138 deletions
+3 -3
View File
@@ -53,12 +53,12 @@ func TestHashKVsEquality(t *testing.T) {
result = append(result, testcase{hashKVs(nil), nil})
for _, key := range keys {
for i := 0; i < len(keyVals); i++ {
for i := range keyVals {
kvs := []KeyValue{keyVals[i](key)}
hash := hashKVs(kvs)
result = append(result, testcase{hash, kvs})
for j := 0; j < len(keyVals); j++ {
for j := range keyVals {
kvs := []KeyValue{
keyVals[i](key),
keyVals[j](key),
@@ -66,7 +66,7 @@ func TestHashKVsEquality(t *testing.T) {
hash := hashKVs(kvs)
result = append(result, testcase{hash, kvs})
for k := 0; k < len(keyVals); k++ {
for k := range keyVals {
kvs := []KeyValue{
keyVals[i](key),
keyVals[j](key),
+4 -8
View File
@@ -13,32 +13,28 @@ import (
// BoolSliceValue converts a bool slice into an array with same elements as slice.
func BoolSliceValue(v []bool) any {
var zero bool
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[bool]())).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
// Int64SliceValue converts an int64 slice into an array with same elements as slice.
func Int64SliceValue(v []int64) any {
var zero int64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[int64]())).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
// Float64SliceValue converts a float64 slice into an array with same elements as slice.
func Float64SliceValue(v []float64) any {
var zero float64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[float64]())).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
// StringSliceValue converts a string slice into an array with same elements as slice.
func StringSliceValue(v []string) any {
var zero string
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[string]())).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
+9 -10
View File
@@ -101,8 +101,8 @@ var sync any
func BenchmarkBoolSliceValue(b *testing.B) {
b.ReportAllocs()
s := []bool{true, false, true, false}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for b.Loop() {
sync = BoolSliceValue(s)
}
}
@@ -110,8 +110,8 @@ func BenchmarkBoolSliceValue(b *testing.B) {
func BenchmarkInt64SliceValue(b *testing.B) {
b.ReportAllocs()
s := []int64{1, 2, 3, 4}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for b.Loop() {
sync = Int64SliceValue(s)
}
}
@@ -119,8 +119,8 @@ func BenchmarkInt64SliceValue(b *testing.B) {
func BenchmarkFloat64SliceValue(b *testing.B) {
b.ReportAllocs()
s := []float64{1.2, 3.4, 5.6, 7.8}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for b.Loop() {
sync = Float64SliceValue(s)
}
}
@@ -128,8 +128,8 @@ func BenchmarkFloat64SliceValue(b *testing.B) {
func BenchmarkStringSliceValue(b *testing.B) {
b.ReportAllocs()
s := []string{"a", "b", "c", "d"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for b.Loop() {
sync = StringSliceValue(s)
}
}
@@ -137,9 +137,8 @@ func BenchmarkStringSliceValue(b *testing.B) {
func BenchmarkAsFloat64Slice(b *testing.B) {
b.ReportAllocs()
var in any = [2]float64{1, 2.3}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
sync = AsFloat64Slice(in)
}
}
+12 -12
View File
@@ -143,9 +143,9 @@ func BenchmarkUint64KB(b *testing.B) {
func BenchmarkUint64(b *testing.B) {
h := New()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
h = h.Uint64(uint64(i))
}
}
@@ -153,45 +153,45 @@ func BenchmarkUint64(b *testing.B) {
func BenchmarkString(b *testing.B) {
h := New()
str := "benchmark_string_value"
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
h = h.String(str)
}
}
func BenchmarkBool(b *testing.B) {
h := New()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
h = h.Bool(i%2 == 0)
}
}
func BenchmarkFloat64(b *testing.B) {
h := New()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
h = h.Float64(float64(i) * 3.14159)
}
}
func BenchmarkInt64(b *testing.B) {
h := New()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
h = h.Int64(int64(i))
}
}
func BenchmarkSum64(b *testing.B) {
h := New().String("key").Uint64(42).Bool(true)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = h.Sum64()
}
}
+1 -1
View File
@@ -58,7 +58,7 @@ func isComparable[T comparable](t T) T { return t }
var (
// keyValueType is used in computeDistinctReflect.
keyValueType = reflect.TypeOf(KeyValue{})
keyValueType = reflect.TypeFor[KeyValue]()
// emptyHash is the hash of an empty set.
emptyHash = xxhash.New().Sum64()
+3 -3
View File
@@ -314,7 +314,7 @@ func TestLookup(t *testing.T) {
}
func TestZeroSetExportedMethodsNoPanic(t *testing.T) {
rType := reflect.TypeOf((*attribute.Set)(nil))
rType := reflect.TypeFor[*attribute.Set]()
rVal := reflect.ValueOf(&attribute.Set{})
for n := 0; n < rType.NumMethod(); n++ {
mType := rType.Method(n)
@@ -552,12 +552,12 @@ func generateStringAttrsWithSize(keyLen, valueLen int) []attribute.KeyValue {
valueBase := ""
// Build key base string
for i := 0; i < keyLen; i++ {
for i := range keyLen {
keyBase += string(rune('a' + i%26))
}
// Build value base string
for i := 0; i < valueLen; i++ {
for i := range valueLen {
valueBase += string(rune('0' + i%10))
}
+1 -2
View File
@@ -66,8 +66,7 @@ func IntValue(v int) Value {
// IntSliceValue creates an INTSLICE Value.
func IntSliceValue(v []int) Value {
var int64Val int64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val)))
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[int64]()))
for i, val := range v {
cp.Elem().Index(i).SetInt(int64(val))
}