Skip to content

Commit cad1676

Browse files
committed
Upgrade golangci-lint, more linters
Introduces new linters, upgrade golangci-lint to version (v1.63.4)
1 parent 647b978 commit cad1676

Some content is hidden

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

67 files changed

+1601
-1001
lines changed

.golangci.yml

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,32 @@ linters-settings:
2525
- ^os.Exit$
2626
- ^panic$
2727
- ^print(ln)?$
28+
varnamelen:
29+
max-distance: 12
30+
min-name-length: 2
31+
ignore-type-assert-ok: true
32+
ignore-map-index-ok: true
33+
ignore-chan-recv-ok: true
34+
ignore-decls:
35+
- i int
36+
- n int
37+
- w io.Writer
38+
- r io.Reader
39+
- b []byte
2840

2941
linters:
3042
enable:
3143
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
3244
- bidichk # Checks for dangerous unicode character sequences
3345
- bodyclose # checks whether HTTP response body is closed successfully
46+
- containedctx # containedctx is a linter that detects struct contained context.Context field
3447
- contextcheck # check the function whether use a non-inherited context
48+
- cyclop # checks function and package cyclomatic complexity
3549
- decorder # check declaration order and count of types, constants, variables and functions
3650
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
3751
- dupl # Tool for code clone detection
3852
- durationcheck # check for two durations multiplied together
53+
- err113 # Golang linter to check the errors handling expressions
3954
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
4055
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted.
4156
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.
@@ -46,66 +61,64 @@ linters:
4661
- forcetypeassert # finds forced type assertions
4762
- gci # Gci control golang package import order and make it always deterministic.
4863
- gochecknoglobals # Checks that no globals are present in Go code
49-
- gochecknoinits # Checks that no init functions are present in Go code
5064
- gocognit # Computes and checks the cognitive complexity of functions
5165
- goconst # Finds repeated strings that could be replaced by a constant
5266
- gocritic # The most opinionated Go source code linter
67+
- gocyclo # Computes and checks the cyclomatic complexity of functions
68+
- godot # Check if comments end in a period
5369
- godox # Tool for detection of FIXME, TODO and other comment keywords
54-
- err113 # Golang linter to check the errors handling expressions
5570
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
5671
- gofumpt # Gofumpt checks whether code was gofumpt-ed.
5772
- goheader # Checks is file header matches to pattern
5873
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
5974
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
60-
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
6175
- goprintffuncname # Checks that printf-like functions are named with `f` at the end
6276
- gosec # Inspects source code for security problems
6377
- gosimple # Linter for Go source code that specializes in simplifying a code
6478
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
6579
- grouper # An analyzer to analyze expression groups.
6680
- importas # Enforces consistent import aliases
6781
- ineffassign # Detects when assignments to existing variables are not used
82+
- lll # Reports long lines
83+
- maintidx # maintidx measures the maintainability index of each function.
84+
- makezero # Finds slice declarations with non-zero initial length
6885
- misspell # Finds commonly misspelled English words in comments
86+
- nakedret # Finds naked returns in functions greater than a specified function length
87+
- nestif # Reports deeply nested if statements
6988
- nilerr # Finds the code that returns nil even if it checks that the error is not nil.
7089
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value.
90+
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
7191
- noctx # noctx finds sending http request without context.Context
7292
- predeclared # find code that shadows one of Go's predeclared identifiers
7393
- revive # golint replacement, finds style mistakes
7494
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
7595
- stylecheck # Stylecheck is a replacement for golint
7696
- tagliatelle # Checks the struct tags.
7797
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
78-
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
98+
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
7999
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
80100
- unconvert # Remove unnecessary type conversions
81101
- unparam # Reports unused function parameters
82102
- unused # Checks Go code for unused constants, variables, functions and types
103+
- varnamelen # checks that the length of a variable's name matches its scope
83104
- wastedassign # wastedassign finds wasted assignment statements
84105
- whitespace # Tool for detection of leading and trailing whitespace
85106
disable:
86107
- depguard # Go linter that checks if package imports are in a list of acceptable packages
87-
- containedctx # containedctx is a linter that detects struct contained context.Context field
88-
- cyclop # checks function and package cyclomatic complexity
89108
- funlen # Tool for detection of long functions
90-
- gocyclo # Computes and checks the cyclomatic complexity of functions
91-
- godot # Check if comments end in a period
92-
- gomnd # An analyzer to detect magic numbers.
109+
- gochecknoinits # Checks that no init functions are present in Go code
110+
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
111+
- interfacebloat # A linter that checks length of interface.
93112
- ireturn # Accept Interfaces, Return Concrete Types
94-
- lll # Reports long lines
95-
- maintidx # maintidx measures the maintainability index of each function.
96-
- makezero # Finds slice declarations with non-zero initial length
97-
- nakedret # Finds naked returns in functions greater than a specified function length
98-
- nestif # Reports deeply nested if statements
99-
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
113+
- mnd # An analyzer to detect magic numbers
100114
- nolintlint # Reports ill-formed or insufficient nolint directives
101115
- paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test
102116
- prealloc # Finds slice declarations that could potentially be preallocated
103117
- promlinter # Check Prometheus metrics naming via promlint
104118
- rowserrcheck # checks whether Err of rows is checked successfully
105119
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed.
106120
- testpackage # linter that makes you use a separate _test package
107-
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
108-
- varnamelen # checks that the length of a variable's name matches its scope
121+
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
109122
- wrapcheck # Checks that errors returned from external packages are wrapped
110123
- wsl # Whitespace Linter - Forces you to use empty lines!
111124

