Skip to content

Commit cffbb1f

Browse files
committed
Fix call typed to exclude named functions
1 parent c59a3b9 commit cffbb1f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Diff for: checker/checker.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,10 @@ func (v *visitor) checkFunc(fn reflect.Type, method bool, node *ast.CallNode, na
568568
}
569569
}
570570

571-
if !fn.IsVariadic() {
571+
// OnCallTyped doesn't work for functions with variadic arguments,
572+
// and doesn't work named function, like `type MyFunc func() int`.
573+
// In PkgPath() is an empty string, it's unnamed function.
574+
if !fn.IsVariadic() && fn.PkgPath() == "" {
572575
funcTypes:
573576
for i := range vm.FuncTypes {
574577
if i == 0 {

Diff for: checker/checker_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,19 @@ func TestCheck_CallFastTyped_Method(t *testing.T) {
734734
require.Equal(t, 42, tree.Node.(*ast.CallNode).Typed)
735735
}
736736

737+
func TestCheck_CallTyped_excludes_named_functions(t *testing.T) {
738+
env := mock.Env{}
739+
740+
tree, err := parser.Parse("FuncNamed('bar')")
741+
require.NoError(t, err)
742+
743+
_, err = checker.Check(tree, conf.New(env))
744+
require.NoError(t, err)
745+
746+
require.False(t, tree.Node.(*ast.CallNode).Fast)
747+
require.Equal(t, 0, tree.Node.(*ast.CallNode).Typed)
748+
}
749+
737750
func TestCheck_works_with_nil_types(t *testing.T) {
738751
env := map[string]interface{}{
739752
"null": nil,

Diff for: test/mock/mock.go

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Env struct {
2828
FuncParam func(_ bool, _ int, _ string) bool
2929
FuncParamAny func(_ interface{}) bool
3030
FuncTooManyReturns func() (int, int, error)
31+
FuncNamed MyFunc
3132
NilFn func()
3233
Variadic func(_ int, _ ...int) bool
3334
Fast func(...interface{}) interface{}
@@ -75,3 +76,5 @@ type Bar struct {
7576
type Abstract interface {
7677
Method(int) int
7778
}
79+
80+
type MyFunc func(string) int

0 commit comments

Comments
 (0)