diff --git a/.golangci.yaml b/.golangci.yaml index 5db5e1aef..f39112024 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -4,12 +4,12 @@ linters: # linters to run in addition to default ones enable: + - copyloopvar - dupl - dupword - durationcheck - errname - errorlint - - exportloopref - forbidigo - forcetypeassert - gci @@ -17,6 +17,7 @@ linters: - godot - gofmt - gosec + - intrange - misspell - nakedret - nolintlint @@ -51,8 +52,11 @@ issues: - non-wrapping format verb for fmt.Errorf. Use `%w` to format errors # We want named parameters even if unused, as they help better document the function - unused-parameter - # Sometimes it is more readable it do a `if err:=a(); err != nil` tha simpy `return a()` + # Sometimes it is more readable it do a `if err:=a(); err != nil` than simpy `return a()` - if-return + # Outdated warining, it only applies to Go < 1.22 + # G601: Implicit memory aliasing in for loop. + - G601 nolintlint: require-explanation: true @@ -70,4 +74,4 @@ linters-settings: staticcheck: # Should be better for it to be autodetected # https://github.com/golangci/golangci-lint/issues/2234 - go: "1.21.5" + go: "1.22.0" diff --git a/common/grpc/contextidler/contextidler_test.go b/common/grpc/contextidler/contextidler_test.go index af8885c27..2cf15ac28 100644 --- a/common/grpc/contextidler/contextidler_test.go +++ b/common/grpc/contextidler/contextidler_test.go @@ -31,7 +31,7 @@ func TestActiveConnection(t *testing.T) { require.Equal(t, "something", s.recvMsgCalledWith, "Streamer RecvMsg called with expected message") // Ping multiple times, each ping is less than timeout, but total time is more than the timeout - for i := 0; i < 5; i++ { + for range 5 { time.Sleep(30 * time.Millisecond) require.NoError(t, c.RecvMsg("something"), "RecvMsg with no error") } diff --git a/common/grpc/logconnections/logconnections.go b/common/grpc/logconnections/logconnections.go index 02b87d04b..4e895fa1c 100644 --- a/common/grpc/logconnections/logconnections.go +++ b/common/grpc/logconnections/logconnections.go @@ -49,7 +49,7 @@ func (ss loggedServerStream) RecvMsg(m interface{}) error { err := ss.ServerStream.RecvMsg(m) v := reflect.ValueOf(m).Elem() t := v.Type() - for i := 0; i < t.NumField(); i++ { + for i := range t.NumField() { n := t.Field(i).Name // Only print exported fields val := v.FieldByName(n) diff --git a/common/grpc/logstreamer/caller.go b/common/grpc/logstreamer/caller.go index b2aed7a18..7c2d1801d 100644 --- a/common/grpc/logstreamer/caller.go +++ b/common/grpc/logstreamer/caller.go @@ -32,7 +32,7 @@ func getCaller() *runtime.Frame { _ = runtime.Callers(0, pcs) // dynamic get the package name and the minimum caller depth - for i := 0; i < maximumCallerDepth; i++ { + for i := range maximumCallerDepth { funcName := runtime.FuncForPC(pcs[i]).Name() if strings.Contains(funcName, "getCaller") { thisPackage = getPackageName(funcName) diff --git a/common/testutils/set_test.go b/common/testutils/set_test.go index da0239134..ecf593e8e 100644 --- a/common/testutils/set_test.go +++ b/common/testutils/set_test.go @@ -16,7 +16,7 @@ func TestSet(t *testing.T) { // Concurrently add items to it var wg sync.WaitGroup - for i := 0; i < testSize; i++ { + for i := range testSize { wg.Add(1) go func(i int) { set.Set(i) @@ -26,14 +26,14 @@ func TestSet(t *testing.T) { wg.Wait() // Check all items are eventually added - for i := 0; i < testSize; i++ { + for i := range testSize { require.True(t, set.Has(i), "Value %d should have been added to the set", i) } require.Equal(t, testSize, set.Len(), "Set should have all items in it") // Concurrently remove items wg = sync.WaitGroup{} - for i := 0; i < testSize; i++ { + for i := range testSize { wg.Add(1) go func(i int) { set.Unset(i) @@ -43,7 +43,7 @@ func TestSet(t *testing.T) { wg.Wait() // Check all items are eventually removed - for i := 0; i < testSize; i++ { + for i := range testSize { require.False(t, set.Has(i), "Value %d should have been removed from the set", i) } } diff --git a/windows-agent/internal/daemon/networking_windows.go b/windows-agent/internal/daemon/networking_windows.go index 0418b842d..ff44ad3cd 100644 --- a/windows-agent/internal/daemon/networking_windows.go +++ b/windows-agent/internal/daemon/networking_windows.go @@ -12,7 +12,7 @@ import ( "golang.org/x/sys/windows" ) -// Does nothing, exists so we can compile the tests without mocks. +//nolint:unused // Does nothing; it exists so we can compile the tests without mocks. var wslIPErr bool func getWslIP() (net.IP, error) { @@ -66,7 +66,7 @@ func getAdaptersAddresses() (head *windows.IpAdapterAddresses, err error) { // Win32 API docs recommend a buff size of 15KB. buff.resizeBytes(15 * kilobyte) - for i := 0; i < 10; i++ { + for range 10 { size := buff.byteCount() err := windows.GetAdaptersAddresses(family, flags, 0, &buff.data[0], &size) if errors.Is(err, windows.ERROR_BUFFER_OVERFLOW) { diff --git a/windows-agent/internal/distros/worker/task_queue.go b/windows-agent/internal/distros/worker/task_queue.go index cc1664bf5..74f2b9bf5 100644 --- a/windows-agent/internal/distros/worker/task_queue.go +++ b/windows-agent/internal/distros/worker/task_queue.go @@ -207,7 +207,7 @@ func removeIf(array []task.Task, predicate func(task.Task) bool) []task.Task { // 0 j i end // j := 0 - for i := 0; i < len(array); i++ { + for i := range array { if predicate(array[i]) { // Rejected continue diff --git a/windows-agent/internal/distros/worker/worker.go b/windows-agent/internal/distros/worker/worker.go index 4a40682bf..ab10624bc 100644 --- a/windows-agent/internal/distros/worker/worker.go +++ b/windows-agent/internal/distros/worker/worker.go @@ -270,7 +270,7 @@ func (w *Worker) processSingleTask(ctx context.Context, t task.Task) error { func (w *Worker) waitForActiveConnection(ctx context.Context) (conn Connection, err error) { log.Debugf(ctx, "Distro %q: ensuring active connection.", w.distro.Name()) - for i := 0; i < 5; i++ { + for range 5 { conn, err = func() (Connection, error) { // Potentially restart distro if it was stopped for some reason if err := w.distro.LockAwake(); err != nil { diff --git a/windows-agent/internal/distros/worker/worker_test.go b/windows-agent/internal/distros/worker/worker_test.go index 5c73ff20c..056f33b55 100644 --- a/windows-agent/internal/distros/worker/worker_test.go +++ b/windows-agent/internal/distros/worker/worker_test.go @@ -326,7 +326,7 @@ func TestSetConnection(t *testing.T) { // GetClient twice and ensure we ping the same service const conn1calls = 2 - for i := 0; i < conn1calls; i++ { + for i := range conn1calls { c := w.Connection() require.NotNil(t, c, "client should be non-nil after setting a connection") err = c.SendProAttachment("123") diff --git a/wsl-pro-service/internal/testutils/mock_agent.go b/wsl-pro-service/internal/testutils/mock_agent.go index b4ca866c0..f0ee6f675 100644 --- a/wsl-pro-service/internal/testutils/mock_agent.go +++ b/wsl-pro-service/internal/testutils/mock_agent.go @@ -140,8 +140,7 @@ func (ch *channel[Recv, Send, Stream]) History() []*Recv { out := make([]*Recv, len(ch.recvHistory)) for i, rcv := range ch.recvHistory { - cpy := rcv - out[i] = &cpy + out[i] = &rcv } return out