Skip to content

Commit

Permalink
Fix false positives where errors are returned
Browse files Browse the repository at this point in the history
Fixes: #5
  • Loading branch information
breml committed Jan 12, 2022
1 parent 817869a commit 807cc6a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions errchkjson.go
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
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
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 807cc6a

Please sign in to comment.