Skip to content

Commit b5e48c9

Browse files
authored
Merge pull request #6 from breml/issue_5
Fix false positives where errors are returned
2 parents 817869a + 807cc6a commit b5e48c9

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

errchkjson.go

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) {
4343
return true
4444
}
4545

46+
// if the error is returned, it is the caller's responsibility to check
47+
// the return value.
48+
if _, ok := n.(*ast.ReturnStmt); ok {
49+
return false
50+
}
51+
4652
ce, ok := n.(*ast.CallExpr)
4753
if ok {
4854
fn, _ := typeutil.Callee(pass.TypesInfo, ce).(*types.Func)

testdata/src/nosafe/a.go

+9
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,12 @@ func NotJSONMarshal() {
612612
f := func() bool { return false }
613613
_ = f()
614614
}
615+
616+
// Issue 5
617+
type T struct {
618+
s string
619+
}
620+
621+
func (t T) MarshalJSON() ([]byte, error) {
622+
return json.Marshal(t.s) // not an error because it is the caller's responsibility to check the error
623+
}

testdata/src/standard/a.go

+9
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,12 @@ func NotJSONMarshal() {
612612
f := func() bool { return false }
613613
_ = f()
614614
}
615+
616+
// Issue 5
617+
type T struct {
618+
f64 float64
619+
}
620+
621+
func (t T) MarshalJSON() ([]byte, error) {
622+
return json.Marshal(t.f64) // not an error because it is the caller's responsibility to check the error
623+
}

0 commit comments

Comments
 (0)