Skip to content

Commit

Permalink
Merge pull request #6 from breml/issue_5
Browse files Browse the repository at this point in the history
Fix false positives where errors are returned
  • Loading branch information
breml authored Jan 16, 2022
2 parents 817869a + 807cc6a commit b5e48c9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions errchkjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions testdata/src/nosafe/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions testdata/src/standard/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b5e48c9

Please sign in to comment.