Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 17a0a64

Browse files
chore: Remove redundant loop captures (#62264)
Go 1.22 changes loop variables to have more sensible semantics where the variable is not reused across iterations by the codegen, so we can simplify a bunch of code working around that counterintuitive behavior.
1 parent 40da412 commit 17a0a64

File tree

14 files changed

+5
-65
lines changed

14 files changed

+5
-65
lines changed

BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ nogo(
293293
"//conditions:default": [
294294
"//dev/linters/bodyclose",
295295
"//dev/linters/depguard",
296-
"//dev/linters/exportloopref",
297296
"//dev/linters/forbidigo",
298297
"//dev/linters/gocheckcompilerdirectives",
299298
"//dev/linters/gocritic",

cmd/frontend/graphqlbackend/codeintel.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ func NewCodeIntelResolver(resolver *resolverstubs.Resolver) *Resolver {
2424

2525
func (r *Resolver) NodeResolvers() map[string]NodeByIDFunc {
2626
m := map[string]NodeByIDFunc{}
27-
for name, f := range r.Resolver.NodeResolvers() {
28-
resolverFunc := f // do not capture loop variable
27+
for name, resolverFunc := range r.Resolver.NodeResolvers() {
2928
m[name] = func(ctx context.Context, id graphql.ID) (Node, error) {
3029
return resolverFunc(ctx, id)
3130
}

dev/linters/exportloopref/BUILD.bazel

-12
This file was deleted.

dev/linters/exportloopref/exportloopref.go

-8
This file was deleted.

dev/linters/go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ require (
1010
github.com/gobwas/glob v0.2.3
1111
github.com/gordonklaus/ineffassign v0.0.0-20230107090616-13ace0543b28
1212
github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db
13-
github.com/kyoh86/exportloopref v0.1.11
1413
github.com/sourcegraph/sourcegraph/lib v0.0.0-20231122233253-1f857814717c
1514
github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e
1615
golang.org/x/tools v0.15.0

dev/linters/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
105105
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
106106
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
107107
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
108-
github.com/kyoh86/exportloopref v0.1.11 h1:1Z0bcmTypkL3Q4k+IDHMWTcnCliEZcaPiIe0/ymEyhQ=
109-
github.com/kyoh86/exportloopref v0.1.11/go.mod h1:qkV4UF1zGl6EkF1ox8L5t9SwyeBAZ3qLMd6up458uqA=
110108
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
111109
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
112110
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=

dev/sg/internal/generate/golang/golang.go

-3
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ func runGoGenerateOnPaths(ctx context.Context, pkgPaths []string, progressBar bo
220220
)
221221

222222
for _, pkgPath := range pkgPaths {
223-
// Do not capture loop variable in goroutine below
224-
pkgPath := pkgPath
225-
226223
p.Go(func(ctx context.Context) error {
227224
file := filepath.Base(pkgPath) // *.go
228225
directory := filepath.Dir(pkgPath)

internal/codeintel/policies/matcher.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,9 @@ func (m *Matcher) matchCommitsOnBranch(ctx context.Context, context matcherConte
243243
continue policyLoop
244244
}
245245

246-
// Don't capture loop variable pointers
247-
localPolicyID := policyID
248-
commitDate := commitDate
249-
250246
context.commitMap[commit] = append(context.commitMap[commit], PolicyMatch{
251247
Name: branchName,
252-
PolicyID: &localPolicyID,
248+
PolicyID: &policyID,
253249
PolicyDuration: policyDuration,
254250
CommittedAt: commitDate,
255251
})
@@ -279,10 +275,9 @@ func (m *Matcher) matchCommitPolicies(ctx context.Context, context matcherContex
279275
continue
280276
}
281277

282-
id := policy.ID // avoid a reference to the loop variable
283278
context.commitMap[policy.Pattern] = append(context.commitMap[policy.Pattern], PolicyMatch{
284279
Name: string(commit.ID),
285-
PolicyID: &id,
280+
PolicyID: &policy.ID,
286281
PolicyDuration: policyDuration,
287282
CommittedAt: commit.Committer.Date,
288283
})

internal/database/migration/definition/definition.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ func (ds *Definitions) traverse(targetIDs []int, next func(definition Definition
321321
}
322322

323323
for _, id := range next(definition) {
324-
nodeID := n.id // avoid referencing the loop variable
325-
newFrontier = append(newFrontier, node{id, &nodeID})
324+
newFrontier = append(newFrontier, node{id, &n.id})
326325
}
327326
}
328327

internal/embeddings/similarity_search.go

-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ func (index *EmbeddingIndex) SimilaritySearch(
107107
if len(rowsPerWorker) > 1 {
108108
var wg conc.WaitGroup
109109
for workerIdx := range len(rowsPerWorker) {
110-
// Capture the loop variable value so we can use it in the closure below.
111-
workerIdx := workerIdx
112110
wg.Go(func() {
113111
heaps[workerIdx] = index.partialSimilaritySearch(query, numResults, rowsPerWorker[workerIdx], opts)
114112
})

internal/updatecheck/handler.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,7 @@ func reserializeNewCodeIntelUsage(payload json.RawMessage) (json.RawMessage, err
515515
}
516516

517517
countsByLanguage := make([]jsonCodeIntelRepositoryCountsByLanguage, 0, len(codeIntelUsage.CountsByLanguage))
518-
for language, counts := range codeIntelUsage.CountsByLanguage {
519-
// note: do not capture loop var by ref
520-
languageID := language
521-
518+
for languageID, counts := range codeIntelUsage.CountsByLanguage {
522519
countsByLanguage = append(countsByLanguage, jsonCodeIntelRepositoryCountsByLanguage{
523520
LanguageID: &languageID,
524521
NumRepositoriesWithUploadRecords: counts.NumRepositoriesWithUploadRecords,
@@ -545,9 +542,6 @@ func reserializeNewCodeIntelUsage(payload json.RawMessage) (json.RawMessage, err
545542

546543
languageRequests := make([]jsonLanguageRequest, 0, len(codeIntelUsage.LanguageRequests))
547544
for _, request := range codeIntelUsage.LanguageRequests {
548-
// note: do not capture loop var by ref
549-
request := request
550-
551545
languageRequests = append(languageRequests, jsonLanguageRequest{
552546
LanguageID: &request.LanguageID,
553547
NumRequests: &request.NumRequests,

lib/batches/env/var.go

-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func (v *variable) UnmarshalJSON(data []byte) error {
5151

5252
for k, value := range kv {
5353
v.name = k
54-
//nolint:exportloopref // There should only be one iteration, so the value of `value` should not change
5554
v.value = &value
5655
}
5756

@@ -78,7 +77,6 @@ func (v *variable) UnmarshalYAML(unmarshal func(any) error) error {
7877

7978
for k, value := range kv {
8079
v.name = k
81-
//nolint:exportloopref // There should only be one iteration, so the value of `value` should not change
8280
v.value = &value
8381
}
8482

linter_deps.bzl

-7
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ def linter_dependencies():
148148
sum = "h1:6WHiuFL9FNjg8RljAaT7FNUuKDbvMqS1i5cr2OE2sLQ=",
149149
)
150150

151-
go_repository(
152-
name = "com_github_kyoh86_exportloopref",
153-
importpath = "github.com/kyoh86/exportloopref",
154-
version = "v0.1.11",
155-
sum = "h1:1Z0bcmTypkL3Q4k+IDHMWTcnCliEZcaPiIe0/ymEyhQ=",
156-
)
157-
158151
go_repository(
159152
name = "co_honnef_go_tools",
160153
importpath = "honnef.co/go/tools",

nogo_config.json

-9
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,5 @@
4646
".*_pb\\.go$": "ignore protobuff generated code",
4747
"dev/buildchecker/slack\\.go$": "ignore false positive"
4848
}
49-
},
50-
"exportloopref": {
51-
"exclude_files": {
52-
"lib/batches/env/var\\.go$": "false positive",
53-
"rules_go.*/.*": "ignore rules_go working directory",
54-
"external/.*": "no need to vet third party code",
55-
".*_generated\\.go$": "ignore generated code",
56-
".*_pb\\.go$": "ignore protobuff generated code"
57-
}
5849
}
5950
}

0 commit comments

Comments
 (0)