Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / staticcheck / testdata / src / CheckNoopMarshal / CheckNoopMarshal.go
1 package pkg
2
3 import (
4         "encoding/json"
5         "encoding/xml"
6 )
7
8 type T1 struct{}
9 type T2 struct{ x int }
10 type T3 struct{ X int }
11 type T4 struct{ T3 }
12 type t5 struct{ X int }
13 type T6 struct{ t5 }
14 type T7 struct{ x int }
15
16 func (T7) MarshalJSON() ([]byte, error) { return nil, nil }
17 func (*T7) UnmarshalJSON([]byte) error  { return nil }
18
19 type T8 struct{ x int }
20
21 func (T8) MarshalXML() ([]byte, error)                         { return nil, nil }
22 func (*T8) UnmarshalXML(*xml.Decoder, *xml.StartElement) error { return nil }
23
24 type T9 struct{}
25
26 func (T9) MarshalText() ([]byte, error) { return nil, nil }
27 func (*T9) UnmarshalText([]byte) error  { return nil }
28
29 type T10 struct{}
30 type T11 struct{ T10 }
31 type T12 struct{ T7 }
32 type t13 struct{}
33
34 func (t13) MarshalJSON() ([]byte, error) { return nil, nil }
35
36 type T14 struct{ t13 }
37 type T15 struct{ *t13 }
38 type T16 struct{ *T3 }
39 type T17 struct{ *T17 }
40 type T18 struct {
41         T17
42         Actual int
43 }
44
45 func fn() {
46         // don't flag structs with no fields
47         json.Marshal(T1{})
48         // no exported fields
49         json.Marshal(T2{}) // want `struct doesn't have any exported fields, nor custom marshaling`
50         // pointer vs non-pointer makes no difference
51         json.Marshal(&T2{}) // want `struct doesn't have any exported fields, nor custom marshaling`
52         // exported field
53         json.Marshal(T3{})
54         // exported field, pointer makes no difference
55         json.Marshal(&T3{})
56         // embeds struct with exported fields
57         json.Marshal(T4{})
58         // exported field
59         json.Marshal(t5{})
60         // embeds unexported type, but said type does have exported fields
61         json.Marshal(T6{})
62         // MarshalJSON
63         json.Marshal(T7{})
64         // MarshalXML does not apply to JSON
65         json.Marshal(T8{}) // want `struct doesn't have any exported fields, nor custom marshaling`
66         // MarshalText
67         json.Marshal(T9{})
68         // embeds exported struct, but it has no fields
69         json.Marshal(T11{}) // want `struct doesn't have any exported fields, nor custom marshaling`
70         // embeds type with MarshalJSON
71         json.Marshal(T12{})
72         // embeds type with MarshalJSON and type isn't exported
73         json.Marshal(T14{})
74         // embedded pointer with MarshalJSON
75         json.Marshal(T15{})
76         // embedded pointer to struct with exported fields
77         json.Marshal(T16{})
78         // don't recurse forever on recursive data structure
79         json.Marshal(T17{}) // want `struct doesn't have any exported fields, nor custom marshaling`
80         json.Marshal(T18{})
81
82         // MarshalJSON does not apply to JSON
83         xml.Marshal(T7{}) // want `struct doesn't have any exported fields, nor custom marshaling`
84         // MarshalXML
85         xml.Marshal(T8{})
86
87         var t2 T2
88         var t3 T3
89         var t7 T7
90         var t8 T8
91         var t9 T9
92         // check that all other variations of methods also work
93         json.Unmarshal(nil, &t2) // want `struct doesn't have any exported fields, nor custom marshaling`
94         json.Unmarshal(nil, &t3)
95         json.Unmarshal(nil, &t9)
96         xml.Unmarshal(nil, &t2) // want `struct doesn't have any exported fields, nor custom marshaling`
97         xml.Unmarshal(nil, &t3)
98         xml.Unmarshal(nil, &t9)
99         (*json.Decoder)(nil).Decode(&t2) // want `struct doesn't have any exported fields, nor custom marshaling`
100         (*json.Decoder)(nil).Decode(&t3)
101         (*json.Decoder)(nil).Decode(&t9)
102         (*json.Encoder)(nil).Encode(t2) // want `struct doesn't have any exported fields, nor custom marshaling`
103         (*json.Encoder)(nil).Encode(t3)
104         (*json.Encoder)(nil).Encode(t9)
105         (*xml.Decoder)(nil).Decode(&t2) // want `struct doesn't have any exported fields, nor custom marshaling`
106         (*xml.Decoder)(nil).Decode(&t3)
107         (*xml.Decoder)(nil).Decode(&t9)
108         (*xml.Encoder)(nil).Encode(t2) // want `struct doesn't have any exported fields, nor custom marshaling`
109         (*xml.Encoder)(nil).Encode(t3)
110         (*xml.Encoder)(nil).Encode(t9)
111
112         (*json.Decoder)(nil).Decode(&t7)
113         (*json.Decoder)(nil).Decode(&t8) // want `struct doesn't have any exported fields, nor custom marshaling`
114         (*json.Encoder)(nil).Encode(t7)
115         (*json.Encoder)(nil).Encode(t8) // want `struct doesn't have any exported fields, nor custom marshaling`
116         (*xml.Decoder)(nil).Decode(&t7) // want `struct doesn't have any exported fields, nor custom marshaling`
117         (*xml.Decoder)(nil).Decode(&t8)
118         (*xml.Encoder)(nil).Encode(t7) // want `struct doesn't have any exported fields, nor custom marshaling`
119         (*xml.Encoder)(nil).Encode(t8)
120
121 }
122
123 var _, _ = json.Marshal(T9{})