1 // Package lintdsl provides helpers for implementing static analysis
2 // checks. Dot-importing this package is encouraged.
11 "golang.org/x/tools/go/analysis"
12 "honnef.co/go/tools/pattern"
15 func Inspect(node ast.Node, fn func(node ast.Node) bool) {
22 func Match(pass *analysis.Pass, q pattern.Pattern, node ast.Node) (*pattern.Matcher, bool) {
23 // Note that we ignore q.Relevant – callers of Match usually use
24 // AST inspectors that already filter on nodes we're interested
26 m := &pattern.Matcher{TypesInfo: pass.TypesInfo}
27 ok := m.Match(q.Root, node)
31 func MatchAndEdit(pass *analysis.Pass, before, after pattern.Pattern, node ast.Node) (*pattern.Matcher, []analysis.TextEdit, bool) {
32 m, ok := Match(pass, before, node)
36 r := pattern.NodeToAST(after.Root, m.State)
37 buf := &bytes.Buffer{}
38 format.Node(buf, pass.Fset, r)
39 edit := []analysis.TextEdit{{
47 func Selector(x, sel string) *ast.SelectorExpr {
48 return &ast.SelectorExpr{
49 X: &ast.Ident{Name: x},
50 Sel: &ast.Ident{Name: sel},
54 // ExhaustiveTypeSwitch panics when called. It can be used to ensure
55 // that type switches are exhaustive.
56 func ExhaustiveTypeSwitch(v interface{}) {
57 panic(fmt.Sprintf("internal error: unhandled case %T", v))