From 409beeb18b4eb072d73f24b61bc7fa479e4ca33b Mon Sep 17 00:00:00 2001 From: Bob Idle <102661087+bobidle@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:52:04 +0100 Subject: [PATCH] golangci-lint: replace exportloopref with copyloopvar and remove the copy of the 'for' variables (#3591) * fix: replace golangci-lint exportloopref with copyloopvar * fix: remove copy of the 'for' variables --------- Co-authored-by: ahrav --- .github/workflows/lint.yml | 2 +- Makefile | 2 +- pkg/buffers/buffer/buffer_test.go | 1 - pkg/buffers/pool/pool_test.go | 2 -- pkg/common/http_test.go | 3 --- pkg/custom_detectors/custom_detectors.go | 1 - pkg/detectors/multi_part_credential_provider_test.go | 1 - pkg/engine/ahocorasick/ahocorasickcore_test.go | 2 -- pkg/sources/circleci/circleci.go | 1 - pkg/sources/gcs/gcs.go | 1 - pkg/sources/gcs/gcs_manager.go | 2 -- pkg/sources/github/github.go | 1 - pkg/sources/gitlab/gitlab.go | 2 +- pkg/sources/huggingface/huggingface.go | 1 - pkg/sources/source_manager.go | 1 - pkg/writers/buffer_writer/bufferwriter_test.go | 3 --- pkg/writers/buffered_file_writer/bufferedfilewriter_test.go | 6 ------ 17 files changed, 3 insertions(+), 29 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 09f00b37e..215c1bfc6 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -28,7 +28,7 @@ jobs: # working-directory: somedir # Optional: golangci-lint command line arguments. - args: --enable bodyclose --enable exportloopref --timeout 10m + args: --enable bodyclose --enable copyloopvar --timeout 10m # Optional: if set to true then the action don't cache or restore ~/go/pkg. # skip-pkg-cache: true diff --git a/Makefile b/Makefile index 43e9a58ab..a74a4f7f7 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ check: go vet $(shell go list ./... | grep -v /vendor/) lint: - golangci-lint run --enable bodyclose --enable exportloopref --out-format=colored-line-number --timeout 10m + golangci-lint run --enable bodyclose --enable copyloopvar --out-format=colored-line-number --timeout 10m test-failing: CGO_ENABLED=0 go test -timeout=5m $(shell go list ./... | grep -v /vendor/) | grep FAIL diff --git a/pkg/buffers/buffer/buffer_test.go b/pkg/buffers/buffer/buffer_test.go index bbf4987e2..f25189890 100644 --- a/pkg/buffers/buffer/buffer_test.go +++ b/pkg/buffers/buffer/buffer_test.go @@ -63,7 +63,6 @@ func TestBufferWrite(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() buf := &Buffer{Buffer: bytes.NewBuffer(make([]byte, 0, tc.initialCapacity))} diff --git a/pkg/buffers/pool/pool_test.go b/pkg/buffers/pool/pool_test.go index 3fb85a028..e322cb707 100644 --- a/pkg/buffers/pool/pool_test.go +++ b/pkg/buffers/pool/pool_test.go @@ -25,7 +25,6 @@ func TestNewBufferPool(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() pool := NewBufferPool(tc.size) @@ -62,7 +61,6 @@ func TestBufferPoolGetPut(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() pool := NewBufferPool(defaultBufferSize) diff --git a/pkg/common/http_test.go b/pkg/common/http_test.go index cc1711b42..d405583b6 100644 --- a/pkg/common/http_test.go +++ b/pkg/common/http_test.go @@ -64,7 +64,6 @@ func TestRetryableHTTPClientCheckRetry(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() var retryCount int @@ -137,7 +136,6 @@ func TestRetryableHTTPClientMaxRetry(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() var retryCount int @@ -202,7 +200,6 @@ func TestRetryableHTTPClientBackoff(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() diff --git a/pkg/custom_detectors/custom_detectors.go b/pkg/custom_detectors/custom_detectors.go index 96b72fa1d..2403661a2 100644 --- a/pkg/custom_detectors/custom_detectors.go +++ b/pkg/custom_detectors/custom_detectors.go @@ -91,7 +91,6 @@ func (c *CustomRegexWebhook) FromData(ctx context.Context, verify bool, data []b // Create result object and test for verification. resultsCh := make(chan detectors.Result, maxTotalMatches) for _, match := range matches { - match := match g.Go(func() error { return c.createResults(ctx, match, verify, resultsCh) }) diff --git a/pkg/detectors/multi_part_credential_provider_test.go b/pkg/detectors/multi_part_credential_provider_test.go index ddc9cb045..a27ce141b 100644 --- a/pkg/detectors/multi_part_credential_provider_test.go +++ b/pkg/detectors/multi_part_credential_provider_test.go @@ -25,7 +25,6 @@ func TestMultiPartCredentialProviders(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() diff --git a/pkg/engine/ahocorasick/ahocorasickcore_test.go b/pkg/engine/ahocorasick/ahocorasickcore_test.go index f2950dd1c..c33954a7d 100644 --- a/pkg/engine/ahocorasick/ahocorasickcore_test.go +++ b/pkg/engine/ahocorasick/ahocorasickcore_test.go @@ -401,8 +401,6 @@ func TestFindDetectorMatches(t *testing.T) { } for _, tc := range testCases { - tc := tc - t.Run(tc.name, func(t *testing.T) { t.Parallel() ac := NewAhoCorasickCore(tc.detectors, tc.opts...) diff --git a/pkg/sources/circleci/circleci.go b/pkg/sources/circleci/circleci.go index 11325a979..d89b4b231 100644 --- a/pkg/sources/circleci/circleci.go +++ b/pkg/sources/circleci/circleci.go @@ -91,7 +91,6 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ . scanErrs := sources.NewScanErrors() for _, proj := range projects { - proj := proj s.jobPool.Go(func() error { builds, err := s.buildsForProject(ctx, proj) if err != nil { diff --git a/pkg/sources/gcs/gcs.go b/pkg/sources/gcs/gcs.go index 7e9a05ced..f23c4f9ee 100644 --- a/pkg/sources/gcs/gcs.go +++ b/pkg/sources/gcs/gcs.go @@ -265,7 +265,6 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ . var wg sync.WaitGroup for obj := range objectCh { - obj := obj o, ok := obj.(object) if !ok { ctx.Logger().Error(fmt.Errorf("unexpected object type: %T", obj), "GCS source unexpected object type", "name", s.name) diff --git a/pkg/sources/gcs/gcs_manager.go b/pkg/sources/gcs/gcs_manager.go index fcbf5a709..33ede589d 100644 --- a/pkg/sources/gcs/gcs_manager.go +++ b/pkg/sources/gcs/gcs_manager.go @@ -397,7 +397,6 @@ func (g *gcsManager) enumerate(ctx context.Context, bkts []bucket) (*attributes, }(time.Now()) for _, bkt := range bkts { - bkt := bkt g.workerPool.Go(func() error { // List all the objects in the bucket and calculate attributes. g.setupBktHandle(&bkt) @@ -466,7 +465,6 @@ func (g *gcsManager) ListObjects(ctx context.Context) (chan io.Reader, error) { gcsErrs := sources.NewScanErrors() for _, bucket := range g.buckets { g.numBuckets++ - bucket := bucket g.workerPool.Go(func() error { objCh, errCh := g.listBucketObjects(ctx, &bucket) for { diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go index 4e2fac186..680a7b42f 100644 --- a/pkg/sources/github/github.go +++ b/pkg/sources/github/github.go @@ -629,7 +629,6 @@ func (s *Source) scan(ctx context.Context, reporter sources.ChunkReporter) error s.repos = reposToScan for i, repoURL := range s.repos { - i, repoURL := i, repoURL s.jobPool.Go(func() error { if common.IsDone(ctx) { return nil diff --git a/pkg/sources/gitlab/gitlab.go b/pkg/sources/gitlab/gitlab.go index 27c48add8..7eec5f86d 100644 --- a/pkg/sources/gitlab/gitlab.go +++ b/pkg/sources/gitlab/gitlab.go @@ -614,7 +614,7 @@ func (s *Source) scanRepos(ctx context.Context, chunksChan chan *sources.Chunk) scanErrs := sources.NewScanErrors() for i, repo := range s.repos { - i, repoURL := i, repo + repoURL := repo s.jobPool.Go(func() error { logger := ctx.Logger().WithValues("repo", repoURL) if common.IsDone(ctx) { diff --git a/pkg/sources/huggingface/huggingface.go b/pkg/sources/huggingface/huggingface.go index 135673eb9..462f33df6 100644 --- a/pkg/sources/huggingface/huggingface.go +++ b/pkg/sources/huggingface/huggingface.go @@ -520,7 +520,6 @@ func (s *Source) scanRepos(ctx context.Context, chunksChan chan *sources.Chunk, } for i, repoURL := range repos { - i, repoURL := i, repoURL s.jobPool.Go(func() error { if common.IsDone(ctx) { return nil diff --git a/pkg/sources/source_manager.go b/pkg/sources/source_manager.go index f3afe74ec..29ac4ce56 100644 --- a/pkg/sources/source_manager.go +++ b/pkg/sources/source_manager.go @@ -352,7 +352,6 @@ func (s *SourceManager) runWithUnits(ctx context.Context, source SourceUnitEnumC unitPool.SetLimit(s.concurrentUnits) } for unit := range unitReporter.unitCh { - unit := unit chunkReporter := &mgrChunkReporter{ unit: unit, chunkCh: make(chan *Chunk, defaultChannelSize), diff --git a/pkg/writers/buffer_writer/bufferwriter_test.go b/pkg/writers/buffer_writer/bufferwriter_test.go index fd6ce69aa..5ef9bccd5 100644 --- a/pkg/writers/buffer_writer/bufferwriter_test.go +++ b/pkg/writers/buffer_writer/bufferwriter_test.go @@ -36,7 +36,6 @@ func TestBufferWriterWrite(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() writer := New() @@ -72,7 +71,6 @@ func TestBufferWriterReadCloser(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() writer := New() @@ -136,7 +134,6 @@ func TestBufferWriterString(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() writer := New() diff --git a/pkg/writers/buffered_file_writer/bufferedfilewriter_test.go b/pkg/writers/buffered_file_writer/bufferedfilewriter_test.go index 3f4fad534..193c398f2 100644 --- a/pkg/writers/buffered_file_writer/bufferedfilewriter_test.go +++ b/pkg/writers/buffered_file_writer/bufferedfilewriter_test.go @@ -30,7 +30,6 @@ func TestBufferedFileWriterNewThreshold(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() writer := New(tc.options...) @@ -74,7 +73,6 @@ func TestBufferedFileWriterString(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() writer := New(WithThreshold(tc.threshold)) @@ -243,7 +241,6 @@ func TestBufferedFileWriterLen(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() writer := New() @@ -385,7 +382,6 @@ func TestBufferedFileWriterClose(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() writer := New(WithThreshold(threshold)) @@ -566,8 +562,6 @@ func TestBufferedFileWriter_ReadFrom(t *testing.T) { } for _, tc := range tests { - tc := tc - t.Run(tc.name, func(t *testing.T) { t.Parallel()