active_tcp.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ type activeTCPConn struct {
2121
closed int32
2222
}
2323

24-
func newActiveTCPConn(ctx context.Context, localAddress string, remoteAddress netip.AddrPort, log logging.LeveledLogger) (a *activeTCPConn) {
24+
func newActiveTCPConn(
25+
ctx context.Context,
26+
localAddress string,
27+
remoteAddress netip.AddrPort,
28+
log logging.LeveledLogger,
29+
) (a *activeTCPConn) {
2530
a = &activeTCPConn{
2631
readBuffer: packetio.NewBuffer(),
2732
writeBuffer: packetio.NewBuffer(),
@@ -31,7 +36,8 @@ func newActiveTCPConn(ctx context.Context, localAddress string, remoteAddress ne
3136
if err != nil {
3237
atomic.StoreInt32(&a.closed, 1)
3338
log.Infof("Failed to dial TCP address %s: %v", remoteAddress, err)
34-
return
39+
40+
return a
3541
}
3642
a.localAddr.Store(laddr)
3743

@@ -46,6 +52,7 @@ func newActiveTCPConn(ctx context.Context, localAddress string, remoteAddress ne
4652
conn, err := dialer.DialContext(ctx, "tcp", remoteAddress.String())
4753
if err != nil {
4854
log.Infof("Failed to dial TCP address %s: %v", remoteAddress, err)
55+
4956
return
5057
}
5158
a.remoteAddr.Store(conn.RemoteAddr())
@@ -57,11 +64,13 @@ func newActiveTCPConn(ctx context.Context, localAddress string, remoteAddress ne
5764
n, err := readStreamingPacket(conn, buff)
5865
if err != nil {
5966
log.Infof("Failed to read streaming packet: %s", err)
67+
6068
break
6169
}
6270

6371
if _, err := a.readBuffer.Write(buff[:n]); err != nil {
6472
log.Infof("Failed to write to buffer: %s", err)
73+
6574
break
6675
}
6776
}
@@ -73,11 +82,13 @@ func newActiveTCPConn(ctx context.Context, localAddress string, remoteAddress ne
7382
n, err := a.writeBuffer.Read(buff)
7483
if err != nil {
7584
log.Infof("Failed to read from buffer: %s", err)
85+
7686
break
7787
}
7888

7989
if _, err = writeStreamingPacket(conn, buff[:n]); err != nil {
8090
log.Infof("Failed to write streaming packet: %s", err)
91+
8192
break
8293
}
8394
}
@@ -98,6 +109,7 @@ func (a *activeTCPConn) ReadFrom(buff []byte) (n int, srcAddr net.Addr, err erro
98109
n, err = a.readBuffer.Read(buff)
99110
// RemoteAddr is assuredly set *after* we can read from the buffer
100111
srcAddr = a.RemoteAddr()
112+
101113
return
102114
}
103115

@@ -113,6 +125,7 @@ func (a *activeTCPConn) Close() error {
113125
atomic.StoreInt32(&a.closed, 1)
114126
_ = a.readBuffer.Close()
115127
_ = a.writeBuffer.Close()
128+
116129
return nil
117130
}
118131

active_tcp_test.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ import (
2121
)
2222

2323
func getLocalIPAddress(t *testing.T, networkType NetworkType) netip.Addr {
24+
t.Helper()
25+
2426
net, err := stdnet.NewNet()
2527
require.NoError(t, err)
2628
_, localAddrs, err := localInterfaces(net, problematicNetworkInterfaces, nil, []NetworkType{networkType}, false)
2729
require.NoError(t, err)
2830
require.NotEmpty(t, localAddrs)
31+
2932
return localAddrs[0]
3033
}
3134

3235
func ipv6Available(t *testing.T) bool {
36+
t.Helper()
37+
3338
net, err := stdnet.NewNet()
3439
require.NoError(t, err)
3540
_, localAddrs, err := localInterfaces(net, problematicNetworkInterfaces, nil, []NetworkType{NetworkTypeTCP6}, false)
3641
require.NoError(t, err)
42+
3743
return len(localAddrs) > 0
3844
}
3945

@@ -89,14 +95,14 @@ func TestActiveTCP(t *testing.T) {
8995

9096
for _, testCase := range testCases {
9197
t.Run(testCase.name, func(t *testing.T) {
92-
r := require.New(t)
98+
req := require.New(t)
9399

94100
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
95101
IP: testCase.listenIPAddress.AsSlice(),
96102
Port: listenPort,
97103
Zone: testCase.listenIPAddress.Zone(),
98104
})
99-
r.NoError(err)
105+
req.NoError(err)
100106
defer func() {
101107
_ = listener.Close()
102108
}()
@@ -113,7 +119,7 @@ func TestActiveTCP(t *testing.T) {
113119
_ = tcpMux.Close()
114120
}()
115121

116-
r.NotNil(tcpMux.LocalAddr(), "tcpMux.LocalAddr() is nil")
122+
req.NotNil(tcpMux.LocalAddr(), "tcpMux.LocalAddr() is nil")
117123

118124
hostAcceptanceMinWait := 100 * time.Millisecond
119125
cfg := &AgentConfig{
@@ -128,8 +134,8 @@ func TestActiveTCP(t *testing.T) {
128134
cfg.MulticastDNSMode = MulticastDNSModeQueryAndGather
129135
}
130136
passiveAgent, err := NewAgent(cfg)
131-
r.NoError(err)
132-
r.NotNil(passiveAgent)
137+
req.NoError(err)
138+
req.NotNil(passiveAgent)
133139

134140
activeAgent, err := NewAgent(&AgentConfig{
135141
CandidateTypes: []CandidateType{CandidateTypeHost},
@@ -138,44 +144,44 @@ func TestActiveTCP(t *testing.T) {
138144
HostAcceptanceMinWait: &hostAcceptanceMinWait,
139145
InterfaceFilter: problematicNetworkInterfaces,
140146
})
141-
r.NoError(err)
142-
r.NotNil(activeAgent)
147+
req.NoError(err)
148+
req.NotNil(activeAgent)
143149

144150
passiveAgentConn, activeAgenConn := connect(passiveAgent, activeAgent)
145-
r.NotNil(passiveAgentConn)
146-
r.NotNil(activeAgenConn)
151+
req.NotNil(passiveAgentConn)
152+
req.NotNil(activeAgenConn)
147153

148154
defer func() {
149-
r.NoError(activeAgenConn.Close())
150-
r.NoError(passiveAgentConn.Close())
155+
req.NoError(activeAgenConn.Close())
156+
req.NoError(passiveAgentConn.Close())
151157
}()
152158

153159
pair := passiveAgent.getSelectedPair()
154-
r.NotNil(pair)
155-
r.Equal(testCase.selectedPairNetworkType, pair.Local.NetworkType().NetworkShort())
160+
req.NotNil(pair)
161+
req.Equal(testCase.selectedPairNetworkType, pair.Local.NetworkType().NetworkShort())
156162

157163
foo := []byte("foo")
158164
_, err = passiveAgentConn.Write(foo)
159-
r.NoError(err)
165+
req.NoError(err)
160166

161167
buffer := make([]byte, 1024)
162168
n, err := activeAgenConn.Read(buffer)
163-
r.NoError(err)
164-
r.Equal(foo, buffer[:n])
169+
req.NoError(err)
170+
req.Equal(foo, buffer[:n])
165171

166172
bar := []byte("bar")
167173
_, err = activeAgenConn.Write(bar)
168-
r.NoError(err)
174+
req.NoError(err)
169175

170176
n, err = passiveAgentConn.Read(buffer)
171-
r.NoError(err)
172-
r.Equal(bar, buffer[:n])
177+
req.NoError(err)
178+
req.Equal(bar, buffer[:n])
173179
})
174180
}
175181
}
176182

177-
// Assert that Active TCP connectivity isn't established inside
178-
// the main thread of the Agent
183+
// Assert that Active TCP connectivity isn't established inside.
184+
// the main thread of the Agent.
179185
func TestActiveTCP_NonBlocking(t *testing.T) {
180186
defer test.CheckRoutines(t)()
181187

@@ -219,7 +225,7 @@ func TestActiveTCP_NonBlocking(t *testing.T) {
219225
<-isConnected
220226
}
221227

222-
// Assert that we ignore remote TCP candidates when running a UDP Only Agent
228+
// Assert that we ignore remote TCP candidates when running a UDP Only Agent.
223229
func TestActiveTCP_Respect_NetworkTypes(t *testing.T) {
224230
defer test.CheckRoutines(t)()
225231
defer test.TimeOut(time.Second * 5).Stop()
@@ -271,7 +277,9 @@ func TestActiveTCP_Respect_NetworkTypes(t *testing.T) {
271277
})
272278
require.NoError(t, err)
273279

274-
invalidCandidate, err := UnmarshalCandidate(fmt.Sprintf("1052353102 1 tcp 1675624447 127.0.0.1 %s typ host tcptype passive", port))
280+
invalidCandidate, err := UnmarshalCandidate(
281+
fmt.Sprintf("1052353102 1 tcp 1675624447 127.0.0.1 %s typ host tcptype passive", port),
282+
)
275283
require.NoError(t, err)
276284
require.NoError(t, aAgent.AddRemoteCandidate(invalidCandidate))
277285
require.NoError(t, bAgent.AddRemoteCandidate(invalidCandidate))

0 commit comments

Comments
 (0)