mirror of
https://github.com/mgechev/revive.git
synced 2025-07-13 01:00:17 +02:00
fix: ast_utils.FuncSignatureIs panics with types names other than ast.Ident (#1174)
This commit is contained in:
@ -1,7 +1,10 @@
|
|||||||
// Package astutils provides utility functions for working with AST nodes
|
// Package astutils provides utility functions for working with AST nodes
|
||||||
package astutils
|
package astutils
|
||||||
|
|
||||||
import "go/ast"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/ast"
|
||||||
|
)
|
||||||
|
|
||||||
// FuncSignatureIs returns true if the given func decl satisfies a signature characterized
|
// FuncSignatureIs returns true if the given func decl satisfies a signature characterized
|
||||||
// by the given name, parameters types and return types; false otherwise.
|
// 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 {
|
for _, field := range fields.List {
|
||||||
typeName := field.Type.(*ast.Ident).Name
|
typeName := getFieldTypeName(field.Type)
|
||||||
if field.Names == nil { // unnamed field
|
if field.Names == nil { // unnamed field
|
||||||
result = append(result, typeName)
|
result = append(result, typeName)
|
||||||
continue
|
continue
|
||||||
@ -59,3 +62,16 @@ func getTypeNames(fields *ast.FieldList) []string {
|
|||||||
|
|
||||||
return result
|
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
testdata/golint/sort.go
vendored
5
testdata/golint/sort.go
vendored
@ -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) 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/
|
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/
|
||||||
|
Reference in New Issue
Block a user