You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
WithContext returns a shallow copy of r with its context changed to ctx.
I've accidentally used WithContext() thinking it would update the request in-place rather than return a copy. When I found my mistake I was hoping staticcheck would catch this issue, perhaps with SA4017, but it doesn't.
Minimal test case:
package foo
import (
"bytes"
"net/http"
)
func bad() {
url := "http://example.com"
payload := "this should be flagged by the context checker"
req, _ := http.NewRequest("POST", url, bytes.NewBufferString(payload))
req.WithContext(nil)
}
bobs-mac% staticcheck foo
/foo/something.go:12:18: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (SA1012)
I expected to see a SA4017 warning about an unused result from a pure function. Is staticcheck marking *net/http.Request.WithContext() as a pure function? Or am I misunderstanding the nature of the check.
The text was updated successfully, but these errors were encountered:
SA4017 works in two separate ways. It has a hard-coded list of pure stdlib functions, and it detects very simple pure functions that consist of a very restricted subset of the Go language.
The list is missing an entry for WithContext, and the automatic detection won't match that method.
According to https://golang.org/pkg/net/http/#Request.WithContext:
I've accidentally used WithContext() thinking it would update the request in-place rather than return a copy. When I found my mistake I was hoping staticcheck would catch this issue, perhaps with SA4017, but it doesn't.
Minimal test case:
I expected to see a SA4017 warning about an unused result from a pure function. Is staticcheck marking *net/http.Request.WithContext() as a pure function? Or am I misunderstanding the nature of the check.
The text was updated successfully, but these errors were encountered: