Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives where errors are returned #6

Merged
merged 1 commit into from Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}