1
0
mirror of https://github.com/mgechev/revive.git synced 2024-12-12 10:44:59 +02:00

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

This commit is contained in:
chavacava 2024-12-07 15:40:39 +01:00 committed by GitHub
parent 06192a5599
commit 24a9325b5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -1,7 +1,10 @@
// Package astutils provides utility functions for working with AST nodes
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.
@ -46,7 +49,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
@ -59,3 +62,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))
}
}

View File

@ -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/