1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00

Add support for the new implementation of for loop variables in go 1.22. (#993)

* Add support for the new behaviour of for loops in go 1.22.

Go 1.22 has changed the behaviour of for loops. Every iteration
makes new loop variables. It is now safe to take their addresses
because they are guaranteed to be unique. Similarly, it is now
safe to capture loop variables in functions.

* adds documentation for public function

---------

Co-authored-by: chavacava <salvadorcavadini+github@gmail.com>
This commit is contained in:
dominiquelefevre
2024-06-02 12:55:26 +03:00
committed by GitHub
parent bbe5eb7414
commit 4242f24f4d
7 changed files with 83 additions and 31 deletions

View File

@@ -7,13 +7,16 @@ import (
"go/types"
"sync"
goversion "github.com/hashicorp/go-version"
"github.com/mgechev/revive/internal/typeparams"
)
// Package represents a package in the project.
type Package struct {
fset *token.FileSet
files map[string]*File
fset *token.FileSet
files map[string]*File
goVersion *goversion.Version
typesPkg *types.Package
typesInfo *types.Info
@@ -29,6 +32,8 @@ var (
trueValue = 1
falseValue = 2
notSet = 3
go122 = goversion.Must(goversion.NewVersion("1.22"))
)
// Files return package's files.
@@ -188,3 +193,8 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) {
}
wg.Wait()
}
// IsAtLeastGo122 returns true if the Go version for this package is 1.22 or higher, false otherwise
func (p *Package) IsAtLeastGo122() bool {
return p.goVersion.GreaterThanOrEqual(go122)
}