mirror of
https://github.com/mgechev/revive.git
synced 2025-07-11 00:50:27 +02:00
Adds support for new type alias any
in the rule redefines-built-in
(#659)
* adds support for any type alias * adds more representative tests
This commit is contained in:
@ -8,6 +8,60 @@ import (
|
|||||||
"github.com/mgechev/revive/lint"
|
"github.com/mgechev/revive/lint"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var builtInConstAndVars = map[string]bool{
|
||||||
|
"true": true,
|
||||||
|
"false": true,
|
||||||
|
"iota": true,
|
||||||
|
"nil": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var builtFunctions = map[string]bool{
|
||||||
|
"append": true,
|
||||||
|
"cap": true,
|
||||||
|
"close": true,
|
||||||
|
"complex": true,
|
||||||
|
"copy": true,
|
||||||
|
"delete": true,
|
||||||
|
"imag": true,
|
||||||
|
"len": true,
|
||||||
|
"make": true,
|
||||||
|
"new": true,
|
||||||
|
"panic": true,
|
||||||
|
"print": true,
|
||||||
|
"println": true,
|
||||||
|
"real": true,
|
||||||
|
"recover": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var builtInTypes = map[string]bool{
|
||||||
|
"ComplexType": true,
|
||||||
|
"FloatType": true,
|
||||||
|
"IntegerType": true,
|
||||||
|
"Type": true,
|
||||||
|
"Type1": true,
|
||||||
|
"bool": true,
|
||||||
|
"byte": true,
|
||||||
|
"complex128": true,
|
||||||
|
"complex64": true,
|
||||||
|
"error": true,
|
||||||
|
"float32": true,
|
||||||
|
"float64": true,
|
||||||
|
"int": true,
|
||||||
|
"int16": true,
|
||||||
|
"int32": true,
|
||||||
|
"int64": true,
|
||||||
|
"int8": true,
|
||||||
|
"rune": true,
|
||||||
|
"string": true,
|
||||||
|
"uint": true,
|
||||||
|
"uint16": true,
|
||||||
|
"uint32": true,
|
||||||
|
"uint64": true,
|
||||||
|
"uint8": true,
|
||||||
|
"uintptr": true,
|
||||||
|
"any": true,
|
||||||
|
}
|
||||||
|
|
||||||
// RedefinesBuiltinIDRule warns when a builtin identifier is shadowed.
|
// RedefinesBuiltinIDRule warns when a builtin identifier is shadowed.
|
||||||
type RedefinesBuiltinIDRule struct{}
|
type RedefinesBuiltinIDRule struct{}
|
||||||
|
|
||||||
@ -15,65 +69,12 @@ type RedefinesBuiltinIDRule struct{}
|
|||||||
func (r *RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
func (r *RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
||||||
var failures []lint.Failure
|
var failures []lint.Failure
|
||||||
|
|
||||||
builtInConstAndVars := map[string]bool{
|
|
||||||
"true": true,
|
|
||||||
"false": true,
|
|
||||||
"iota": true,
|
|
||||||
"nil": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
builtFunctions := map[string]bool{
|
|
||||||
"append": true,
|
|
||||||
"cap": true,
|
|
||||||
"close": true,
|
|
||||||
"complex": true,
|
|
||||||
"copy": true,
|
|
||||||
"delete": true,
|
|
||||||
"imag": true,
|
|
||||||
"len": true,
|
|
||||||
"make": true,
|
|
||||||
"new": true,
|
|
||||||
"panic": true,
|
|
||||||
"print": true,
|
|
||||||
"println": true,
|
|
||||||
"real": true,
|
|
||||||
"recover": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
builtInTypes := map[string]bool{
|
|
||||||
"ComplexType": true,
|
|
||||||
"FloatType": true,
|
|
||||||
"IntegerType": true,
|
|
||||||
"Type": true,
|
|
||||||
"Type1": true,
|
|
||||||
"bool": true,
|
|
||||||
"byte": true,
|
|
||||||
"complex128": true,
|
|
||||||
"complex64": true,
|
|
||||||
"error": true,
|
|
||||||
"float32": true,
|
|
||||||
"float64": true,
|
|
||||||
"int": true,
|
|
||||||
"int16": true,
|
|
||||||
"int32": true,
|
|
||||||
"int64": true,
|
|
||||||
"int8": true,
|
|
||||||
"rune": true,
|
|
||||||
"string": true,
|
|
||||||
"uint": true,
|
|
||||||
"uint16": true,
|
|
||||||
"uint32": true,
|
|
||||||
"uint64": true,
|
|
||||||
"uint8": true,
|
|
||||||
"uintptr": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
onFailure := func(failure lint.Failure) {
|
onFailure := func(failure lint.Failure) {
|
||||||
failures = append(failures, failure)
|
failures = append(failures, failure)
|
||||||
}
|
}
|
||||||
|
|
||||||
astFile := file.AST
|
astFile := file.AST
|
||||||
w := &lintRedefinesBuiltinID{builtInConstAndVars, builtFunctions, builtInTypes, onFailure}
|
w := &lintRedefinesBuiltinID{onFailure}
|
||||||
ast.Walk(w, astFile)
|
ast.Walk(w, astFile)
|
||||||
|
|
||||||
return failures
|
return failures
|
||||||
@ -85,34 +86,46 @@ func (r *RedefinesBuiltinIDRule) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type lintRedefinesBuiltinID struct {
|
type lintRedefinesBuiltinID struct {
|
||||||
constsAndVars map[string]bool
|
onFailure func(lint.Failure)
|
||||||
funcs map[string]bool
|
|
||||||
types map[string]bool
|
|
||||||
onFailure func(lint.Failure)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor {
|
func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor {
|
||||||
switch n := node.(type) {
|
switch n := node.(type) {
|
||||||
case *ast.GenDecl:
|
case *ast.GenDecl:
|
||||||
if n.Tok != token.TYPE {
|
switch n.Tok {
|
||||||
return nil // skip if not type declaration
|
case token.TYPE:
|
||||||
}
|
typeSpec, ok := n.Specs[0].(*ast.TypeSpec)
|
||||||
typeSpec, ok := n.Specs[0].(*ast.TypeSpec)
|
if !ok {
|
||||||
if !ok {
|
return nil
|
||||||
return nil
|
}
|
||||||
}
|
id := typeSpec.Name.Name
|
||||||
id := typeSpec.Name.Name
|
if ok, bt := w.isBuiltIn(id); ok {
|
||||||
if w.types[id] {
|
w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, id))
|
||||||
w.addFailure(n, fmt.Sprintf("redefinition of the built-in type %s", id))
|
}
|
||||||
|
case token.VAR, token.CONST:
|
||||||
|
for _, vs := range n.Specs {
|
||||||
|
valSpec, ok := vs.(*ast.ValueSpec)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, name := range valSpec.Names {
|
||||||
|
if ok, bt := w.isBuiltIn(name.Name); ok {
|
||||||
|
w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil // skip if not type/var/const declaration
|
||||||
}
|
}
|
||||||
|
|
||||||
case *ast.FuncDecl:
|
case *ast.FuncDecl:
|
||||||
if n.Recv != nil {
|
if n.Recv != nil {
|
||||||
return w // skip methods
|
return w // skip methods
|
||||||
}
|
}
|
||||||
|
|
||||||
id := n.Name.Name
|
id := n.Name.Name
|
||||||
if w.funcs[id] {
|
if ok, bt := w.isBuiltIn(id); ok {
|
||||||
w.addFailure(n, fmt.Sprintf("redefinition of the built-in function %s", id))
|
w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, id))
|
||||||
}
|
}
|
||||||
case *ast.AssignStmt:
|
case *ast.AssignStmt:
|
||||||
for _, e := range n.Lhs {
|
for _, e := range n.Lhs {
|
||||||
@ -121,13 +134,20 @@ func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.constsAndVars[id.Name] {
|
if ok, bt := w.isBuiltIn(id.Name); ok {
|
||||||
var msg string
|
var msg string
|
||||||
if n.Tok == token.DEFINE {
|
println(bt, id.Name)
|
||||||
msg = fmt.Sprintf("assignment creates a shadow of built-in identifier %s", id.Name)
|
switch bt {
|
||||||
} else {
|
case "constant or variable":
|
||||||
msg = fmt.Sprintf("assignment modifies built-in identifier %s", id.Name)
|
if n.Tok == token.DEFINE {
|
||||||
|
msg = fmt.Sprintf("assignment creates a shadow of built-in identifier %s", id.Name)
|
||||||
|
} else {
|
||||||
|
msg = fmt.Sprintf("assignment modifies built-in identifier %s", id.Name)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
msg = fmt.Sprintf("redefinition of the built-in %s %s", bt, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.addFailure(n, msg)
|
w.addFailure(n, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,3 +164,19 @@ func (w lintRedefinesBuiltinID) addFailure(node ast.Node, msg string) {
|
|||||||
Failure: msg,
|
Failure: msg,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w lintRedefinesBuiltinID) isBuiltIn(id string) (r bool, builtInKind string) {
|
||||||
|
if builtFunctions[id] {
|
||||||
|
return true, "function"
|
||||||
|
}
|
||||||
|
|
||||||
|
if builtInConstAndVars[id] {
|
||||||
|
return true, "constant or variable"
|
||||||
|
}
|
||||||
|
|
||||||
|
if builtInTypes[id] {
|
||||||
|
return true, "type"
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
10
testdata/redefines-builtin-id.go
vendored
10
testdata/redefines-builtin-id.go
vendored
@ -19,3 +19,13 @@ func delete(set []int64, i int) (y []int64) { // MATCH /redefinition of the buil
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type any int // MATCH /redefinition of the built-in type any/
|
||||||
|
|
||||||
|
func any() {} // MATCH /redefinition of the built-in type any/
|
||||||
|
|
||||||
|
var any int // MATCH /redefinition of the built-in type any/
|
||||||
|
|
||||||
|
const any = 1 // MATCH /redefinition of the built-in type any/
|
||||||
|
|
||||||
|
var i, copy int // MATCH /redefinition of the built-in function copy/
|
||||||
|
Reference in New Issue
Block a user