| 1 | // Copyright 2019 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 deepequalerrors defines an Analyzer that checks for the use |
| 6 | // of reflect.DeepEqual with error values. |
| 7 | package deepequalerrors |
| 8 | |
| 9 | import ( |
| 10 | "go/ast" |
| 11 | "go/types" |
| 12 | |
| 13 | "golang.org/x/tools/go/analysis" |
| 14 | "golang.org/x/tools/go/analysis/passes/inspect" |
| 15 | "golang.org/x/tools/go/ast/inspector" |
| 16 | "golang.org/x/tools/go/types/typeutil" |
| 17 | "golang.org/x/tools/internal/typesinternal" |
| 18 | ) |
| 19 | |
| 20 | const Doc = `check for calls of reflect.DeepEqual on error values |
| 21 | |
| 22 | The deepequalerrors checker looks for calls of the form: |
| 23 | |
| 24 | reflect.DeepEqual(err1, err2) |
| 25 | |
| 26 | where err1 and err2 are errors. Using reflect.DeepEqual to compare |
| 27 | errors is discouraged.` |
| 28 | |
| 29 | var Analyzer = &analysis.Analyzer{ |
| 30 | Name: "deepequalerrors", |
| 31 | Doc: Doc, |
| 32 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/deepequalerrors", |
| 33 | Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 34 | Run: run, |
| 35 | } |
| 36 | |
| 37 | func run(pass *analysis.Pass) (any, error) { |
| 38 | if !typesinternal.Imports(pass.Pkg, "reflect") { |
| 39 | return nil, nil // doesn't directly import reflect |
| 40 | } |
| 41 | |
| 42 | inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 43 | |
| 44 | nodeFilter := []ast.Node{ |
| 45 | (*ast.CallExpr)(nil), |
| 46 | } |
| 47 | inspect.Preorder(nodeFilter, func(n ast.Node) { |
| 48 | call := n.(*ast.CallExpr) |
| 49 | obj := typeutil.Callee(pass.TypesInfo, call) |
| 50 | if typesinternal.IsFunctionNamed(obj, "reflect", "DeepEqual") && hasError(pass, call.Args[0]) && hasError(pass, call.Args[1]) { |
| 51 | pass.ReportRangef(call, "avoid using reflect.DeepEqual with errors") |
| 52 | } |
| 53 | }) |
| 54 | return nil, nil |
| 55 | } |
| 56 | |
| 57 | var errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface) |
| 58 | |
| 59 | // hasError reports whether the type of e contains the type error. |
| 60 | // See containsError, below, for the meaning of "contains". |
| 61 | func hasError(pass *analysis.Pass, e ast.Expr) bool { |
| 62 | tv, ok := pass.TypesInfo.Types[e] |
| 63 | if !ok { // no type info, assume good |
| 64 | return false |
| 65 | } |
| 66 | return containsError(tv.Type) |
| 67 | } |
| 68 | |
| 69 | // Report whether any type that typ could store and that could be compared is the |
| 70 | // error type. This includes typ itself, as well as the types of struct field, slice |
| 71 | // and array elements, map keys and elements, and pointers. It does not include |
| 72 | // channel types (incomparable), arg and result types of a Signature (not stored), or |
| 73 | // methods of a named or interface type (not stored). |
| 74 | func containsError(typ types.Type) bool { |
| 75 | // Track types being processed, to avoid infinite recursion. |
| 76 | // Using types as keys here is OK because we are checking for the identical pointer, not |
| 77 | // type identity. See analysis/passes/printf/types.go. |
| 78 | inProgress := make(map[types.Type]bool) |
| 79 | |
| 80 | var check func(t types.Type) bool |
| 81 | check = func(t types.Type) bool { |
| 82 | if t == errorType { |
| 83 | return true |
| 84 | } |
| 85 | if inProgress[t] { |
| 86 | return false |
| 87 | } |
| 88 | inProgress[t] = true |
| 89 | switch t := t.(type) { |
| 90 | case *types.Pointer: |
| 91 | return check(t.Elem()) |
| 92 | case *types.Slice: |
| 93 | return check(t.Elem()) |
| 94 | case *types.Array: |
| 95 | return check(t.Elem()) |
| 96 | case *types.Map: |
| 97 | return check(t.Key()) || check(t.Elem()) |
| 98 | case *types.Struct: |
| 99 | for field := range t.Fields() { |
| 100 | if check(field.Type()) { |
| 101 | return true |
| 102 | } |
| 103 | } |
| 104 | case *types.Named, *types.Alias: |
| 105 | return check(t.Underlying()) |
| 106 | |
| 107 | // We list the remaining valid type kinds for completeness. |
| 108 | case *types.Basic: |
| 109 | case *types.Chan: // channels store values, but they are not comparable |
| 110 | case *types.Signature: |
| 111 | case *types.Tuple: // tuples are only part of signatures |
| 112 | case *types.Interface: |
| 113 | } |
| 114 | return false |
| 115 | } |
| 116 | |
| 117 | return check(typ) |
| 118 | } |