forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrchkjson.go
641 lines (539 loc) · 26.9 KB
/
errchkjson.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//golangcitest:args -Eerrchkjson
//golangcitest:config_path testdata/configs/errchkjson.yml
package testdata
import (
"encoding"
"encoding/json"
"fmt"
"io/ioutil"
"unsafe"
)
type marshalText struct{}
func (mt marshalText) MarshalText() ([]byte, error) {
return []byte(`mt`), nil
}
var _ encoding.TextMarshaler = marshalText(struct{}{})
// JSONMarshalSafeTypesWithNoSafe contains a multitude of test cases to marshal different combinations of types to JSON,
// that are safe, that is, they will never return an error, if these types are marshaled to JSON.
func JSONMarshalSafeTypesWithNoSafe() {
var err error
_, _ = json.Marshal(nil) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
json.Marshal(nil) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(nil) // nil is safe and check-error-free-encoding is false
_ = err
_, _ = json.MarshalIndent(nil, "", " ") // ERROR "Error return value of `encoding/json.MarshalIndent` is not checked"
json.MarshalIndent(nil, "", " ") // ERROR "Error return value of `encoding/json.MarshalIndent` is not checked"
_, err = json.MarshalIndent(nil, "", " ") // nil is safe and check-error-free-encoding is false
_ = err
enc := json.NewEncoder(ioutil.Discard)
_ = enc.Encode(nil) // ERROR "Error return value of `\\([*]encoding/json.Encoder\\).Encode` is not checked"
enc.Encode(nil) // ERROR "Error return value of `\\([*]encoding/json.Encoder\\).Encode` is not checked"
err = enc.Encode(nil) // nil is safe and check-error-free-encoding is false
_ = err
var b bool
_, _ = json.Marshal(b) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(b) // bool is safe and check-error-free-encoding is false
_ = err
var i int
_, _ = json.Marshal(i) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(i) // int is safe and check-error-free-encoding is false
_ = err
var i8 int8
_, _ = json.Marshal(i8) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(i8) // int8 is safe and check-error-free-encoding is false
_ = err
var i16 int16
_, _ = json.Marshal(i16) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(i16) // int16 is safe and check-error-free-encoding is false
_ = err
var i32 int32
_, _ = json.Marshal(i32) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(i32) // int32 / rune is safe and check-error-free-encoding is false
_ = err
var i64 int64
_, _ = json.Marshal(i64) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(i64) // int64 is safe and check-error-free-encoding is false
_ = err
var ui uint
_, _ = json.Marshal(ui) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(ui) // uint is safe and check-error-free-encoding is false
_ = err
var ui8 uint8
_, _ = json.Marshal(ui8) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(ui8) // uint8 is safe and check-error-free-encoding is false
_ = err
var ui16 uint16
_, _ = json.Marshal(ui16) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(ui16) // uint16 is safe and check-error-free-encoding is false
_ = err
var ui32 uint32
_, _ = json.Marshal(ui32) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(ui32) // uint32 is safe and check-error-free-encoding is false
_ = err
var ui64 uint64
_, _ = json.Marshal(ui64) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(ui64) // uint64 is safe and check-error-free-encoding is false
_ = err
var uiptr uintptr
_, _ = json.Marshal(uiptr) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(uiptr) // uintptr is safe and check-error-free-encoding is false
_ = err
var str string
_, _ = json.Marshal(str) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(str) // string is safe and check-error-free-encoding is false
_ = err
var strSlice []string
_, _ = json.Marshal(strSlice) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(strSlice) // []string is safe and check-error-free-encoding is false
_ = err
var intSlice []int
_, _ = json.Marshal(intSlice) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(intSlice) // []int is safe and check-error-free-encoding is false
_ = err
var boolSlice []bool
_, _ = json.Marshal(boolSlice) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(boolSlice) // []bool is safe and check-error-free-encoding is false
_ = err
var strArray [10]string
_, _ = json.Marshal(strArray) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(strArray) // [10]string is safe and check-error-free-encoding is false
_ = err
var intArray [10]int
_, _ = json.Marshal(intArray) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(intArray) // [10]int is safe and check-error-free-encoding is false
_ = err
var boolArray [10]bool
_, _ = json.Marshal(boolArray) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(boolArray) // [10]bool is safe and check-error-free-encoding is false
_ = err
var basicStruct struct {
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32 // also rune
Int64 int64
Uint uint
Uint8 uint8 // also byte
Uint16 uint16
Uint32 uint32
Uint64 uint64
Uintptr uintptr
String string
}
_, _ = json.Marshal(basicStruct) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(basicStruct) // struct containing only safe basic types is safe and check-error-free-encoding is false
_ = err
var ptrStruct struct {
Bool *bool
Int *int
Int8 *int8
Int16 *int16
Int32 *int32
Int64 *int64
Uint *uint
Uint8 *uint8
Uint16 *uint16
Uint32 *uint32
Uint64 *uint64
Uintptr *uintptr
String *string
}
_, _ = json.Marshal(ptrStruct) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(ptrStruct) // struct containing pointer to only safe basic types is safe and check-error-free-encoding is false
_ = err
var mapStrStr map[string]string
_, _ = json.Marshal(mapStrStr) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(mapStrStr) // map[string]string is safe and check-error-free-encoding is false
_ = err
var mapStrInt map[string]int
_, _ = json.Marshal(mapStrInt) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(mapStrInt) // map[string]int is safe and check-error-free-encoding is false
_ = err
var mapStrBool map[string]bool
_, _ = json.Marshal(mapStrBool) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(mapStrBool) // map[string]bool is safe and check-error-free-encoding is false
_ = err
var mapIntStr map[int]string
_, _ = json.Marshal(mapIntStr) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(mapIntStr) // map[int]string is safe and check-error-free-encoding is false
_ = err
var mapIntInt map[int]int
_, _ = json.Marshal(mapIntInt) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(mapIntInt) // map[int]int is safe and check-error-free-encoding is false
_ = err
var mapIntBool map[int]bool
_, _ = json.Marshal(mapIntBool) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(mapIntBool) // map[int]bool is safe and check-error-free-encoding is false
_ = err
type innerStruct struct {
Bool bool
Int int
String string
StrSlice []string
IntSlice []int
BoolSlice []bool
StrArray [10]string
IntArray [10]int
BoolArray [10]bool
MapStrStr map[string]string
MapStrInt map[string]int
MapStrBool map[string]bool
MapIntStr map[int]string
MapIntInt map[int]int
MapIntBool map[int]bool
}
var outerStruct struct {
Bool bool
Int int
String string
StrSlice []string
IntSlice []int
BoolSlice []bool
StrArray [10]string
IntArray [10]int
BoolArray [10]bool
MapStrStr map[string]string
MapStrInt map[string]int
MapStrBool map[string]bool
MapIntStr map[int]string
MapIntInt map[int]int
MapIntBool map[int]bool
InnerStruct innerStruct
}
_, _ = json.Marshal(outerStruct) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(outerStruct) // struct with only safe types is safe and check-error-free-encoding is false
_ = err
}
type (
structKey struct{ id int }
ExportedUnsafeAndInvalidStruct struct { // unsafe unexported but omitted
F64 float64
F64Ptr *float64
F64Slice []float64
F64Array [10]float64
MapStrF64 map[string]float64
MapEIStr map[interface{}]string
Number json.Number
NumberPtr *json.Number
NumberSlice []json.Number
MapNumberStr map[json.Number]string
Ei interface{}
Stringer fmt.Stringer
Mt marshalText
MapMarshalTextString map[marshalText]string
C128 complex128
C128Ptr *complex128
C128Slice []complex128
C128Array [10]complex128
MapBoolStr map[bool]string
MapF64Str map[float64]string
F func()
Ch chan struct{}
UnsafePtr unsafe.Pointer
MapStructStr map[structKey]string
}
)
// JSONMarshalSafeStructWithUnexportedFieldsWithNoSafe contains a struct with unexported, unsafe fields.
func JSONMarshalSaveStructWithUnexportedFieldsWithNoSafe() {
var err error
var unexportedInStruct struct {
Bool bool // safe exported
f64 float64 // unsafe unexported
f64Ptr *float64 // unsafe unexported
f64Slice []float64 // unsafe unexported
f64Array [10]float64 // unsafe unexported
mapStrF64 map[string]float64 // unsafe unexported
mapEIStr map[interface{}]string // unsafe unexported
number json.Number // unsafe unexported
numberPtr *json.Number // unsafe unexported
numberSlice []json.Number // unsafe unexported
mapNumberStr map[json.Number]string // unsafe unexported
ei interface{} // unsafe unexported
stringer fmt.Stringer // unsafe unexported
mt marshalText // unsafe unexported
mapMarshalTextString map[marshalText]string // unsafe unexported
unexportedStruct ExportedUnsafeAndInvalidStruct // unsafe unexported
unexportedStructPtr *ExportedUnsafeAndInvalidStruct // unsafe unexported
c128 complex128 // invalid unexported
c128Slice []complex128 // invalid unexported
c128Array [10]complex128 // invalid unexported
mapBoolStr map[bool]string // invalid unexported
mapF64Str map[float64]string // invalid unexported
f func() // invalid unexported
ch chan struct{} // invalid unexported
unsafePtr unsafe.Pointer // invalid unexported
mapStructStr map[structKey]string // invalid unexported
}
_ = unexportedInStruct.f64
_ = unexportedInStruct.f64Ptr
_ = unexportedInStruct.f64Slice
_ = unexportedInStruct.f64Array
_ = unexportedInStruct.mapStrF64
_ = unexportedInStruct.mapEIStr
_ = unexportedInStruct.number
_ = unexportedInStruct.numberPtr
_ = unexportedInStruct.numberSlice
_ = unexportedInStruct.mapNumberStr
_ = unexportedInStruct.ei
_ = unexportedInStruct.stringer
_ = unexportedInStruct.mt
_ = unexportedInStruct.mapMarshalTextString
_ = unexportedInStruct.unexportedStruct
_ = unexportedInStruct.unexportedStructPtr
_ = unexportedInStruct.c128
_ = unexportedInStruct.c128Slice
_ = unexportedInStruct.c128Array
_ = unexportedInStruct.mapBoolStr
_ = unexportedInStruct.mapF64Str
_ = unexportedInStruct.f
_ = unexportedInStruct.ch
_ = unexportedInStruct.unsafePtr
_ = unexportedInStruct.mapStructStr[structKey{1}]
_, _ = json.Marshal(unexportedInStruct) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(unexportedInStruct) // struct containing unsafe but unexported fields is safe
_ = err
}
// JSONMarshalSafeStructWithOmittedFieldsWithNoSafe contains a struct with omitted, unsafe fields.
func JSONMarshalSaveStructWithOmittedFieldsWithNoSafe() {
var err error
var omitInStruct struct {
Bool bool // safe exported
F64 float64 `json:"-"` // unsafe exported but omitted
F64Ptr *float64 `json:"-"` // unsafe exported but omitted
F64Slice []float64 `json:"-"` // unsafe exported but omitted
F64Array [10]float64 `json:"-"` // unsafe exported but omitted
MapStrF64 map[string]float64 `json:"-"` // unsafe exported but omitted
MapEIStr map[interface{}]string `json:"-"` // unsafe exported but omitted
Number json.Number `json:"-"` // unsafe exported but omitted
NumberPtr *json.Number `json:"-"` // unsafe exported but omitted
NumberSlice []json.Number `json:"-"` // unsafe exported but omitted
MapNumberStr map[json.Number]string `json:"-"` // unsafe exported but omitted
Ei interface{} `json:"-"` // unsafe exported but omitted
Stringer fmt.Stringer `json:"-"` // unsafe exported but omitted
Mt marshalText `json:"-"` // unsafe exported but omitted
MapMarshalTextString map[marshalText]string `json:"-"` // unsafe exported but omitted
ExportedStruct ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but omitted
ExportedStructPtr *ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but omitted
C128 complex128 `json:"-"` // invalid exported but omitted
C128Slice []complex128 `json:"-"` // invalid exported but omitted
C128Array [10]complex128 `json:"-"` // invalid exported but omitted
MapBoolStr map[bool]string `json:"-"` // invalid exported but omitted
MapF64Str map[float64]string `json:"-"` // invalid exported but omitted
F func() `json:"-"` // invalid exported but omitted
Ch chan struct{} `json:"-"` // invalid exported but omitted
UnsafePtr unsafe.Pointer `json:"-"` // invalid exported but omitted
MapStructStr map[structKey]string `json:"-"` // invalid exported but omitted
}
_ = omitInStruct.MapStructStr[structKey{1}]
_, _ = json.Marshal(omitInStruct) // ERROR "Error return value of `encoding/json.Marshal` is not checked"
_, err = json.Marshal(omitInStruct) // struct containing unsafe but omitted, exported fields is safe and check-error-free-encoding is false
_ = err
}
// JSONMarshalUnsafeTypes contains a multitude of test cases to marshal different combinations of types to JSON,
// that can potentially lead to json.Marshal returning an error.
func JSONMarshalUnsafeTypes() {
var err error
var f32 float32
json.Marshal(f32) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
_, _ = json.Marshal(f32) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
_, err = json.Marshal(f32) // err is checked
_ = err
var f64 float64
_, _ = json.Marshal(f64) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float64` found"
_, err = json.Marshal(f64) // err is checked
_ = err
var f32Slice []float32
_, _ = json.Marshal(f32Slice) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
_, err = json.Marshal(f32Slice) // err is checked
_ = err
var f64Slice []float64
_, _ = json.Marshal(f64Slice) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float64` found"
_, err = json.Marshal(f64Slice) // err is checked
_ = err
var f32Array [10]float32
_, _ = json.Marshal(f32Array) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
_, err = json.Marshal(f32Array) // err is checked
_ = err
var f64Array [10]float64
_, _ = json.Marshal(f64Array) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float64` found"
_, err = json.Marshal(f64Array) // err is checked
_ = err
var structPtrF32 struct {
F32 *float32
}
_, _ = json.Marshal(structPtrF32) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
_, err = json.Marshal(structPtrF32) // err is checked
_ = err
var structPtrF64 struct {
F64 *float64
}
_, _ = json.Marshal(structPtrF64) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float64` found"
_, err = json.Marshal(structPtrF64) // err is checked
_ = err
var mapStrF32 map[string]float32
_, _ = json.Marshal(mapStrF32) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
_, err = json.Marshal(mapStrF32) // err is checked
_ = err
var mapStrF64 map[string]float64
_, _ = json.Marshal(mapStrF64) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float64` found"
_, err = json.Marshal(mapStrF64) // err is checked
_ = err
var mapEIStr map[interface{}]string
_, _ = json.Marshal(mapEIStr) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `interface{}` as map key found"
_, err = json.Marshal(mapEIStr) // err is checked
_ = err
var mapStringerStr map[fmt.Stringer]string
_, _ = json.Marshal(mapStringerStr) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `fmt.Stringer` as map key found"
_, err = json.Marshal(mapStringerStr) // err is checked
_ = err
var number json.Number
_, _ = json.Marshal(number) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `encoding/json.Number` found"
_, err = json.Marshal(number) // err is checked
_ = err
var numberSlice []json.Number
_, _ = json.Marshal(numberSlice) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `encoding/json.Number` found"
_, err = json.Marshal(numberSlice) // err is checked
_ = err
var mapNumberStr map[json.Number]string
_, _ = json.Marshal(mapNumberStr) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `encoding/json.Number` as map key found"
_, err = json.Marshal(mapNumberStr) // err is checked
_ = err
var mapStrNumber map[string]json.Number
_, _ = json.Marshal(mapStrNumber) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `encoding/json.Number` found"
_, err = json.Marshal(mapStrNumber) // err is checked
_ = err
var ei interface{}
_, _ = json.Marshal(ei) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `interface{}` found"
_, err = json.Marshal(ei) // err is checked
_ = err
var eiptr *interface{}
_, _ = json.Marshal(eiptr) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `*interface{}` found"
_, err = json.Marshal(eiptr) // err is checked
_ = err
var stringer fmt.Stringer
_, _ = json.Marshal(stringer) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `fmt.Stringer` found"
_, err = json.Marshal(stringer) // err is checked
_ = err
var structWithEmptyInterface struct {
EmptyInterface interface{}
}
_, _ = json.Marshal(structWithEmptyInterface) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `interface{}` found"
_, err = json.Marshal(structWithEmptyInterface) // err is checked
_ = err
var mt marshalText
_, _ = json.Marshal(mt) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `[a-z-]+.marshalText` found"
_, err = json.Marshal(mt) // err is checked
_ = err
var mapMarshalTextString map[marshalText]string
_, _ = json.Marshal(mapMarshalTextString) // ERROR "Error return value of `encoding/json.Marshal` is not checked: unsafe type `[a-z-]+.marshalText` as map key found"
_, err = json.Marshal(mapMarshalTextString) // err is checked
_ = err
}
// JSONMarshalInvalidTypes contains a multitude of test cases to marshal different combinations of types to JSON,
// that are invalid and not supported by json.Marshal, that is they will always return an error, if these types used
// with json.Marshal.
func JSONMarshalInvalidTypes() {
var err error
var c64 complex64
json.Marshal(c64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_, _ = json.Marshal(c64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_, err = json.Marshal(c64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_ = err
var c128 complex128
_, _ = json.Marshal(c128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_, err = json.Marshal(c128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_ = err
var sliceC64 []complex64
_, _ = json.Marshal(sliceC64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_, err = json.Marshal(sliceC64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_ = err
var sliceC128 []complex128
_, _ = json.Marshal(sliceC128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_, err = json.Marshal(sliceC128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_ = err
var arrayC64 []complex64
_, _ = json.Marshal(arrayC64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_, err = json.Marshal(arrayC64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_ = err
var arrayC128 []complex128
_, _ = json.Marshal(arrayC128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_, err = json.Marshal(arrayC128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_ = err
var structPtrC64 struct {
C64 *complex64
}
_, _ = json.Marshal(structPtrC64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_, err = json.Marshal(structPtrC64) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` found"
_ = err
var structPtrC128 struct {
C128 *complex128
}
_, _ = json.Marshal(structPtrC128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_, err = json.Marshal(structPtrC128) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` found"
_ = err
var mapBoolStr map[bool]string
_, _ = json.Marshal(mapBoolStr) // ERROR "`encoding/json.Marshal` for unsupported type `bool` as map key found"
_, err = json.Marshal(mapBoolStr) // ERROR "`encoding/json.Marshal` for unsupported type `bool` as map key found"
_ = err
var mapF32Str map[float32]string
_, _ = json.Marshal(mapF32Str) // ERROR "`encoding/json.Marshal` for unsupported type `float32` as map key found"
_, err = json.Marshal(mapF32Str) // ERROR "`encoding/json.Marshal` for unsupported type `float32` as map key found"
_ = err
var mapF64Str map[float64]string
_, _ = json.Marshal(mapF64Str) // ERROR "`encoding/json.Marshal` for unsupported type `float64` as map key found"
_, err = json.Marshal(mapF64Str) // ERROR "`encoding/json.Marshal` for unsupported type `float64` as map key found"
_ = err
var mapC64Str map[complex64]string
_, _ = json.Marshal(mapC64Str) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` as map key found"
_, err = json.Marshal(mapC64Str) // ERROR "`encoding/json.Marshal` for unsupported type `complex64` as map key found"
_ = err
var mapC128Str map[complex128]string
_, _ = json.Marshal(mapC128Str) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` as map key found"
_, err = json.Marshal(mapC128Str) // ERROR "`encoding/json.Marshal` for unsupported type `complex128` as map key found"
_ = err
mapStructStr := map[structKey]string{structKey{1}: "str"}
_, _ = json.Marshal(mapStructStr) // ERROR "`encoding/json.Marshal` for unsupported type `[a-z-]+.structKey` as map key found"
_, err = json.Marshal(mapStructStr) // ERROR "`encoding/json.Marshal` for unsupported type `[a-z-]+.structKey` as map key found"
_ = err
f := func() {}
_, _ = json.Marshal(f) // ERROR "`encoding/json.Marshal` for unsupported type `func\\(\\)` found"
_, err = json.Marshal(f) // ERROR "`encoding/json.Marshal` for unsupported type `func\\(\\)` found"
_ = err
var ch chan struct{} = make(chan struct{})
_, _ = json.Marshal(ch) // ERROR "`encoding/json.Marshal` for unsupported type `chan struct{}` found"
_, err = json.Marshal(ch) // ERROR "`encoding/json.Marshal` for unsupported type `chan struct{}` found"
_ = err
var unsafePtr unsafe.Pointer
_, _ = json.Marshal(unsafePtr) // ERROR "`encoding/json.Marshal` for unsupported type `unsafe.Pointer` found"
_, err = json.Marshal(unsafePtr) // ERROR "`encoding/json.Marshal` for unsupported type `unsafe.Pointer` found"
_ = err
}
// NotJSONMarshal contains other go ast node types, that are not considered by errchkjson
func NotJSONMarshal() {
s := fmt.Sprintln("I am not considered by errchkjson")
_ = s
f := func() bool { return false }
_ = f()
}
// JSONMarshalStructWithoutExportedFields contains a struct without exported fields.
func JSONMarshalStructWithoutExportedFields() {
var err error
var withoutExportedFields struct {
privateField bool
ExportedButOmittedField bool `json:"-"`
}
_, err = json.Marshal(withoutExportedFields) // want "Error argument passed to `encoding/json.Marshal` does not contain any exported field"
_ = err
}
// JSONMarshalStructWithoutExportedFields contains a struct without exported fields.
func JSONMarshalStructWithNestedStructWithoutExportedFields() {
var err error
var withNestedStructWithoutExportedFields struct {
ExportedStruct struct {
privatField bool
}
}
_, err = json.Marshal(withNestedStructWithoutExportedFields)
_ = err
}