revive icon indicating copy to clipboard operation
revive copied to clipboard

False positive datarace detection in Go 1.22

Open EldarKurbanov opened this issue 1 year ago • 0 comments

Describe the bug I see this linter warning in my project which uses Go 1.22 in go.mod: datarace: datarace: range value i is captured (by-reference) in goroutine (revive)

To Reproduce Steps to reproduce the behavior:

  1. Write this code:
package test

import (
	"log"
	"sync"
	"testing"
)

func Test(t *testing.T) {
	t.Parallel()

	clients := []string{"a", "b", "c", "d", "e"}
	wg := sync.WaitGroup{}

	const one = 1

	for i := range clients {
		wg.Add(one)

		go func() {
			defer wg.Done()

			log.Println(clients[i])
		}()
	}

	wg.Wait()
}

  1. Enable all rules in config:
enableAllRules = true
  1. Run revive:
revive -config conf.toml .

Expected behavior No warning for Go 1.22

Logs PS C:\Users\Eldar\Projects\test> revive -config conf.toml . a_test.go:23:24: datarace: range value i is captured (by-reference) in goroutine a_test.go:23:24: loop variable i captured by func literal

Desktop (please complete the following information):

  • OS: Windows 11 Pro 23H2
  • go version go1.22.0 windows/amd64
  • Revive version 1.3.7

EldarKurbanov avatar Mar 04 '24 10:03 EldarKurbanov