From 807cc6ad2809cbede5c3c0a26617c5f5b8ecbf29 Mon Sep 17 00:00:00 2001 From: Lucas Bremgartner Date: Wed, 12 Jan 2022 20:19:11 +0100 Subject: [PATCH] Fix false positives where errors are returned Fixes: #5 --- errchkjson.go | 6 ++++++ testdata/src/nosafe/a.go | 9 +++++++++ testdata/src/standard/a.go | 9 +++++++++ 3 files changed, 24 insertions(+) diff --git a/errchkjson.go b/errchkjson.go index 6c6dd77..4d6d45e 100644 --- a/errchkjson.go +++ b/errchkjson.go @@ -43,6 +43,12 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) { return true } + // if the error is returned, it is the caller's responsibility to check + // the return value. + if _, ok := n.(*ast.ReturnStmt); ok { + return false + } + ce, ok := n.(*ast.CallExpr) if ok { fn, _ := typeutil.Callee(pass.TypesInfo, ce).(*types.Func) diff --git a/testdata/src/nosafe/a.go b/testdata/src/nosafe/a.go index dd07e1b..e51d811 100644 --- a/testdata/src/nosafe/a.go +++ b/testdata/src/nosafe/a.go @@ -612,3 +612,12 @@ func NotJSONMarshal() { f := func() bool { return false } _ = f() } + +// Issue 5 +type T struct { + s string +} + +func (t T) MarshalJSON() ([]byte, error) { + return json.Marshal(t.s) // not an error because it is the caller's responsibility to check the error +} diff --git a/testdata/src/standard/a.go b/testdata/src/standard/a.go index 9fae0fd..a9e1a7b 100644 --- a/testdata/src/standard/a.go +++ b/testdata/src/standard/a.go @@ -612,3 +612,12 @@ func NotJSONMarshal() { f := func() bool { return false } _ = f() } + +// Issue 5 +type T struct { + f64 float64 +} + +func (t T) MarshalJSON() ([]byte, error) { + return json.Marshal(t.f64) // not an error because it is the caller's responsibility to check the error +}