sonatard / noctx Goto Github PK
View Code? Open in Web Editor NEWnoctx finds sending http request without context.Context.
License: MIT License
noctx finds sending http request without context.Context.
License: MIT License
type key struct{}
req, _ := http.NewRequest("POST", "", nil)
req = req.WithContext(context.WithValue(req.Context(), key{}, 0))
noctx raises this error:
should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
req, _ := http.NewRequest("POST", "", nil)
^
req, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
_, _ = func() (*http.Request, error) { return req, nil }() // OK
cli.Do(req)
=== RUN Test
--- FAIL: Test (0.63s)
analysistest.go:324: a/a.go:33: no diagnostic was reported matching "should rewrite http.NewRequestWithContext or add \\(\\*Request\\).WithContext"
FAIL
Process finished with exit code 1
I'm always interested how to improve my code, but this one is a bit irritating to me. Maybe I'm wrong in doing so, but it seems like detection is too strict here.
In my API client, I have a helper function to abstract creating the basic request. Usually I then add headers or other stuff afterwards, before executing the request.
func (a *Api) NewApiRequest(method string, path string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequestWithContext(a.Context, method, a.BaseURL+path, body)
if err != nil {
return
}
req.SetBasicAuth(a.Username, a.Password)
return
}
Now when using the function, noctx complains:
req, err := a.NewApiRequest("POST", "/upload", nil) // should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
if err != nil {
return fmt.Errorf("could not create request: %w", err)
}
// Adding more to the request
data, err := a.RequestApi(req, http.StatusCreated)
if err != nil {
return fmt.Errorf("could not create upload: %w", err)
}
What do you think about it? For now I just ignored noctx here.
// Sending an HTTP request and accepting context
func SendWithContext(ctx context.Context, body io.Reader) error {
// Change NewRequest to NewRequestWithContext and pass context it
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://example.com", body)
if err != nil {
return nil
}
You are returning nil on error
It`s small mistake but someone can copy this code
Hi ๐
Please, bump golang.org/x/tools
to v0.7.0 (as minimum) and make new tag in your repo.
It's important to be consistent with new Go 1.20 golangci-lint.
Related to golangci/golangci-lint#3086
Thanks!
P.S. Optionally I recommend
As described in the docs, http.NewRequest
uses the context.Background
context which is supposed to be used in tests according to the docs. Therefore, it is cleaner to use http.NewContext
in tests where we make HTTP requests against test servers.
Therefore, I think the linter should allow the usage of http.NewRequest
, http.Get
, http.Header
and all similar functions in tests because it's the recommended way and provides much simpler code than having to explicitly use http.NewRequestWithContext
with context.Background
each time.
Currently, SSA is not working with generics.
So your linter produces a panic when it is used with generics.
There is an issue open about that in the Go repository: golang/go#48525
Inside golangci-lint, we disabled your linters: golangci/golangci-lint#2649
You have 2 solutions:
Related to golang/go#50558
Thank you for the wonderful linter.
This linter seems to be mainly net/http, but is it out of scope to issue a warning to methods that use database/sql without context.Context?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.