mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 15:12:02 +00:00
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.
This commit is contained in:
parent
40da412d2c
commit
17a0a6461c
@ -293,7 +293,6 @@ nogo(
|
||||
"//conditions:default": [
|
||||
"//dev/linters/bodyclose",
|
||||
"//dev/linters/depguard",
|
||||
"//dev/linters/exportloopref",
|
||||
"//dev/linters/forbidigo",
|
||||
"//dev/linters/gocheckcompilerdirectives",
|
||||
"//dev/linters/gocritic",
|
||||
|
||||
@ -24,8 +24,7 @@ func NewCodeIntelResolver(resolver *resolverstubs.Resolver) *Resolver {
|
||||
|
||||
func (r *Resolver) NodeResolvers() map[string]NodeByIDFunc {
|
||||
m := map[string]NodeByIDFunc{}
|
||||
for name, f := range r.Resolver.NodeResolvers() {
|
||||
resolverFunc := f // do not capture loop variable
|
||||
for name, resolverFunc := range r.Resolver.NodeResolvers() {
|
||||
m[name] = func(ctx context.Context, id graphql.ID) (Node, error) {
|
||||
return resolverFunc(ctx, id)
|
||||
}
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "exportloopref",
|
||||
srcs = ["exportloopref.go"],
|
||||
importpath = "github.com/sourcegraph/sourcegraph/dev/linters/exportloopref",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//dev/linters/nolint",
|
||||
"@com_github_kyoh86_exportloopref//:go_default_library",
|
||||
],
|
||||
)
|
||||
@ -1,8 +0,0 @@
|
||||
package exportloopref
|
||||
|
||||
import (
|
||||
"github.com/kyoh86/exportloopref"
|
||||
"github.com/sourcegraph/sourcegraph/dev/linters/nolint"
|
||||
)
|
||||
|
||||
var Analyzer = nolint.Wrap(exportloopref.Analyzer)
|
||||
@ -10,7 +10,6 @@ require (
|
||||
github.com/gobwas/glob v0.2.3
|
||||
github.com/gordonklaus/ineffassign v0.0.0-20230107090616-13ace0543b28
|
||||
github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db
|
||||
github.com/kyoh86/exportloopref v0.1.11
|
||||
github.com/sourcegraph/sourcegraph/lib v0.0.0-20231122233253-1f857814717c
|
||||
github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e
|
||||
golang.org/x/tools v0.15.0
|
||||
|
||||
@ -105,8 +105,6 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/kyoh86/exportloopref v0.1.11 h1:1Z0bcmTypkL3Q4k+IDHMWTcnCliEZcaPiIe0/ymEyhQ=
|
||||
github.com/kyoh86/exportloopref v0.1.11/go.mod h1:qkV4UF1zGl6EkF1ox8L5t9SwyeBAZ3qLMd6up458uqA=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
|
||||
@ -220,9 +220,6 @@ func runGoGenerateOnPaths(ctx context.Context, pkgPaths []string, progressBar bo
|
||||
)
|
||||
|
||||
for _, pkgPath := range pkgPaths {
|
||||
// Do not capture loop variable in goroutine below
|
||||
pkgPath := pkgPath
|
||||
|
||||
p.Go(func(ctx context.Context) error {
|
||||
file := filepath.Base(pkgPath) // *.go
|
||||
directory := filepath.Dir(pkgPath)
|
||||
|
||||
@ -243,13 +243,9 @@ func (m *Matcher) matchCommitsOnBranch(ctx context.Context, context matcherConte
|
||||
continue policyLoop
|
||||
}
|
||||
|
||||
// Don't capture loop variable pointers
|
||||
localPolicyID := policyID
|
||||
commitDate := commitDate
|
||||
|
||||
context.commitMap[commit] = append(context.commitMap[commit], PolicyMatch{
|
||||
Name: branchName,
|
||||
PolicyID: &localPolicyID,
|
||||
PolicyID: &policyID,
|
||||
PolicyDuration: policyDuration,
|
||||
CommittedAt: commitDate,
|
||||
})
|
||||
@ -279,10 +275,9 @@ func (m *Matcher) matchCommitPolicies(ctx context.Context, context matcherContex
|
||||
continue
|
||||
}
|
||||
|
||||
id := policy.ID // avoid a reference to the loop variable
|
||||
context.commitMap[policy.Pattern] = append(context.commitMap[policy.Pattern], PolicyMatch{
|
||||
Name: string(commit.ID),
|
||||
PolicyID: &id,
|
||||
PolicyID: &policy.ID,
|
||||
PolicyDuration: policyDuration,
|
||||
CommittedAt: commit.Committer.Date,
|
||||
})
|
||||
|
||||
@ -321,8 +321,7 @@ func (ds *Definitions) traverse(targetIDs []int, next func(definition Definition
|
||||
}
|
||||
|
||||
for _, id := range next(definition) {
|
||||
nodeID := n.id // avoid referencing the loop variable
|
||||
newFrontier = append(newFrontier, node{id, &nodeID})
|
||||
newFrontier = append(newFrontier, node{id, &n.id})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -107,8 +107,6 @@ func (index *EmbeddingIndex) SimilaritySearch(
|
||||
if len(rowsPerWorker) > 1 {
|
||||
var wg conc.WaitGroup
|
||||
for workerIdx := range len(rowsPerWorker) {
|
||||
// Capture the loop variable value so we can use it in the closure below.
|
||||
workerIdx := workerIdx
|
||||
wg.Go(func() {
|
||||
heaps[workerIdx] = index.partialSimilaritySearch(query, numResults, rowsPerWorker[workerIdx], opts)
|
||||
})
|
||||
|
||||
@ -515,10 +515,7 @@ func reserializeNewCodeIntelUsage(payload json.RawMessage) (json.RawMessage, err
|
||||
}
|
||||
|
||||
countsByLanguage := make([]jsonCodeIntelRepositoryCountsByLanguage, 0, len(codeIntelUsage.CountsByLanguage))
|
||||
for language, counts := range codeIntelUsage.CountsByLanguage {
|
||||
// note: do not capture loop var by ref
|
||||
languageID := language
|
||||
|
||||
for languageID, counts := range codeIntelUsage.CountsByLanguage {
|
||||
countsByLanguage = append(countsByLanguage, jsonCodeIntelRepositoryCountsByLanguage{
|
||||
LanguageID: &languageID,
|
||||
NumRepositoriesWithUploadRecords: counts.NumRepositoriesWithUploadRecords,
|
||||
@ -545,9 +542,6 @@ func reserializeNewCodeIntelUsage(payload json.RawMessage) (json.RawMessage, err
|
||||
|
||||
languageRequests := make([]jsonLanguageRequest, 0, len(codeIntelUsage.LanguageRequests))
|
||||
for _, request := range codeIntelUsage.LanguageRequests {
|
||||
// note: do not capture loop var by ref
|
||||
request := request
|
||||
|
||||
languageRequests = append(languageRequests, jsonLanguageRequest{
|
||||
LanguageID: &request.LanguageID,
|
||||
NumRequests: &request.NumRequests,
|
||||
|
||||
2
lib/batches/env/var.go
vendored
2
lib/batches/env/var.go
vendored
@ -51,7 +51,6 @@ func (v *variable) UnmarshalJSON(data []byte) error {
|
||||
|
||||
for k, value := range kv {
|
||||
v.name = k
|
||||
//nolint:exportloopref // There should only be one iteration, so the value of `value` should not change
|
||||
v.value = &value
|
||||
}
|
||||
|
||||
@ -78,7 +77,6 @@ func (v *variable) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
|
||||
for k, value := range kv {
|
||||
v.name = k
|
||||
//nolint:exportloopref // There should only be one iteration, so the value of `value` should not change
|
||||
v.value = &value
|
||||
}
|
||||
|
||||
|
||||
@ -148,13 +148,6 @@ def linter_dependencies():
|
||||
sum = "h1:6WHiuFL9FNjg8RljAaT7FNUuKDbvMqS1i5cr2OE2sLQ=",
|
||||
)
|
||||
|
||||
go_repository(
|
||||
name = "com_github_kyoh86_exportloopref",
|
||||
importpath = "github.com/kyoh86/exportloopref",
|
||||
version = "v0.1.11",
|
||||
sum = "h1:1Z0bcmTypkL3Q4k+IDHMWTcnCliEZcaPiIe0/ymEyhQ=",
|
||||
)
|
||||
|
||||
go_repository(
|
||||
name = "co_honnef_go_tools",
|
||||
importpath = "honnef.co/go/tools",
|
||||
|
||||
@ -46,14 +46,5 @@
|
||||
".*_pb\\.go$": "ignore protobuff generated code",
|
||||
"dev/buildchecker/slack\\.go$": "ignore false positive"
|
||||
}
|
||||
},
|
||||
"exportloopref": {
|
||||
"exclude_files": {
|
||||
"lib/batches/env/var\\.go$": "false positive",
|
||||
"rules_go.*/.*": "ignore rules_go working directory",
|
||||
"external/.*": "no need to vet third party code",
|
||||
".*_generated\\.go$": "ignore generated code",
|
||||
".*_pb\\.go$": "ignore protobuff generated code"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user