From 47749f45848b697f7c991fa516a056da8ea4001c Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 9 Aug 2024 18:40:16 -0700 Subject: [PATCH] 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 --- errorlint/allowed.go | 1 + errorlint/testdata/src/allowed/allowed.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/errorlint/allowed.go b/errorlint/allowed.go index cf48170..04eeddb 100644 --- a/errorlint/allowed.go +++ b/errorlint/allowed.go @@ -84,6 +84,7 @@ func setDefaultAllowedErrors() { {Err: "context.Canceled", Fun: "(context.Context).Err"}, // pkg/encoding/json {Err: "io.EOF", Fun: "(*encoding/json.Decoder).Decode"}, + {Err: "io.EOF", Fun: "(*encoding/json.Decoder).Token"}, // pkg/encoding/csv {Err: "io.EOF", Fun: "(*encoding/csv.Reader).Read"}, // pkg/mime/multipart diff --git a/errorlint/testdata/src/allowed/allowed.go b/errorlint/testdata/src/allowed/allowed.go index e3ff1ce..acac223 100644 --- a/errorlint/testdata/src/allowed/allowed.go +++ b/errorlint/testdata/src/allowed/allowed.go @@ -251,11 +251,16 @@ func ContextErr(ctx context.Context) error { return nil } -func JSONReader(r io.Reader) { - err := json.NewDecoder(r).Decode(nil) +func JSONReader(dec *json.Decoder) (err error) { + _, err = dec.Token() if err == io.EOF { - fmt.Println(err) + return + } + err = dec.Decode(nil) + if err == io.EOF { + return } + return } func CSVReader(r io.Reader) {