Skip to content

SA5008: false positive on json:"-,omitempty" struct tag #1699

Open

Description

@patjakdev

I have a struct which correctly marshals to and unmarshals from JSON with the following struct tag:

type nodeJSON struct {
    // omitted...
    Subtract           *binaryJSON `json:"-,omitempty"`
    // omitted...
}

However, since upgrading to staticcheck v0.7.0, I've been getting this false positive:

internal/json/json.go:112:33: should encoding/json ignore this field or name it "-"? Either use `json:"-"` to ignore the field or use `json:"'-',omitempty"` to specify "-" as the name (SA5008)

As near as I can tell, there's nothing wrong with the current construction. The suggested fix is new syntax that is only available if using the encoding/json/v2 package, which I am not.

Particulars

  • staticcheck -version: staticcheck 2026.1 (v0.7.0)
  • staticcheck -debug.version:
staticcheck 2026.1 (v0.7.0)

Compiled with Go version: go1.25.6
Main module:
        honnef.co/go/tools@v0.7.0 (sum: h1:w6WUp1VbkqPEgLz4rkBzH/CSU6HkoqNLp6GstyTx3lU=)
Dependencies:
        github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=)
        golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=)
        golang.org/x/mod@v0.31.0 (sum: h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=)
        golang.org/x/sync@v0.19.0 (sum: h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=)
        golang.org/x/tools@v0.40.1-0.20260108161641-ca281cf95054 (sum: h1:CHVDrNHx9ZoOrNN9kKWYIbT5Rj+WF2rlwPkhbQQ5V4U=)

Activity

  1. added
    needs-triage
    on Feb 25, 2026
  2. dominikh commented on Feb 25, 2026

    @dominikh
    Owner

    When I made the change I checked that encoding/json also supports quoting the - when it's used as a field name. Did I make a mistake there?

    As for the false positive, you're technically correct, but I've decided that the unquoted form is too prone to accidents, see also #1655. To make things more explicit, and to prepare for a potential migration to encoding/json/v2, we emit the diagnostic even for encoding/json.

  3. dominikh commented on Feb 25, 2026

    @dominikh
    Owner

    Oh this is very annoying. Whether encoding/json accepts that syntax depends on the value of GOEXPERIMENT...

    $ cat foo.go
    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"log"
    )
    
    type S struct {
    	Foo int `json:"'-'"`
    }
    
    func main() {
    	s := S{42}
    	j, err := json.Marshal(s)
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Println(string(j))
    }
    
    $ go run foo.go
    {"Foo":42}
    $ GOEXPERIMENT=jsonv2 go run foo.go
    {"-":42}
    

    To be clear, this is something I need to fix.

  4. added
    upstream
    and removed
    needs-triage
    on Feb 25, 2026
  5. patjakdev commented on Feb 25, 2026

    @patjakdev
    Author

    Thanks @dominikh, much appreciated. I was hopeful about using the '-' syntax as suggested by the linter myself but was disappointed to see that it only works in json/v2.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    false-positiveupstream

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions