Skip to content

refactor: replace IsAtLeastGo1* functions by IsAtLeastGoVersion #1274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions lint/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ var (
trueValue = 1
falseValue = 2

go115 = goversion.Must(goversion.NewVersion("1.15"))
go121 = goversion.Must(goversion.NewVersion("1.21"))
go122 = goversion.Must(goversion.NewVersion("1.22"))
go124 = goversion.Must(goversion.NewVersion("1.24"))
// Go115 is a constant representing the Go version 1.15
Go115 = goversion.Must(goversion.NewVersion("1.15"))
// Go121 is a constant representing the Go version 1.21
Go121 = goversion.Must(goversion.NewVersion("1.21"))
// Go122 is a constant representing the Go version 1.22
Go122 = goversion.Must(goversion.NewVersion("1.22"))
// Go124 is a constant representing the Go version 1.24
Go124 = goversion.Must(goversion.NewVersion("1.24"))
)

// Files return package's files.
Expand Down Expand Up @@ -196,24 +200,9 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error
return eg.Wait()
}

// IsAtLeastGo115 returns true if the Go version for this package is 1.15 or higher, false otherwise
func (p *Package) IsAtLeastGo115() bool {
return p.goVersion.GreaterThanOrEqual(go115)
}

// IsAtLeastGo121 returns true if the Go version for this package is 1.21 or higher, false otherwise
func (p *Package) IsAtLeastGo121() bool {
return p.goVersion.GreaterThanOrEqual(go121)
}

// 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)
}

// IsAtLeastGo124 returns true if the Go version for this package is 1.24 or higher, false otherwise
func (p *Package) IsAtLeastGo124() bool {
return p.goVersion.GreaterThanOrEqual(go124)
// IsAtLeastGoVersion returns true if the Go version for this package is v or higher, false otherwise
func (p *Package) IsAtLeastGoVersion(v *goversion.Version) bool {
return p.goVersion.GreaterThanOrEqual(v)
}

func getSortableMethodFlagForFunction(fn *ast.FuncDecl) sortableMethodsFlags {
Expand Down
2 changes: 1 addition & 1 deletion rule/datarace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type DataRaceRule struct{}

// Apply applies the rule to given file.
func (r *DataRaceRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
isGo122 := file.Pkg.IsAtLeastGo122()
isGo122 := file.Pkg.IsAtLeastGoVersion(lint.Go122)
var failures []lint.Failure
for _, decl := range file.AST.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
Expand Down
2 changes: 1 addition & 1 deletion rule/range_val_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type RangeValAddress struct{}
func (*RangeValAddress) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

if file.Pkg.IsAtLeastGo122() {
if file.Pkg.IsAtLeastGoVersion(lint.Go122) {
return failures
}

Expand Down
2 changes: 1 addition & 1 deletion rule/range_val_in_closure.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type RangeValInClosureRule struct{}
func (*RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

if file.Pkg.IsAtLeastGo122() {
if file.Pkg.IsAtLeastGoVersion(lint.Go122) {
return failures
}

Expand Down
2 changes: 1 addition & 1 deletion rule/redefines_builtin_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.F
astFile := file.AST

builtFuncs := maps.Clone(builtFunctions)
if file.Pkg.IsAtLeastGo121() {
if file.Pkg.IsAtLeastGoVersion(lint.Go121) {
maps.Copy(builtFuncs, builtFunctionsAfterGo121)
}
w := &lintRedefinesBuiltinID{
Expand Down
2 changes: 1 addition & 1 deletion rule/redundant_test_main_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type RedundantTestMainExitRule struct{}
func (*RedundantTestMainExitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

if !file.IsTest() || !file.Pkg.IsAtLeastGo115() {
if !file.IsTest() || !file.Pkg.IsAtLeastGoVersion(lint.Go115) {
// skip analysis for non-test files or for Go versions before 1.15
return failures
}
Expand Down
2 changes: 1 addition & 1 deletion rule/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *StructTagRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure
w := lintStructTagRule{
onFailure: onFailure,
userDefined: r.userDefined,
isAtLeastGo124: file.Pkg.IsAtLeastGo124(),
isAtLeastGo124: file.Pkg.IsAtLeastGoVersion(lint.Go124),
}

ast.Walk(w, file.AST)
Expand Down
Loading