Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / go / analysis / passes / unmarshal / unmarshal.go
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 // The unmarshal package defines an Analyzer that checks for passing
6 // non-pointer or non-interface types to unmarshal and decode functions.
7 package unmarshal
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 )
18
19 const Doc = `report passing non-pointer or non-interface values to unmarshal
20
21 The unmarshal analysis reports calls to functions such as json.Unmarshal
22 in which the argument type is not a pointer or an interface.`
23
24 var Analyzer = &analysis.Analyzer{
25         Name:     "unmarshal",
26         Doc:      Doc,
27         Requires: []*analysis.Analyzer{inspect.Analyzer},
28         Run:      run,
29 }
30
31 func run(pass *analysis.Pass) (interface{}, error) {
32         switch pass.Pkg.Path() {
33         case "encoding/gob", "encoding/json", "encoding/xml", "encoding/asn1":
34                 // These packages know how to use their own APIs.
35                 // Sometimes they are testing what happens to incorrect programs.
36                 return nil, nil
37         }
38
39         inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
40
41         nodeFilter := []ast.Node{
42                 (*ast.CallExpr)(nil),
43         }
44         inspect.Preorder(nodeFilter, func(n ast.Node) {
45                 call := n.(*ast.CallExpr)
46                 fn := typeutil.StaticCallee(pass.TypesInfo, call)
47                 if fn == nil {
48                         return // not a static call
49                 }
50
51                 // Classify the callee (without allocating memory).
52                 argidx := -1
53                 recv := fn.Type().(*types.Signature).Recv()
54                 if fn.Name() == "Unmarshal" && recv == nil {
55                         // "encoding/json".Unmarshal
56                         // "encoding/xml".Unmarshal
57                         // "encoding/asn1".Unmarshal
58                         switch fn.Pkg().Path() {
59                         case "encoding/json", "encoding/xml", "encoding/asn1":
60                                 argidx = 1 // func([]byte, interface{})
61                         }
62                 } else if fn.Name() == "Decode" && recv != nil {
63                         // (*"encoding/json".Decoder).Decode
64                         // (* "encoding/gob".Decoder).Decode
65                         // (* "encoding/xml".Decoder).Decode
66                         t := recv.Type()
67                         if ptr, ok := t.(*types.Pointer); ok {
68                                 t = ptr.Elem()
69                         }
70                         tname := t.(*types.Named).Obj()
71                         if tname.Name() == "Decoder" {
72                                 switch tname.Pkg().Path() {
73                                 case "encoding/json", "encoding/xml", "encoding/gob":
74                                         argidx = 0 // func(interface{})
75                                 }
76                         }
77                 }
78                 if argidx < 0 {
79                         return // not a function we are interested in
80                 }
81
82                 if len(call.Args) < argidx+1 {
83                         return // not enough arguments, e.g. called with return values of another function
84                 }
85
86                 t := pass.TypesInfo.Types[call.Args[argidx]].Type
87                 switch t.Underlying().(type) {
88                 case *types.Pointer, *types.Interface:
89                         return
90                 }
91
92                 switch argidx {
93                 case 0:
94                         pass.Reportf(call.Lparen, "call of %s passes non-pointer", fn.Name())
95                 case 1:
96                         pass.Reportf(call.Lparen, "call of %s passes non-pointer as second argument", fn.Name())
97                 }
98         })
99         return nil, nil
100 }