| 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package errorsas defines an Analyzer that checks that the second argument to |
| 6 | // [errors.As] is a pointer to a type implementing error. |
| 7 | package errorsas |
| 8 | |
| 9 | import ( |
| 10 | "errors" |
| 11 | "go/ast" |
| 12 | "go/types" |
| 13 | |
| 14 | "golang.org/x/tools/go/analysis" |
| 15 | typeindexanalyzer "golang.org/x/tools/internal/analysis/typeindex" |
| 16 | "golang.org/x/tools/internal/typesinternal/typeindex" |
| 17 | ) |
| 18 | |
| 19 | const Doc = `report passing non-pointer or non-error values to errors.As |
| 20 | |
| 21 | The errorsas analyzer reports calls to errors.As where the type |
| 22 | of the second argument is not a pointer to a type implementing error. |
| 23 | For example: |
| 24 | |
| 25 | var unwrappedErr net.DNSError |
| 26 | errors.As(err, unwrappedErr) // should use &unwrappedErr, DNSError.Error has a pointer reciever |
| 27 | ` |
| 28 | |
| 29 | var Analyzer = &analysis.Analyzer{ |
| 30 | Name: "errorsas", |
| 31 | Doc: Doc, |
| 32 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/errorsas", |
| 33 | Requires: []*analysis.Analyzer{typeindexanalyzer.Analyzer}, |
| 34 | Run: run, |
| 35 | } |
| 36 | |
| 37 | func run(pass *analysis.Pass) (any, error) { |
| 38 | switch pass.Pkg.Path() { |
| 39 | case "errors", "errors_test": |
| 40 | // These packages know how to use their own APIs. |
| 41 | // Sometimes they are testing what happens to incorrect programs. |
| 42 | return nil, nil |
| 43 | } |
| 44 | |
| 45 | var ( |
| 46 | index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) |
| 47 | info = pass.TypesInfo |
| 48 | ) |
| 49 | |
| 50 | for curCall := range index.Calls(index.Object("errors", "As")) { |
| 51 | call := curCall.Node().(*ast.CallExpr) |
| 52 | if len(call.Args) < 2 { |
| 53 | continue // spread call: errors.As(pair()) |
| 54 | } |
| 55 | |
| 56 | // Check for incorrect arguments. |
| 57 | if err := checkAsTarget(info, call.Args[1]); err != nil { |
| 58 | pass.ReportRangef(call, "%v", err) |
| 59 | continue |
| 60 | } |
| 61 | } |
| 62 | return nil, nil |
| 63 | } |
| 64 | |
| 65 | // checkAsTarget reports an error if the second argument to errors.As is invalid. |
| 66 | func checkAsTarget(info *types.Info, e ast.Expr) error { |
| 67 | t := info.Types[e].Type |
| 68 | if types.Identical(t.Underlying(), anyType) { |
| 69 | // A target of any is always allowed, since it often indicates |
| 70 | // a value forwarded from another source. |
| 71 | return nil |
| 72 | } |
| 73 | pt, ok := t.Underlying().(*types.Pointer) |
| 74 | if !ok { |
| 75 | return errors.New("second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type") |
| 76 | } |
| 77 | if types.Identical(pt.Elem(), errorType) { |
| 78 | return errors.New("second argument to errors.As should not be *error") |
| 79 | } |
| 80 | if !types.IsInterface(pt.Elem()) && !types.AssignableTo(pt.Elem(), errorType) { |
| 81 | return errors.New("second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type") |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | var ( |
| 87 | anyType = types.Universe.Lookup("any").Type() |
| 88 | errorType = types.Universe.Lookup("error").Type() |
| 89 | ) |