honnef.co/go/tools@v0.4.7/staticcheck/testdata/src/example.com/CheckNoopMarshal/CheckNoopMarshal.go (about)

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