Skip to content

Commit 05c4801

Browse files
skajichavacava
andauthored
check param, result, type param of function in redefines-builtin-id rule (#1023)
* check param, result, type param of function in redefines-builtin-id rule * combine the if statements * tiny refactoring to make it more Go idiomatic --------- Co-authored-by: chavacava <[email protected]>
1 parent e54773e commit 05c4801

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

rule/redefines-builtin-id.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor {
145145
if ok, bt := w.isBuiltIn(id); ok {
146146
w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, id))
147147
}
148+
case *ast.FuncType:
149+
var fields []*ast.Field
150+
if n.TypeParams != nil {
151+
fields = append(fields, n.TypeParams.List...)
152+
}
153+
if n.Params != nil {
154+
fields = append(fields, n.Params.List...)
155+
}
156+
if n.Results != nil {
157+
fields = append(fields, n.Results.List...)
158+
}
159+
for _, field := range fields {
160+
for _, name := range field.Names {
161+
obj := name.Obj
162+
isTypeOrName := obj != nil && (obj.Kind == ast.Var || obj.Kind == ast.Typ)
163+
if !isTypeOrName {
164+
continue
165+
}
166+
167+
id := obj.Name
168+
if ok, bt := w.isBuiltIn(id); ok {
169+
w.addFailure(name, fmt.Sprintf("redefinition of the built-in %s %s", bt, id))
170+
}
171+
}
172+
}
148173
case *ast.AssignStmt:
149174
for _, e := range n.Lhs {
150175
id, ok := e.(*ast.Ident)

testdata/redefines-builtin-id.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ func foo() {
4141
_ = max
4242
_ = min
4343
}
44+
45+
func foo1(new int) { // MATCH /redefinition of the built-in function new/
46+
_ = new
47+
}
48+
49+
func foo2() (new int) { // MATCH /redefinition of the built-in function new/
50+
return
51+
}
52+
53+
func foo3[new any]() { // MATCH /redefinition of the built-in function new/
54+
}

0 commit comments

Comments
 (0)