-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwraperrfmt.go
152 lines (121 loc) · 2.71 KB
/
wraperrfmt.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
package wraperrfmt
import (
"go/ast"
"go/constant"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/ssa"
)
var errType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
var Analyzer = &analysis.Analyzer{
Name: "wraperrfmt",
Doc: Doc,
Run: run,
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
},
}
const Doc = "wraperrfmt checks invalid arguments of xerrors.Errorf"
func run(pass *analysis.Pass) (interface{}, error) {
funcs := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs
for i := range funcs {
for _, b := range funcs[i].Blocks {
for _, inst := range b.Instrs {
if isInvalidErrorf(pass, inst) {
pass.Reportf(inst.Pos(), "unexpected format. format must end with ': %%w'")
}
}
}
}
return nil, nil
}
func isInvalidErrorf(pass *analysis.Pass, inst ssa.Instruction) bool {
call, ok := inst.(*ssa.Call)
if !ok {
return false
}
if !isCallErrorf(call) {
return false
}
format := getFormat(call.Call.Args)
if !strings.Contains(format, "%w") {
return false
}
typ, ok := lastErr(pass, call.Pos())
if !ok {
return false
}
if strings.HasSuffix(format, ": %w") && typ != nil && types.Implements(typ, errType) {
return false
}
return true
}
func isCallErrorf(call *ssa.Call) bool {
f := call.Common().StaticCallee()
if f == nil || f.Pkg == nil {
return false
}
if removeVendor(f.Pkg.Pkg.Path()) != "golang.org/x/xerrors" {
return false
}
if f.Name() != "Errorf" {
return false
}
return true
}
func removeVendor(path string) string {
s := strings.Split(path, "/")
for i := range s {
if s[i] == "vendor" {
return strings.Join(s[i+1:], "/")
}
}
return path
}
func getFormat(args []ssa.Value) string {
if len(args) == 0 {
return ""
}
format, isConst := args[0].(*ssa.Const)
if !isConst {
return ""
}
if format.Value.Kind() != constant.String {
return ""
}
return constant.StringVal(format.Value)
}
func lastErr(pass *analysis.Pass, pos token.Pos) (types.Type, bool) {
file := getFile(pass.Files, pos)
if file == nil {
return nil, false
}
path, exact := astutil.PathEnclosingInterval(file, pos, pos)
if !exact || len(path) == 0 {
return nil, false
}
callExpr, ok := path[0].(*ast.CallExpr)
if !ok {
return nil, false
}
if callExpr.Ellipsis != token.NoPos {
return nil, false
}
if len(callExpr.Args) < 2 {
return nil, true
}
last := callExpr.Args[len(callExpr.Args)-1]
return pass.TypesInfo.TypeOf(last), true
}
func getFile(fs []*ast.File, pos token.Pos) *ast.File {
for i := range fs {
if fs[i].Pos() <= pos && pos <= fs[i].End() {
return fs[i]
}
}
return nil
}