| 811 | } |
| 812 | |
| 813 | func (t *walker) returnStmt(n *ast.ReturnStmt) ast.Visitor { |
| 814 | // Doesn't return errors. Continue recursing. |
| 815 | if len(t.errorIndices) == 0 { |
| 816 | return t |
| 817 | } |
| 818 | |
| 819 | // Naked return. |
| 820 | // We want to add assignments to the named return values. |
| 821 | if n.Results == nil { |
| 822 | if t.optout(n.Pos()) { |
| 823 | return nil |
| 824 | } |
| 825 | |
| 826 | // Ignore errors that have already been wrapped. |
| 827 | names := make([]string, 0, len(t.errorIndices)) |
| 828 | for _, ident := range t.errorIdents { |
| 829 | if _, ok := t.alreadyWrapped[ident.Obj]; ok { |
| 830 | continue |
| 831 | } |
| 832 | names = append(names, ident.Name) |
| 833 | } |
| 834 | |
| 835 | if len(names) > 0 { |
| 836 | *t.inserts = append(*t.inserts, &insertWrapAssign{ |
| 837 | Names: names, |
| 838 | Before: n.Pos(), |
| 839 | }) |
| 840 | } |
| 841 | |
| 842 | return nil |
| 843 | } |
| 844 | |
| 845 | // Return with multiple return values being automatically expanded |
| 846 | // E.g., |
| 847 | // func foo() (int, error) { |
| 848 | // return bar() |
| 849 | // } |
| 850 | // This needs to become: |
| 851 | // func foo() (int, error) { |
| 852 | // return Wrap2(bar()) |
| 853 | // } |
| 854 | // This is only supported if numReturns <= 6 and only the last return value is an error. |
| 855 | if len(n.Results) == 1 && t.numReturns > 1 { |
| 856 | if _, ok := n.Results[0].(*ast.CallExpr); !ok { |
| 857 | t.logf(n.Pos(), "skipping function with incorrect number of return values: got %d, want %d", len(n.Results), t.numReturns) |
| 858 | return t |
| 859 | } |
| 860 | |
| 861 | t.wrapReturnCall(t.numReturns, n) |
| 862 | return t |
| 863 | } |
| 864 | |
| 865 | for _, idx := range t.errorIndices { |
| 866 | t.wrapExpr(1, n.Results[idx]) |
| 867 | } |
| 868 | |
| 869 | return t |
| 870 | } |