1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00

check clear/max/min in redefines-builtin-id rule on go1.21+ (#1024)

* check clear/max/min in redefines-builtin-id rule on go1.21+

* Update rule/redefines-builtin-id.go

Co-authored-by: Denis Voytyuk <5462781+denisvmedia@users.noreply.github.com>

---------

Co-authored-by: Denis Voytyuk <5462781+denisvmedia@users.noreply.github.com>
This commit is contained in:
Shoichi Kaji 2024-08-15 20:14:16 +09:00 committed by GitHub
parent a638ed6e24
commit e54773e4b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 6 deletions

View File

@ -33,6 +33,7 @@ var (
falseValue = 2 falseValue = 2
notSet = 3 notSet = 3
go121 = goversion.Must(goversion.NewVersion("1.21"))
go122 = goversion.Must(goversion.NewVersion("1.22")) go122 = goversion.Must(goversion.NewVersion("1.22"))
) )
@ -194,6 +195,11 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) {
wg.Wait() wg.Wait()
} }
// 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 // IsAtLeastGo122 returns true if the Go version for this package is 1.22 or higher, false otherwise
func (p *Package) IsAtLeastGo122() bool { func (p *Package) IsAtLeastGo122() bool {
return p.goVersion.GreaterThanOrEqual(go122) return p.goVersion.GreaterThanOrEqual(go122)

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"go/ast" "go/ast"
"go/token" "go/token"
"maps"
"github.com/mgechev/revive/lint" "github.com/mgechev/revive/lint"
) )
@ -33,6 +34,12 @@ var builtFunctions = map[string]bool{
"recover": true, "recover": true,
} }
var builtFunctionsAfterGo121 = map[string]bool{
"clear": true,
"max": true,
"min": true,
}
var builtInTypes = map[string]bool{ var builtInTypes = map[string]bool{
"bool": true, "bool": true,
"byte": true, "byte": true,
@ -69,7 +76,17 @@ func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.F
} }
astFile := file.AST astFile := file.AST
w := &lintRedefinesBuiltinID{onFailure}
builtFuncs := maps.Clone(builtFunctions)
if file.Pkg.IsAtLeastGo121() {
maps.Copy(builtFuncs, builtFunctionsAfterGo121)
}
w := &lintRedefinesBuiltinID{
onFailure: onFailure,
builtInConstAndVars: builtInConstAndVars,
builtFunctions: builtFuncs,
builtInTypes: builtInTypes,
}
ast.Walk(w, astFile) ast.Walk(w, astFile)
return failures return failures
@ -81,7 +98,10 @@ func (*RedefinesBuiltinIDRule) Name() string {
} }
type lintRedefinesBuiltinID struct { type lintRedefinesBuiltinID struct {
onFailure func(lint.Failure) onFailure func(lint.Failure)
builtInConstAndVars map[string]bool
builtFunctions map[string]bool
builtInTypes map[string]bool
} }
func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor { func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor {
@ -162,16 +182,16 @@ func (w lintRedefinesBuiltinID) addFailure(node ast.Node, msg string) {
}) })
} }
func (lintRedefinesBuiltinID) isBuiltIn(id string) (r bool, builtInKind string) { func (w *lintRedefinesBuiltinID) isBuiltIn(id string) (r bool, builtInKind string) {
if builtFunctions[id] { if w.builtFunctions[id] {
return true, "function" return true, "function"
} }
if builtInConstAndVars[id] { if w.builtInConstAndVars[id] {
return true, "constant or variable" return true, "constant or variable"
} }
if builtInTypes[id] { if w.builtInTypes[id] {
return true, "type" return true, "type"
} }

View File

@ -32,3 +32,12 @@ var i, copy int // MATCH /redefinition of the built-in function copy/
// issue #792 // issue #792
type () type ()
func foo() {
clear := 0 // MATCH /redefinition of the built-in function clear/
max := 0 // MATCH /redefinition of the built-in function max/
min := 0 // MATCH /redefinition of the built-in function min/
_ = clear
_ = max
_ = min
}