1
2
3
4
5 package stdmethods
6
7 import (
8 _ "embed"
9 "go/ast"
10 "go/types"
11 "strings"
12
13 "golang.org/x/tools/go/analysis"
14 "golang.org/x/tools/go/analysis/passes/inspect"
15 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
16 "golang.org/x/tools/go/ast/inspector"
17 )
18
19
20 var doc string
21
22 var Analyzer = &analysis.Analyzer{
23 Name: "stdmethods",
24 Doc: analysisutil.MustExtractDoc(doc, "stdmethods"),
25 URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/stdmethods",
26 Requires: []*analysis.Analyzer{inspect.Analyzer},
27 Run: run,
28 }
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 var canonicalMethods = map[string]struct{ args, results []string }{
47 "As": {[]string{"any"}, []string{"bool"}},
48
49 "Format": {[]string{"=fmt.State", "rune"}, []string{}},
50 "GobDecode": {[]string{"[]byte"}, []string{"error"}},
51 "GobEncode": {[]string{}, []string{"[]byte", "error"}},
52 "Is": {[]string{"error"}, []string{"bool"}},
53 "MarshalJSON": {[]string{}, []string{"[]byte", "error"}},
54 "MarshalXML": {[]string{"*xml.Encoder", "xml.StartElement"}, []string{"error"}},
55 "ReadByte": {[]string{}, []string{"byte", "error"}},
56 "ReadFrom": {[]string{"=io.Reader"}, []string{"int64", "error"}},
57 "ReadRune": {[]string{}, []string{"rune", "int", "error"}},
58 "Scan": {[]string{"=fmt.ScanState", "rune"}, []string{"error"}},
59 "Seek": {[]string{"=int64", "int"}, []string{"int64", "error"}},
60 "UnmarshalJSON": {[]string{"[]byte"}, []string{"error"}},
61 "UnmarshalXML": {[]string{"*xml.Decoder", "xml.StartElement"}, []string{"error"}},
62 "UnreadByte": {[]string{}, []string{"error"}},
63 "UnreadRune": {[]string{}, []string{"error"}},
64 "Unwrap": {[]string{}, []string{"error"}},
65 "WriteByte": {[]string{"byte"}, []string{"error"}},
66 "WriteTo": {[]string{"=io.Writer"}, []string{"int64", "error"}},
67 }
68
69 func run(pass *analysis.Pass) (interface{}, error) {
70 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
71
72 nodeFilter := []ast.Node{
73 (*ast.FuncDecl)(nil),
74 (*ast.InterfaceType)(nil),
75 }
76 inspect.Preorder(nodeFilter, func(n ast.Node) {
77 switch n := n.(type) {
78 case *ast.FuncDecl:
79 if n.Recv != nil {
80 canonicalMethod(pass, n.Name)
81 }
82 case *ast.InterfaceType:
83 for _, field := range n.Methods.List {
84 for _, id := range field.Names {
85 canonicalMethod(pass, id)
86 }
87 }
88 }
89 })
90 return nil, nil
91 }
92
93 func canonicalMethod(pass *analysis.Pass, id *ast.Ident) {
94
95 expect, ok := canonicalMethods[id.Name]
96 if !ok {
97 return
98 }
99
100
101 sign := pass.TypesInfo.Defs[id].Type().(*types.Signature)
102 args := sign.Params()
103 results := sign.Results()
104
105
106
107
108 if id.Name == "WriteTo" && args.Len() > 1 {
109 return
110 }
111
112
113
114 if id.Name == "Is" || id.Name == "As" || id.Name == "Unwrap" {
115 if recv := sign.Recv(); recv == nil || !implementsError(recv.Type()) {
116 return
117 }
118 }
119
120
121
122 if id.Name == "Unwrap" {
123 if args.Len() == 0 && results.Len() == 1 {
124 t := typeString(results.At(0).Type())
125 if t == "error" || t == "[]error" {
126 return
127 }
128 }
129 pass.ReportRangef(id, "method Unwrap() should have signature Unwrap() error or Unwrap() []error")
130 return
131 }
132
133
134 if !matchParams(pass, expect.args, args, "=") || !matchParams(pass, expect.results, results, "=") {
135 return
136 }
137
138
139 if !matchParams(pass, expect.args, args, "") || !matchParams(pass, expect.results, results, "") {
140 expectFmt := id.Name + "(" + argjoin(expect.args) + ")"
141 if len(expect.results) == 1 {
142 expectFmt += " " + argjoin(expect.results)
143 } else if len(expect.results) > 1 {
144 expectFmt += " (" + argjoin(expect.results) + ")"
145 }
146
147 actual := typeString(sign)
148 actual = strings.TrimPrefix(actual, "func")
149 actual = id.Name + actual
150
151 pass.ReportRangef(id, "method %s should have signature %s", actual, expectFmt)
152 }
153 }
154
155 func typeString(typ types.Type) string {
156 return types.TypeString(typ, (*types.Package).Name)
157 }
158
159 func argjoin(x []string) string {
160 y := make([]string, len(x))
161 for i, s := range x {
162 if s[0] == '=' {
163 s = s[1:]
164 }
165 y[i] = s
166 }
167 return strings.Join(y, ", ")
168 }
169
170
171 func matchParams(pass *analysis.Pass, expect []string, actual *types.Tuple, prefix string) bool {
172 for i, x := range expect {
173 if !strings.HasPrefix(x, prefix) {
174 continue
175 }
176 if i >= actual.Len() {
177 return false
178 }
179 if !matchParamType(x, actual.At(i).Type()) {
180 return false
181 }
182 }
183 if prefix == "" && actual.Len() > len(expect) {
184 return false
185 }
186 return true
187 }
188
189
190 func matchParamType(expect string, actual types.Type) bool {
191 expect = strings.TrimPrefix(expect, "=")
192
193 t := typeString(actual)
194 return t == expect ||
195 (t == "any" || t == "interface{}") && (expect == "any" || expect == "interface{}")
196 }
197
198 var errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
199
200 func implementsError(actual types.Type) bool {
201 return types.Implements(actual, errorType)
202 }
203
View as plain text