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