Skip to content

jsonmarshalignoredeerror FN: bare-call json.Unmarshal(data, &v) (ExprStmt) discards the error but is never inspected — only As [Content truncated due to length] #39982

Closed as not planned

Description

@github-actions

Summary

jsonmarshalignoredeerror only inspects assignment statements, so it catches the explicit-blank discard forms (_ = json.Unmarshal(...), v, _ := json.Marshal(...)) but completely misses the bare expression-statement form json.Unmarshal(data, &v) / json.Marshal(x), where every return — including the error — is discarded implicitly. The implicit-discard form is the most common mistake the linter exists to prevent, yet it silently passes a CI-enforced check (.github/workflows/cgo.yml:1123), giving false assurance that all json.Marshal/Unmarshal error-discards are caught.

Location

pkg/linters/jsonmarshalignoredeerror/jsonmarshalignoredeerror.go:31

nodeFilter := []ast.Node{(*ast.AssignStmt)(nil)}   // <-- ExprStmt never visited

A bare call such as json.Unmarshal(data, &v) is an *ast.ExprStmt, not an *ast.AssignStmt, so the Preorder walk never sees it.

Evidence the pattern is used in-repo

Bare json.Unmarshal(...) calls already appear in the codebase:

  • pkg/parser/json_path_locator_test.go:141
  • pkg/cli/logs_awinfo_backward_compat_test.go:173 and :182
  • pkg/workflow/dependabot_test.go:607

These are test files, and CI runs the linter with -test=false (cgo.yml:1123), so they don't trip enforcement today. The gap is that any production pkg/ bare-call would escape silently — a latent false negative in an enforced linter.

What is already correct

Package identity is resolved properly through TypesInfo in isJSONFunc (:75-93, *types.PkgName.Imported().Path() == "encoding/json"), so this is purely a node-coverage gap, not a matching gap.

Recommended fix

Add (*ast.ExprStmt)(nil) to the node filter and handle the bare-call case: when an ExprStmt's X is a *ast.CallExpr to json.Marshal/json.Unmarshal, report it (a bare call discards all returns, including the error). Keep the existing AssignStmt branch for the explicit-blank forms.

nodeFilter := []ast.Node{(*ast.AssignStmt)(nil), (*ast.ExprStmt)(nil)}
// ...
case *ast.ExprStmt:
    if call, ok := stmt.X.(*ast.CallExpr); ok {
        if isJSONFunc(pass, call, "Marshal") || isJSONFunc(pass, call, "Unmarshal") { report }
    }

Validation checklist

  • Bare json.Unmarshal(data, &v) in a non-test file → diagnostic.
  • Bare json.Marshal(x) → diagnostic.
  • Existing b, _ := json.Marshal(x) and _ = json.Unmarshal(...) still reported (no regression).
  • go test ./pkg/linters/jsonmarshalignoredeerror/... green.

Effort

Small — single-file change.

Generated by 🤖 Sergo - Serena Go Expert · ◷

  • expires on Jun 24, 2026, 9:25 PM UTC-08:00

Activity

  1. added
    cookie
    on Jun 18, 2026
  2. github-actions commented on Jun 25, 2026

    @github-actions
    ContributorAuthor

    This issue was automatically closed because it expired on 2026-06-25T05:25:30.310Z.

    Closed by Workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

cookiesergo

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions