Debian Codegraph @

staticcheck/sa9005/testdata/go1.0/CheckNoopMarshal/CheckNoopMarshal.go

raw
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 type t19 string
46 type T20 string
47
48 type T21 struct{ t19 }
49 type T22 struct{ T20 }
50 type T23 struct{ *T20 }
51
52 func fn() {
53 // don't flag structs with no fields
54 json.Marshal(T1{})
55 // no exported fields
56 json.Marshal(T2{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
57 json.MarshalIndent(T2{}, "", " ") //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
58 // pointer vs non-pointer makes no difference
59 json.Marshal(&T2{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
60 // exported field
61 json.Marshal(T3{})
62 // exported field, pointer makes no difference
63 json.Marshal(&T3{})
64 // embeds struct with exported fields
65 json.Marshal(T4{})
66 // exported field
67 json.Marshal(t5{})
68 // embeds unexported type, but said type does have exported fields
69 json.Marshal(T6{})
70 // MarshalJSON
71 json.Marshal(T7{})
72 // MarshalXML does not apply to JSON
73 json.Marshal(T8{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T8' doesn't have any exported fields, nor custom marshaling`)
74 // MarshalText
75 json.Marshal(T9{})
76 // embeds exported struct, but it has no fields
77 json.Marshal(T11{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T11' doesn't have any exported fields, nor custom marshaling`)
78 // embeds type with MarshalJSON
79 json.Marshal(T12{})
80 // embeds type with MarshalJSON and type isn't exported
81 json.Marshal(T14{})
82 // embedded pointer with MarshalJSON
83 json.Marshal(T15{})
84 // embedded pointer to struct with exported fields
85 json.Marshal(T16{})
86 // don't recurse forever on recursive data structure
87 json.Marshal(T17{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T17' doesn't have any exported fields, nor custom marshaling`)
88 json.Marshal(T18{})
89
90 // embedding an unexported, non-struct type
91 json.Marshal(T21{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T21' doesn't have any exported fields, nor custom marshaling`)
92 // embedding an exported, non-struct type
93 json.Marshal(T22{})
94 // embedding an exported, non-struct type
95 json.Marshal(T23{})
96
97 // MarshalJSON does not apply to XML
98 xml.Marshal(T7{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T7' doesn't have any exported fields, nor custom marshaling`)
99 xml.MarshalIndent(T7{}, "", " ") //@ diag(`struct type 'example.com/CheckNoopMarshal.T7' doesn't have any exported fields, nor custom marshaling`)
100 // MarshalXML
101 xml.Marshal(T8{})
102
103 var t2 T2
104 var t3 T3
105 var t7 T7
106 var t8 T8
107 var t9 T9
108 // check that all other variations of methods also work
109 json.Unmarshal(nil, &t2) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
110 json.Unmarshal(nil, &t3)
111 json.Unmarshal(nil, &t9)
112 xml.Unmarshal(nil, &t2) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
113 xml.Unmarshal(nil, &t3)
114 xml.Unmarshal(nil, &t9)
115 (*json.Decoder)(nil).Decode(&t2) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
116 (*json.Decoder)(nil).Decode(&t3)
117 (*json.Decoder)(nil).Decode(&t9)
118 (*json.Encoder)(nil).Encode(t2) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
119 (*json.Encoder)(nil).Encode(t3)
120 (*json.Encoder)(nil).Encode(t9)
121 (*xml.Decoder)(nil).Decode(&t2) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
122 (*xml.Decoder)(nil).Decode(&t3)
123 (*xml.Decoder)(nil).Decode(&t9)
124 (*xml.Encoder)(nil).Encode(t2) //@ diag(`struct type 'example.com/CheckNoopMarshal.T2' doesn't have any exported fields, nor custom marshaling`)
125 (*xml.Encoder)(nil).Encode(t3)
126 (*xml.Encoder)(nil).Encode(t9)
127
128 (*json.Decoder)(nil).Decode(&t7)
129 (*json.Decoder)(nil).Decode(&t8) //@ diag(`struct type 'example.com/CheckNoopMarshal.T8' doesn't have any exported fields, nor custom marshaling`)
130 (*json.Encoder)(nil).Encode(t7)
131 (*json.Encoder)(nil).Encode(t8) //@ diag(`struct type 'example.com/CheckNoopMarshal.T8' doesn't have any exported fields, nor custom marshaling`)
132 (*xml.Decoder)(nil).Decode(&t7) //@ diag(`struct type 'example.com/CheckNoopMarshal.T7' doesn't have any exported fields, nor custom marshaling`)
133 (*xml.Decoder)(nil).Decode(&t8)
134 (*xml.Encoder)(nil).Encode(t7) //@ diag(`struct type 'example.com/CheckNoopMarshal.T7' doesn't have any exported fields, nor custom marshaling`)
135 (*xml.Encoder)(nil).Encode(t8)
136
137 }
138
139 var _, _ = json.Marshal(T9{})