-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrwrapfmt.go
44 lines (36 loc) · 896 Bytes
/
errwrapfmt.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
package errwrapfmt
import (
"go/ast"
"regexp"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Analyzer finds wrong error wrap format.
var Analyzer = &analysis.Analyzer{
Name: "errwrapfmt",
Doc: "finds wrong error wrap format",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
var (
wrapReg = regexp.MustCompile(`%w`)
rightWrapReg = regexp.MustCompile(`: %w`)
)
func run(pass *analysis.Pass) (any, error) {
ins := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.BasicLit)(nil),
}
ins.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.BasicLit:
if wrapReg.MatchString(n.Value) && !rightWrapReg.MatchString(n.Value) {
pass.Reportf(n.Pos(), "wrong error wrap format")
}
}
})
return nil, nil
}