8000 SA1026: false positive for nil pointer to struct containing unsupported type · Issue #1712 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

SA1026: false positive for nil pointer to struct containing unsupported type #1712

Open

Description

@arp242

The following code:

func main() {
    resp := &http.Response{
        StatusCode: 200,
        // ... etc
    }
8000


    j, err := json.Marshal(struct {
        *http.Response
        Body    []byte
        Request *http.Request
    }{Response: resp, Body: []byte("ABC")})

    if err != nil {
        panic(err)
    }

    fmt.Println(string(j))
}

Works as expected; the http.Request struct contains some fields that can't be marshaled (func, channel), but it overrides the Request field:

% go run test.go
{
   [..]
   "StatusCode": 200,
   [..]
}

But staticcheck errors:

% staticcheck test.go
test.go:15:25: trying to marshal unsupported type func() (io.ReadCloser, error), via x.Request.GetBody (SA1026)

staticcheck (devel, v0.7.0-0.dev.0.20260409232628-ba2c20843b32)

Activity

  1. dominikh commented on May 2, 2026

    @dominikh
    Owner

    Not embedding related, this is a simpler reproducer:

    func main() {
    	resp := &http.Response{
    		StatusCode: 200,
    		// ... etc
    	}
    
    	j, err := json.Marshal(resp)
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Println(string(j))
    }
    

    The problem is that our analysis is type-based, but you can marshal a *http.Request as long as it is nil, because the encoder won't recurse into it to find the field of unsupported type.

    I don't think we can avoid this false positive in the general case (with non-trivial data flow) without neutering the check a fair bit.

  2. changed the title [-]SA1026: false positive for fields in embedded struct that are overridden[/-] [+]SA1026: false positive for nil pointer to struct containing unsupported type[/+] on May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0