Skip to content

Commit 1f5b159

Browse files
authored
Use already enabled revive linter and add depguard (#2883)
* Refactor golangci-lint conf Order settings alphabetically. * Add revive settings to golangci conf * Check blank imports * Check bool-literal-in-expr * Check constant-logical-expr * Check context-as-argument * Check context-key-type * Check deep-exit * Check defer * Check dot-imports * Check duplicated-imports * Check early-return * Check empty-block * Check empty-lines * Check error-naming * Check error-return * Check error-strings * Check errorf * Stop ignoring context first arg in tests * Check exported comments * Check flag-parameter * Check identical branches * Check if-return * Check increment-decrement * Check indent-error-flow * Check deny list of go imports * Check import shadowing * Check package comments * Check range * Check range val in closure * Check range val address * Check redefines builtin id * Check string-format * Check struct tag * Check superfluous else * Check time equal * Check var naming * Check var declaration * Check unconditional recursion * Check unexported return * Check unhandled errors * Check unnecessary stmt * Check unnecessary break * Check waitgroup by value * Exclude deep-exit check in example*_test.go files
1 parent c5809aa commit 1f5b159

File tree

113 files changed

+767
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+767
-449
lines changed

.golangci.yml

Lines changed: 204 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ linters:
1010
# Specifically enable linters we want to use.
1111
enable:
1212
- deadcode
13+
- depguard
1314
- errcheck
15+
- godot
1416
- gofmt
1517
- goimports
1618
- gosimple
1719
- govet
18-
- godot
1920
- ineffassign
2021
- misspell
2122
- revive
@@ -25,30 +26,221 @@ linters:
2526
- unused
2627
- varcheck
2728

28-
2929
issues:
30+
# Maximum issues count per one linter.
31+
# Set to 0 to disable.
32+
# Default: 50
33+
# Setting to unlimited so the linter only is run once to debug all issues.
34+
max-issues-per-linter: 0
35+
# Maximum count of issues with the same text.
36+
# Set to 0 to disable.
37+
# Default: 3
38+
# Setting to unlimited so the linter only is run once to debug all issues.
39+
max-same-issues: 0
40+
# Excluding configuration per-path, per-linter, per-text and per-source.
3041
exclude-rules:
31-
# helpers in tests often (rightfully) pass a *testing.T as their first argument
32-
- path: _test\.go
33-
text: "context.Context should be the first parameter of a function"
42+
# TODO: Having appropriate comments for exported objects helps development,
43+
# even for objects in internal packages. Appropriate comments for all
44+
# exported objects should be added and this exclusion removed.
45+
- path: '.*internal/.*'
46+
text: "exported (method|function|type|const) (.+) should have comment or be unexported"
3447
linters:
3548
- revive
36-
# Yes, they are, but it's okay in a test
49+
# Yes, they are, but it's okay in a test.
3750
- path: _test\.go
3851
text: "exported func.*returns unexported type.*which can be annoying to use"
3952
linters:
4053
- revive
54+
# Example test functions should be treated like main.
55+
- path: example.*_test\.go
56+
text: "calls to (.+) only in main[(][)] or init[(][)] functions"
57+
linters:
58+
- revive
59+
include:
60+
# revive exported should have comment or be unexported.
61+
- EXC0012
62+
# revive package comment should be of the form ...
63+
- EXC0013
4164

4265
linters-settings:
43-
misspell:
44-
locale: US
45-
ignore-words:
46-
- cancelled
47-
goimports:
48-
local-prefixes: go.opentelemetry.io
66+
depguard:
67+
# Check the list against standard lib.
68+
# Default: false
69+
include-go-root: true
70+
# A list of packages for the list type specified.
71+
# Default: []
72+
packages:
73+
- "crypto/md5"
74+
- "crypto/sha1"
75+
- "crypto/**/pkix"
76+
ignore-file-rules:
77+
- "**/*_test.go"
78+
additional-guards:
79+
# Do not allow testing packages in non-test files.
80+
- list-type: denylist
81+
include-go-root: true
82+
packages:
83+
- testing
84+
- github.com/stretchr/testify
85+
ignore-file-rules:
86+
- "**/*_test.go"
87+
- "**/*test/*.go"
88+
- "**/internal/matchers/*.go"
4989
godot:
5090
exclude:
5191
# Exclude sentence fragments for lists.
5292
- '^[ ]*[-•]'
5393
# Exclude sentences prefixing a list.
5494
- ':$'
95+
goimports:
96+
local-prefixes: go.opentelemetry.io
97+
misspell:
98+
locale: US
99+
ignore-words:
100+
- cancelled
101+
revive:
102+
# Sets the default failure confidence.
103+
# This means that linting errors with less than 0.8 confidence will be ignored.
104+
# Default: 0.8
105+
confidence: 0.01
106+
rules:
107+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#blank-imports
108+
- name: blank-imports
109+
disabled: false
110+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#bool-literal-in-expr
111+
- name: bool-literal-in-expr
112+
disabled: false
113+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#constant-logical-expr
114+
- name: constant-logical-expr
115+
disabled: false
116+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#context-as-argument
117+
- name: context-as-argument
118+
disabled: false
119+
arguments:
120+
allowTypesBefore: "*testing.T"
121+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#context-keys-type
122+
- name: context-keys-type
123+
disabled: false
124+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#deep-exit
125+
- name: deep-exit
126+
disabled: false
127+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#defer
128+
- name: defer
129+
disabled: false
130+
arguments:
131+
- ["call-chain", "loop"]
132+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#dot-imports
133+
- name: dot-imports
134+
disabled: false
135+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#duplicated-imports
136+
- name: duplicated-imports
137+
disabled: false
138+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#early-return
139+
- name: early-return
140+
disabled: false
141+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#empty-block
142+
- name: empty-block
143+
disabled: false
144+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#empty-lines
145+
- name: empty-lines
146+
disabled: false
147+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#error-naming
148+
- name: error-naming
149+
disabled: false
150+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#error-return
151+
- name: error-return
152+
disabled: false
153+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#error-strings
154+
- name: error-strings
155+
disabled: false
156+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#errorf
157+
- name: errorf
158+
disabled: false
159+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#exported
160+
- name: exported
161+
disabled: false
162+
arguments:
163+
- "sayRepetitiveInsteadOfStutters"
164+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#flag-parameter
165+
- name: flag-parameter
166+
disabled: false
167+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#identical-branches
168+
- name: identical-branches
169+
disabled: false
170+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#if-return
171+
- name: if-return
172+
disabled: false
173+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#increment-decrement
174+
- name: increment-decrement
175+
disabled: false
176+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#indent-error-flow
177+
- name: indent-error-flow
178+
disabled: false
179+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#import-shadowing
180+
- name: import-shadowing
181+
disabled: false
182+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#package-comments
183+
- name: package-comments
184+
disabled: false
185+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#range
186+
- name: range
187+
disabled: false
188+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#range-val-in-closure
189+
- name: range-val-in-closure
190+
disabled: false
191+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#range-val-address
192+
- name: range-val-address
193+
disabled: false
194+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#redefines-builtin-id
195+
- name: redefines-builtin-id
196+
disabled: false
197+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#string-format
198+
- name: string-format
199+
disabled: false
200+
arguments:
201+
- - panic
202+
- '/^[^\n]*$/'
203+
- must not contain line breaks
204+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#struct-tag
205+
- name: struct-tag
206+
disabled: false
207+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#superfluous-else
208+
- name: superfluous-else
209+
disabled: false
210+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#time-equal
211+
- name: time-equal
212+
disabled: false
213+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#var-naming
214+
- name: var-naming
215+
disabled: false
216+
arguments:
217+
- ["ID"] # AllowList
218+
- ["Otel", "Aws", "Gcp"] # DenyList
219+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#var-declaration
220+
- name: var-declaration
221+
disabled: false
222+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unconditional-recursion
223+
- name: unconditional-recursion
224+
disabled: false
225+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unexported-return
226+
- name: unexported-return
227+
disabled: false
228+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unhandled-error
229+
- name: unhandled-error
230+
disabled: false
231+
arguments:
232+
- "fmt.Fprint"
233+
- "fmt.Fprintf"
234+
- "fmt.Fprintln"
235+
- "fmt.Print"
236+
- "fmt.Printf"
237+
- "fmt.Println"
238+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unnecessary-stmt
239+
- name: unnecessary-stmt
240+
disabled: false
241+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#useless-break
242+
- name: useless-break
243+
disabled: false
244+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#waitgroup-by-value
245+
- name: waitgroup-by-value
246+
disabled: false

attribute/encoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ func copyAndEscape(buf *bytes.Buffer, val string) {
133133
for _, ch := range val {
134134
switch ch {
135135
case '=', ',', escapeChar:
136-
buf.WriteRune(escapeChar)
136+
_, _ = buf.WriteRune(escapeChar)
137137
}
138-
buf.WriteRune(ch)
138+
_, _ = buf.WriteRune(ch)
139139
}
140140
}
141141

attribute/iterator_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func TestEmptyIterator(t *testing.T) {
5656
}
5757

5858
func TestMergedIterator(t *testing.T) {
59-
6059
type inputs struct {
6160
name string
6261
keys1 []string

attribute/set.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func EmptySet() *Set {
7171
return emptySet
7272
}
7373

74-
// reflect abbreviates reflect.ValueOf.
75-
func (d Distinct) reflect() reflect.Value {
74+
// reflectValue abbreviates reflect.ValueOf(d).
75+
func (d Distinct) reflectValue() reflect.Value {
7676
return reflect.ValueOf(d.iface)
7777
}
7878

@@ -86,15 +86,15 @@ func (l *Set) Len() int {
8686
if l == nil || !l.equivalent.Valid() {
8787
return 0
8888
}
89-
return l.equivalent.reflect().Len()
89+
return l.equivalent.reflectValue().Len()
9090
}
9191

9292
// Get returns the KeyValue at ordered position idx in this set.
9393
func (l *Set) Get(idx int) (KeyValue, bool) {
9494
if l == nil {
9595
return KeyValue{}, false
9696
}
97-
value := l.equivalent.reflect()
97+
value := l.equivalent.reflectValue()
9898

9999
if idx >= 0 && idx < value.Len() {
100100
// Note: The Go compiler successfully avoids an allocation for
@@ -110,7 +110,7 @@ func (l *Set) Value(k Key) (Value, bool) {
110110
if l == nil {
111111
return Value{}, false
112112
}
113-
rValue := l.equivalent.reflect()
113+
rValue := l.equivalent.reflectValue()
114114
vlen := rValue.Len()
115115

116116
idx := sort.Search(vlen, func(idx int) bool {

attribute/value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
//go:generate stringer -type=Type
2626

2727
// Type describes the type of the data Value holds.
28-
type Type int
28+
type Type int // nolint: revive // redefines builtin Type.
2929

3030
// Value represents the value part in key-value pairs.
3131
type Value struct {

baggage/baggage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type Property struct {
6868
hasData bool
6969
}
7070

71+
// NewKeyProperty returns a new Property for key.
72+
//
73+
// If key is invalid, an error will be returned.
7174
func NewKeyProperty(key string) (Property, error) {
7275
if !keyRe.MatchString(key) {
7376
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)
@@ -77,6 +80,9 @@ func NewKeyProperty(key string) (Property, error) {
7780
return p, nil
7881
}
7982

83+
// NewKeyValueProperty returns a new Property for key with value.
84+
//
85+
// If key or value are invalid, an error will be returned.
8086
func NewKeyValueProperty(key, value string) (Property, error) {
8187
if !keyRe.MatchString(key) {
8288
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)

baggage/baggage_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func TestBaggageMembers(t *testing.T) {
612612
},
613613
}
614614

615-
baggage := Baggage{list: baggage.List{
615+
bag := Baggage{list: baggage.List{
616616
"foo": {
617617
Value: "1",
618618
Properties: []baggage.Property{
@@ -626,13 +626,13 @@ func TestBaggageMembers(t *testing.T) {
626626
},
627627
}}
628628

629-
assert.ElementsMatch(t, members, baggage.Members())
629+
assert.ElementsMatch(t, members, bag.Members())
630630
}
631631

632632
func TestBaggageMember(t *testing.T) {
633-
baggage := Baggage{list: baggage.List{"foo": {Value: "1"}}}
634-
assert.Equal(t, Member{key: "foo", value: "1"}, baggage.Member("foo"))
635-
assert.Equal(t, Member{}, baggage.Member("bar"))
633+
bag := Baggage{list: baggage.List{"foo": {Value: "1"}}}
634+
assert.Equal(t, Member{key: "foo", value: "1"}, bag.Member("foo"))
635+
assert.Equal(t, Member{}, bag.Member("bar"))
636636
}
637637

638638
func TestMemberKey(t *testing.T) {

bridge/opencensus/aggregation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func TestHistogramAggregation(t *testing.T) {
276276
if output.Kind() != aggregation.HistogramKind {
277277
t.Errorf("recordAggregationsFromPoints(%v) = %v, want %v", input, output.Kind(), aggregation.HistogramKind)
278278
}
279-
if end != now {
279+
if !end.Equal(now) {
280280
t.Errorf("recordAggregationsFromPoints(%v).end() = %v, want %v", input, end, now)
281281
}
282282
distAgg, ok := output.(aggregation.Histogram)

bridge/opencensus/exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"go.opentelemetry.io/otel/sdk/resource"
3838
)
3939

40-
var errConversion = errors.New("Unable to convert from OpenCensus to OpenTelemetry")
40+
var errConversion = errors.New("unable to convert from OpenCensus to OpenTelemetry")
4141

4242
// NewMetricExporter returns an OpenCensus exporter that exports to an
4343
// OpenTelemetry exporter.

bridge/opencensus/test/bridge_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ func TestToFromContext(t *testing.T) {
137137
// Get the opentelemetry span using the OpenCensus FromContext, and end it
138138
otSpan2 := octrace.FromContext(ctx)
139139
defer otSpan2.End()
140-
141140
}()
142141

143142
spans := sr.Ended()

0 commit comments

Comments
 (0)