| 733 | } |
| 734 | |
| 735 | func (t *walker) funcType(parent ast.Node, ft *ast.FuncType) ast.Visitor { |
| 736 | // Clear state in case we're recursing into a function literal |
| 737 | // inside a function that returns an error. |
| 738 | newT := *t |
| 739 | newT.errorObjs = nil |
| 740 | newT.errorIdents = nil |
| 741 | newT.errorIndices = nil |
| 742 | newT.numReturns = 0 |
| 743 | t = &newT |
| 744 | |
| 745 | // If the function does not return anything, |
| 746 | // we still need to recurse into any function literals. |
| 747 | // Just return this visitor to continue recursing. |
| 748 | if ft.Results == nil { |
| 749 | return t |
| 750 | } |
| 751 | |
| 752 | // If the function has return values, |
| 753 | // we need to consider the following cases: |
| 754 | // |
| 755 | // - no error return value |
| 756 | // - unnamed error return |
| 757 | // - named error return |
| 758 | var ( |
| 759 | objs []*ast.Object // objects of error return values |
| 760 | idents []*ast.Ident // identifiers of named error return values |
| 761 | indices []int // indices of error return values |
| 762 | count int // total number of return values |
| 763 | // Invariants: |
| 764 | // len(indices) <= count |
| 765 | // len(names) == 0 || len(names) == len(indices) |
| 766 | ) |
| 767 | for _, field := range ft.Results.List { |
| 768 | isError := isIdent(field.Type, "error") |
| 769 | |
| 770 | // field.Names is nil for unnamed return values. |
| 771 | // Either all returns are named or none are. |
| 772 | if len(field.Names) > 0 { |
| 773 | for _, name := range field.Names { |
| 774 | if isError { |
| 775 | objs = append(objs, name.Obj) |
| 776 | idents = append(idents, name) |
| 777 | indices = append(indices, count) |
| 778 | } |
| 779 | count++ |
| 780 | } |
| 781 | } else { |
| 782 | if isError { |
| 783 | indices = append(indices, count) |
| 784 | } |
| 785 | count++ |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | // If there are no error return values, |
| 790 | // recurse to look for function literals. |
| 791 | if len(indices) == 0 { |
| 792 | return t |