-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypednil.go
213 lines (183 loc) · 4.54 KB
/
typednil.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package typednil
import (
"fmt"
"go/token"
"go/types"
"strings"
"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/ssa"
)
const doc = "typednil finds a comparison between typed nil and untyped nil"
// Analyzer finds a comparison between typed nil and untyped nil
var Analyzer = &analysis.Analyzer{
Name: "typednil",
Doc: doc,
Run: new(analyzer).run,
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
},
FactTypes: []analysis.Fact{new(isNilable)},
}
type nilableKind int
const (
interfaceNilable nilableKind = iota
concreteNilable
)
func (k nilableKind) String() string {
switch k {
case interfaceNilable:
return "I"
default:
return "C"
}
}
type result struct {
Pos token.Position
Kind nilableKind
}
type isNilable struct {
Name string
Results map[int]result
}
func (*isNilable) AFact() {}
var _ analysis.Fact = (*isNilable)(nil)
func (f *isNilable) String() string {
rets := make([]string, 0, len(f.Results))
for index, r := range f.Results {
rets = append(rets, fmt.Sprintf("%v:%v", index, r.Kind))
}
return fmt.Sprintf("nilable results [%v]", strings.Join(rets, ","))
}
var _ fmt.Stringer = (*isNilable)(nil)
type analyzer struct {
pass *analysis.Pass
}
func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
a.pass = pass
a.findNilable()
a.findTypedNilCmp()
return nil, nil
}
func (a *analyzer) isTypedNil(v ssa.Value) (reason string) {
switch v := v.(type) {
case *ssa.MakeInterface:
switch x := v.X.(type) {
case *ssa.Const:
if x.IsNil() && !types.Identical(x.Type(), types.Typ[types.UntypedNil]) {
return fmt.Sprintf("%v convert to interface", x)
}
return ""
default:
return a.nilableFuncCall(x, concreteNilable)
}
default:
return a.nilableFuncCall(v, interfaceNilable)
}
}
func (a *analyzer) nilableFuncCall(v ssa.Value, kind nilableKind) string {
switch v := v.(type) {
case *ssa.Call:
fact := a.importFact(v)
if fact == nil {
return ""
}
if r, ok := fact.Results[0]; ok && r.Kind == kind {
reason := fact.Name + " may become typed nil"
if r.Pos.IsValid() {
reason += " in " + r.Pos.String()
}
return reason
}
return ""
case *ssa.Extract:
call, _ := v.Tuple.(*ssa.Call)
if call == nil {
return ""
}
fact := a.importFact(call)
if fact == nil {
return ""
}
if r, ok := fact.Results[v.Index]; ok && r.Kind == kind {
return fmt.Sprintf("%s may become typed nil in %v", fact.Name, r.Pos)
}
return ""
default:
return ""
}
}
func (a *analyzer) importFact(v *ssa.Call) *isNilable {
if v.Call.Method != nil {
return nil
}
fun, _ := v.Call.Value.(*ssa.Function)
if fun == nil || fun.Object() == nil {
return nil
}
var fact isNilable
ok := a.pass.ImportObjectFact(fun.Object(), &fact)
if ok {
return &fact
}
return nil
}
func (a *analyzer) isCostNil(v ssa.Value) bool {
switch v := v.(type) {
case *ssa.Const:
return v.IsNil()
}
return false
}
func (a *analyzer) findNilable() {
s := a.pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
for _, f := range s.SrcFuncs {
obj := f.Object()
if obj == nil {
continue
}
rets := analysisutil.Returns(f)
results := make(map[int]result)
for _, ret := range rets {
for i, r := range ret.Results {
if _, ok := results[i]; ok {
continue
}
sr := f.Signature.Results().At(i)
pos := a.pass.Fset.Position(ret.Pos())
switch {
case types.IsInterface(r.Type()) && a.isTypedNil(r) != "":
results[i] = result{Pos: pos, Kind: interfaceNilable}
case !types.IsInterface(sr.Type()) && a.isCostNil(r):
results[i] = result{Pos: pos, Kind: concreteNilable}
}
}
}
if len(results) != 0 {
fact := &isNilable{
Name: f.String(),
Results: results,
}
a.pass.ExportObjectFact(obj, fact)
}
}
}
func (a *analyzer) findTypedNilCmp() {
s := a.pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
analysisutil.InspectFuncs(s.SrcFuncs, func(_ int, instr ssa.Instruction) bool {
binOp, _ := instr.(*ssa.BinOp)
if binOp == nil || (binOp.Op != token.EQL && binOp.Op != token.NEQ) {
return true
}
if reason := a.isTypedNil(binOp.X); reason != "" && a.isCostNil(binOp.Y) {
a.pass.Reportf(binOp.Pos(), "it may become a comparison between a typed nil and an untyped nil because %s", reason)
return true
}
if reason := a.isTypedNil(binOp.Y); reason != "" && a.isCostNil(binOp.X) {
a.pass.Reportf(binOp.Pos(), "it may become a comparison between a typed nil and an untyped nil because %s", reason)
return true
}
return true
})
}