Skip to content

Commit

Permalink
Fix call typed to exclude named functions
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jan 24, 2023
1 parent c59a3b9 commit cffbb1f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ func (v *visitor) checkFunc(fn reflect.Type, method bool, node *ast.CallNode, na
}
}

if !fn.IsVariadic() {
// OnCallTyped doesn't work for functions with variadic arguments,
// and doesn't work named function, like `type MyFunc func() int`.
// In PkgPath() is an empty string, it's unnamed function.
if !fn.IsVariadic() && fn.PkgPath() == "" {
funcTypes:
for i := range vm.FuncTypes {
if i == 0 {
Expand Down
13 changes: 13 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,19 @@ func TestCheck_CallFastTyped_Method(t *testing.T) {
require.Equal(t, 42, tree.Node.(*ast.CallNode).Typed)
}

func TestCheck_CallTyped_excludes_named_functions(t *testing.T) {
env := mock.Env{}

tree, err := parser.Parse("FuncNamed('bar')")
require.NoError(t, err)

_, err = checker.Check(tree, conf.New(env))
require.NoError(t, err)

require.False(t, tree.Node.(*ast.CallNode).Fast)
require.Equal(t, 0, tree.Node.(*ast.CallNode).Typed)
}

func TestCheck_works_with_nil_types(t *testing.T) {
env := map[string]interface{}{
"null": nil,
Expand Down
3 changes: 3 additions & 0 deletions test/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Env struct {
FuncParam func(_ bool, _ int, _ string) bool
FuncParamAny func(_ interface{}) bool
FuncTooManyReturns func() (int, int, error)
FuncNamed MyFunc
NilFn func()
Variadic func(_ int, _ ...int) bool
Fast func(...interface{}) interface{}
Expand Down Expand Up @@ -75,3 +76,5 @@ type Bar struct {
type Abstract interface {
Method(int) int
}

type MyFunc func(string) int

0 comments on commit cffbb1f

Please sign in to comment.