forked from sqs/goreturns
/
fix.go
144 lines (127 loc) · 3.61 KB
/
fix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package returns
import (
"fmt"
"go/ast"
"go/printer"
"go/token"
"go/types"
"os"
)
func fixReturns(fset *token.FileSet, f *ast.File, typeInfo *types.Info) error {
// map of potentially incomplete return statements (that might
// need fixing) to the FuncType of the return's enclosing FuncDecl
// or FuncLit
incReturns := map[*ast.ReturnStmt]*ast.FuncType{}
// collect incomplete returns
ast.Walk(visitor{returns: incReturns}, f)
// printIncReturnsVerbose(fset, incReturns)
IncReturnsLoop:
for ret, ftyp := range incReturns {
if ftyp.Results == nil {
continue
}
numRVs := len(ret.Results)
if numRVs == len(ftyp.Results.List) {
// correct return arity
continue
}
if numRVs == 0 {
// skip naked returns (could be named return values)
continue
}
if numRVs > len(ftyp.Results.List) {
// too many return values; preserve and ignore
continue
}
// skip if return value is a func call (whose multiple returns
// might be expanded)
if e, ok := ret.Results[0].(*ast.CallExpr); ok {
if !funcHasSingleReturnVal(typeInfo, e) {
continue
}
}
// left-fill zero values
zvs := make([]ast.Expr, len(ftyp.Results.List)-numRVs)
for i, rt := range ftyp.Results.List[:len(zvs)] {
zv := newZeroValueNode(rt.Type)
if zv == nil {
// be conservative; if we can't determine the zero
// value, don't fill in anything
continue IncReturnsLoop
}
zvs[i] = zv
}
ret.Results = append(zvs, ret.Results...)
}
return nil
}
type visitor struct {
enclosing *ast.FuncType // innermost enclosing func
returns map[*ast.ReturnStmt]*ast.FuncType // potentially incomplete returns
}
func (v visitor) Visit(node ast.Node) ast.Visitor {
if node == nil {
return v
}
switch n := node.(type) {
case *ast.FuncDecl:
return visitor{enclosing: n.Type, returns: v.returns}
case *ast.FuncLit:
return visitor{enclosing: n.Type, returns: v.returns}
case *ast.ReturnStmt:
v.returns[n] = v.enclosing
}
return v
}
// newZeroValueNode returns an AST expr representing the zero value of
// typ. If determining the zero value requires additional information
// (e.g., type-checking output), it returns nil.
func newZeroValueNode(typ ast.Expr) ast.Expr {
switch v := typ.(type) {
case *ast.Ident:
switch v.Name {
case "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "byte", "rune", "uint", "int", "uintptr":
return &ast.BasicLit{Kind: token.INT, Value: "0"}
case "float32", "float64":
return &ast.BasicLit{Kind: token.FLOAT, Value: "0"}
case "complex64", "complex128":
return &ast.BasicLit{Kind: token.IMAG, Value: "0"}
case "bool":
return &ast.Ident{Name: "false"}
case "string":
return &ast.BasicLit{Kind: token.STRING, Value: `""`}
case "error":
return &ast.Ident{Name: "nil"}
}
case *ast.ArrayType:
if v.Len == nil {
// slice
return &ast.Ident{Name: "nil"}
}
return &ast.CompositeLit{Type: v}
case *ast.StarExpr:
return &ast.Ident{Name: "nil"}
}
return nil
}
func printIncReturns(fset *token.FileSet, v map[*ast.ReturnStmt]*ast.FuncType) {
for ret, ftyp := range v {
fmt.Print("FUNC TYPE: ")
printer.Fprint(os.Stdout, fset, ftyp)
fmt.Print(" RETURN: ")
printer.Fprint(os.Stdout, fset, ret)
fmt.Println()
}
}
func printIncReturnsVerbose(fset *token.FileSet, v map[*ast.ReturnStmt]*ast.FuncType) {
for ret, ftyp := range v {
fmt.Print("FUNC TYPE: ")
ast.Print(fset, ftyp)
fmt.Print(" RETURN: ")
ast.Print(fset, ret)
fmt.Println()
}
}