From 05c4801f9be1c3727e640c5c314cdf0694fce956 Mon Sep 17 00:00:00 2001 From: Shoichi Kaji Date: Thu, 15 Aug 2024 20:29:11 +0900 Subject: [PATCH] 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 --- rule/redefines-builtin-id.go | 25 +++++++++++++++++++++++++ testdata/redefines-builtin-id.go | 11 +++++++++++ 2 files changed, 36 insertions(+) diff --git a/rule/redefines-builtin-id.go b/rule/redefines-builtin-id.go index f367ea8..8aaaf03 100644 --- a/rule/redefines-builtin-id.go +++ b/rule/redefines-builtin-id.go @@ -145,6 +145,31 @@ func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor { if ok, bt := w.isBuiltIn(id); ok { w.addFailure(n, fmt.Sprintf("redefinition of the built-in %s %s", bt, id)) } + case *ast.FuncType: + var fields []*ast.Field + if n.TypeParams != nil { + fields = append(fields, n.TypeParams.List...) + } + if n.Params != nil { + fields = append(fields, n.Params.List...) + } + if n.Results != nil { + fields = append(fields, n.Results.List...) + } + for _, field := range fields { + for _, name := range field.Names { + obj := name.Obj + isTypeOrName := obj != nil && (obj.Kind == ast.Var || obj.Kind == ast.Typ) + if !isTypeOrName { + continue + } + + id := obj.Name + if ok, bt := w.isBuiltIn(id); ok { + w.addFailure(name, fmt.Sprintf("redefinition of the built-in %s %s", bt, id)) + } + } + } case *ast.AssignStmt: for _, e := range n.Lhs { id, ok := e.(*ast.Ident) diff --git a/testdata/redefines-builtin-id.go b/testdata/redefines-builtin-id.go index e5f7c16..2426b10 100644 --- a/testdata/redefines-builtin-id.go +++ b/testdata/redefines-builtin-id.go @@ -41,3 +41,14 @@ func foo() { _ = max _ = min } + +func foo1(new int) { // MATCH /redefinition of the built-in function new/ + _ = new +} + +func foo2() (new int) { // MATCH /redefinition of the built-in function new/ + return +} + +func foo3[new any]() { // MATCH /redefinition of the built-in function new/ +}