Skip to content

Commit 9823913

Browse files
mmorel-35MrAlias
andauthored
chore: enable gocritic linter (#7095)
#### Description Enable and fixes several rules from [gocritic](https://golangci-lint.run/usage/linters/#gocritic) linter --------- Signed-off-by: Matthieu MOREL <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent 8955578 commit 9823913

File tree

37 files changed

+158
-146
lines changed

37 files changed

+158
-146
lines changed

.golangci.yml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ linters:
1010
- depguard
1111
- errcheck
1212
- errorlint
13+
- gocritic
1314
- godot
1415
- gosec
1516
- govet
@@ -86,6 +87,20 @@ linters:
8687
deny:
8788
- pkg: go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal
8889
desc: Do not use cross-module internal packages.
90+
gocritic:
91+
disabled-checks:
92+
- appendAssign
93+
- commentedOutCode
94+
- dupArg
95+
- hugeParam
96+
- importShadow
97+
- paramTypeCombine
98+
- ptrToRefParam
99+
- preferDecodeRune
100+
- rangeValCopy
101+
- unnamedResult
102+
- whyNoLint
103+
enable-all: true
89104
godot:
90105
exclude:
91106
# Exclude links.
@@ -225,10 +240,6 @@ linters:
225240
- linters:
226241
- gosec
227242
text: 'G402: TLS MinVersion too low.'
228-
paths:
229-
- third_party$
230-
- builtin$
231-
- examples$
232243
issues:
233244
max-issues-per-linter: 0
234245
max-same-issues: 0
@@ -245,7 +256,3 @@ formatters:
245256
max-len: 120
246257
exclusions:
247258
generated: lax
248-
paths:
249-
- third_party$
250-
- builtin$
251-
- examples$

attribute/encoder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ func (d *defaultAttrEncoder) Encode(iter Iterator) string {
9696
for iter.Next() {
9797
i, keyValue := iter.IndexedAttribute()
9898
if i > 0 {
99-
_, _ = buf.WriteRune(',')
99+
_ = buf.WriteByte(',')
100100
}
101101
copyAndEscape(buf, string(keyValue.Key))
102102

103-
_, _ = buf.WriteRune('=')
103+
_ = buf.WriteByte('=')
104104

105105
if keyValue.Value.Type() == STRING {
106106
copyAndEscape(buf, keyValue.Value.AsString())
@@ -122,7 +122,7 @@ func copyAndEscape(buf *bytes.Buffer, val string) {
122122
for _, ch := range val {
123123
switch ch {
124124
case '=', ',', escapeChar:
125-
_, _ = buf.WriteRune(escapeChar)
125+
_ = buf.WriteByte(escapeChar)
126126
}
127127
_, _ = buf.WriteRune(ch)
128128
}

attribute/filter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Filter func(KeyValue) bool
1515
//
1616
// If keys is empty a deny-all filter is returned.
1717
func NewAllowKeysFilter(keys ...Key) Filter {
18-
if len(keys) <= 0 {
18+
if len(keys) == 0 {
1919
return func(kv KeyValue) bool { return false }
2020
}
2121

@@ -34,7 +34,7 @@ func NewAllowKeysFilter(keys ...Key) Filter {
3434
//
3535
// If keys is empty an allow-all filter is returned.
3636
func NewDenyKeysFilter(keys ...Key) Filter {
37-
if len(keys) <= 0 {
37+
if len(keys) == 0 {
3838
return func(kv KeyValue) bool { return true }
3939
}
4040

baggage/baggage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ var safeKeyCharset = [utf8.RuneSelf]bool{
812812
// validateBaggageName checks if the string is a valid OpenTelemetry Baggage name.
813813
// Baggage name is a valid, non-empty UTF-8 string.
814814
func validateBaggageName(s string) bool {
815-
if len(s) == 0 {
815+
if s == "" {
816816
return false
817817
}
818818

@@ -828,7 +828,7 @@ func validateBaggageValue(s string) bool {
828828

829829
// validateKey checks if the string is a valid W3C Baggage key.
830830
func validateKey(s string) bool {
831-
if len(s) == 0 {
831+
if s == "" {
832832
return false
833833
}
834834

bridge/opencensus/trace_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,10 @@ func TestOTelSpanContextToOC(t *testing.T) {
134134
gotTraceState = strings.Join(gotTraceStateEntries, ",")
135135
}
136136
assert.Equal(t, expectedTraceState, gotTraceState, "Tracestate should preserve entries")
137-
} else {
137+
} else if got.Tracestate != nil {
138138
// For empty tracestate cases, ensure the field is properly handled
139-
if got.Tracestate != nil {
140-
entries := got.Tracestate.Entries()
141-
assert.Empty(t, entries, "Empty tracestate should result in empty entries")
142-
}
139+
entries := got.Tracestate.Entries()
140+
assert.Empty(t, entries, "Empty tracestate should result in empty entries")
143141
}
144142
})
145143
}

bridge/opentracing/bridge_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var (
135135
type testTextMapPropagator struct{}
136136

137137
func (t testTextMapPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
138-
carrier.Set(testHeader, strings.Join([]string{traceID.String(), spanID.String()}, ":"))
138+
carrier.Set(testHeader, traceID.String()+":"+spanID.String())
139139

140140
// Test for panic
141141
_ = carrier.Get("test")

exporters/otlp/otlplog/otlploggrpc/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ func newGRPCDialOptions(cfg config) []grpc.DialOption {
8686
dialOpts = append(dialOpts, grpc.WithDefaultServiceConfig(cfg.serviceConfig.Value))
8787
}
8888
// Prioritize GRPCCredentials over Insecure (passing both is an error).
89-
if cfg.gRPCCredentials.Value != nil {
89+
switch {
90+
case cfg.gRPCCredentials.Value != nil:
9091
dialOpts = append(dialOpts, grpc.WithTransportCredentials(cfg.gRPCCredentials.Value))
91-
} else if cfg.insecure.Value {
92+
case cfg.insecure.Value:
9293
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
93-
} else {
94+
default:
9495
// Default to using the host's root CA.
9596
dialOpts = append(dialOpts, grpc.WithTransportCredentials(
9697
credentials.NewTLS(nil),

exporters/otlp/otlplog/otlploggrpc/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func loadCertificates(certPath, keyPath string) ([]tls.Certificate, error) {
563563
func insecureFromScheme(prev setting[bool], scheme string) setting[bool] {
564564
if scheme == "https" {
565565
return newSetting(false)
566-
} else if len(scheme) > 0 {
566+
} else if scheme != "" {
567567
return newSetting(true)
568568
}
569569

exporters/otlp/otlplog/otlploggrpc/config_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,8 @@ func assertTLSConfig(t *testing.T, want, got setting[*tls.Config]) {
525525

526526
if want.Value.RootCAs == nil {
527527
assert.Nil(t, got.Value.RootCAs, "*tls.Config.RootCAs")
528-
} else {
529-
if assert.NotNil(t, got.Value.RootCAs, "RootCAs") {
530-
assert.True(t, want.Value.RootCAs.Equal(got.Value.RootCAs), "RootCAs equal")
531-
}
528+
} else if assert.NotNil(t, got.Value.RootCAs, "RootCAs") {
529+
assert.True(t, want.Value.RootCAs.Equal(got.Value.RootCAs), "RootCAs equal")
532530
}
533531
assert.Equal(t, want.Value.Certificates, got.Value.Certificates, "Certificates")
534532
}

exporters/otlp/otlplog/otlploghttp/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (c *httpClient) uploadLogs(ctx context.Context, data []*logpb.ResourceLogs)
200200
return err
201201
}
202202
respStr := strings.TrimSpace(respData.String())
203-
if len(respStr) == 0 {
203+
if respStr == "" {
204204
respStr = "(empty)"
205205
}
206206
bodyErr := fmt.Errorf("body: %s", respStr)
@@ -232,7 +232,7 @@ func (c *httpClient) newRequest(ctx context.Context, body []byte) (request, erro
232232

233233
switch c.compression {
234234
case NoCompression:
235-
r.ContentLength = (int64)(len(body))
235+
r.ContentLength = int64(len(body))
236236
req.bodyReader = bodyReader(body)
237237
case GzipCompression:
238238
// Ensure the content length is not used.

0 commit comments

Comments
 (0)