Skip to content
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

fix: ast_utils.FuncSignatureIs panics with types names other than ast.Ident #1174

Merged
merged 1 commit into from
Dec 7, 2024
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
20 changes: 18 additions & 2 deletions internal/astutils/ast_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package astutils

import "go/ast"
import (
"fmt"
"go/ast"
)

// FuncSignatureIs returns true if the given func decl satisfies a signature characterized
// by the given name, parameters types and return types; false otherwise.
Expand Down Expand Up @@ -45,7 +48,7 @@ func getTypeNames(fields *ast.FieldList) []string {
}

for _, field := range fields.List {
typeName := field.Type.(*ast.Ident).Name
typeName := getFieldTypeName(field.Type)
if field.Names == nil { // unnamed field
result = append(result, typeName)
continue
Expand All @@ -58,3 +61,16 @@ func getTypeNames(fields *ast.FieldList) []string {

return result
}

func getFieldTypeName(typ ast.Expr) string {
switch f := typ.(type) {
case *ast.Ident:
return f.Name
case *ast.SelectorExpr:
return f.Sel.Name + "." + getFieldTypeName(f.X)
case *ast.StarExpr:
return "*" + getFieldTypeName(f.X)
default:
panic(fmt.Sprintf("not supported type %T", typ))
}
}
5 changes: 5 additions & 0 deletions testdata/golint/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ type Vv []int

func (vv Vv) Len() (result int) { return len(w) } // MATCH /exported method Vv.Len should have comment or be unexported/
func (vv Vv) Less(i int, j int) (result bool) { return w[i] < w[j] } // MATCH /exported method Vv.Less should have comment or be unexported/

// X is ...
type X []int

func (x X) Less(i *pkg.tip) (result bool) { return len(x) } // MATCH /exported method X.Less should have comment or be unexported/
Loading