Skip to content

Commit 47749f4

Browse files
kolyshkinpolyfloyd
authored andcommitted
feat: add exception for encoding/json.decoder.Token
As noted in https://pkg.go.dev/encoding/json#Decoder.Token, it can return io.EOF. Add a test case. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent bbdc417 commit 47749f4

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

errorlint/allowed.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func setDefaultAllowedErrors() {
8484
{Err: "context.Canceled", Fun: "(context.Context).Err"},
8585
// pkg/encoding/json
8686
{Err: "io.EOF", Fun: "(*encoding/json.Decoder).Decode"},
87+
{Err: "io.EOF", Fun: "(*encoding/json.Decoder).Token"},
8788
// pkg/encoding/csv
8889
{Err: "io.EOF", Fun: "(*encoding/csv.Reader).Read"},
8990
// pkg/mime/multipart

errorlint/testdata/src/allowed/allowed.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,16 @@ func ContextErr(ctx context.Context) error {
251251
return nil
252252
}
253253

254-
func JSONReader(r io.Reader) {
255-
err := json.NewDecoder(r).Decode(nil)
254+
func JSONReader(dec *json.Decoder) (err error) {
255+
_, err = dec.Token()
256256
if err == io.EOF {
257-
fmt.Println(err)
257+
return
258+
}
259+
err = dec.Decode(nil)
260+
if err == io.EOF {
261+
return
258262
}
263+
return
259264
}
260265

261266
func CSVReader(r io.Reader) {

0 commit comments

Comments
 (0)