Skip to content

Commit

Permalink
maint: Use new linters in golangci-lint (#705)
Browse files Browse the repository at this point in the history
🍄
⬆️
  • Loading branch information
EduardGomezEscandell authored Mar 27, 2024
2 parents 0d826b2 + 7ac965c commit 1ec39be
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 17 deletions.
10 changes: 7 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
linters:
# linters to run in addition to default ones
enable:
- copyloopvar
- dupl
- dupword
- durationcheck
- errname
- errorlint
- exportloopref
- forbidigo
- forcetypeassert
- gci
- gocheckcompilerdirectives
- godot
- gofmt
- gosec
- intrange
- misspell
- nakedret
- nolintlint
Expand Down Expand Up @@ -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
Expand All @@ -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"
2 changes: 1 addition & 1 deletion common/grpc/contextidler/contextidler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion common/grpc/logconnections/logconnections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion common/grpc/logstreamer/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions common/testutils/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions windows-agent/internal/daemon/networking_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion windows-agent/internal/distros/worker/task_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion windows-agent/internal/distros/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion windows-agent/internal/distros/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions wsl-pro-service/internal/testutils/mock_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ec39be

Please sign in to comment.