diff --git a/go.mod b/go.mod index b2a3c45e11cc..1e7e82b195f4 100644 --- a/go.mod +++ b/go.mod @@ -100,7 +100,7 @@ require ( github.com/shirou/gopsutil/v4 v4.25.4 github.com/sirupsen/logrus v1.9.3 github.com/sivchari/containedctx v1.0.3 - github.com/sonatard/noctx v0.1.0 + github.com/sonatard/noctx v0.3.3 github.com/sourcegraph/go-diff v0.7.0 github.com/spf13/cobra v1.9.1 github.com/spf13/pflag v1.0.6 diff --git a/go.sum b/go.sum index 182df4b27f2b..9875fd835bef 100644 --- a/go.sum +++ b/go.sum @@ -530,8 +530,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sivchari/containedctx v1.0.3 h1:x+etemjbsh2fB5ewm5FeLNi5bUjK0V8n0RB+Wwfd0XE= github.com/sivchari/containedctx v1.0.3/go.mod h1:c1RDvCbnJLtH4lLcYD/GqwiBSSf4F5Qk0xld2rBqzJ4= -github.com/sonatard/noctx v0.1.0 h1:JjqOc2WN16ISWAjAk8M5ej0RfExEXtkEyExl2hLW+OM= -github.com/sonatard/noctx v0.1.0/go.mod h1:0RvBxqY8D4j9cTTTWE8ylt2vqj2EPI8fHmrxHdsaZ2c= +github.com/sonatard/noctx v0.3.3 h1:9+wFUxZfjiCdNadFaGH55sa7Y1r6yKZiAsUVmCP+tjw= +github.com/sonatard/noctx v0.3.3/go.mod h1:64XdbzFb18XL4LporKXp8poqZtPKbCrqQ402CV+kJas= github.com/sourcegraph/go-diff v0.7.0 h1:9uLlrd5T46OXs5qpp8L/MTltk0zikUGi0sNNyCpA8G0= github.com/sourcegraph/go-diff v0.7.0/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs= github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA= diff --git a/pkg/golinters/noctx/noctx.go b/pkg/golinters/noctx/noctx.go index b0c5f9b50040..18391d4b5581 100644 --- a/pkg/golinters/noctx/noctx.go +++ b/pkg/golinters/noctx/noctx.go @@ -12,7 +12,7 @@ func New() *goanalysis.Linter { return goanalysis.NewLinter( a.Name, - "Finds sending http request without context.Context", + "Detects function and method with missing usage of context.Context", []*analysis.Analyzer{a}, nil, ).WithLoadMode(goanalysis.LoadModeTypesInfo) diff --git a/pkg/golinters/noctx/testdata/noctx.go b/pkg/golinters/noctx/testdata/noctx.go index 8b077a566206..8a34443e2029 100644 --- a/pkg/golinters/noctx/testdata/noctx.go +++ b/pkg/golinters/noctx/testdata/noctx.go @@ -3,73 +3,58 @@ package testdata import ( "context" + "database/sql" "net/http" ) var newRequestPkg = http.NewRequest -func Noctx() { - const url = "http://example.com" +func _() { + const url = "https://example.com" + cli := &http.Client{} ctx := context.Background() - http.Get(url) // want `net/http\.Get must not be called` - _ = http.Get // OK - f := http.Get // OK - f(url) // want `net/http\.Get must not be called` - - http.Head(url) // want `net/http\.Head must not be called` - http.Post(url, "", nil) // want `net/http\.Post must not be called` - http.PostForm(url, nil) // want `net/http\.PostForm must not be called` - - cli.Get(url) // want `\(\*net/http\.Client\)\.Get must not be called` - _ = cli.Get // OK - m := cli.Get // OK - m(url) // want `\(\*net/http\.Client\)\.Get must not be called` - - cli.Head(url) // want `\(\*net/http\.Client\)\.Head must not be called` - cli.Post(url, "", nil) // want `\(\*net/http\.Client\)\.Post must not be called` - cli.PostForm(url, nil) // want `\(\*net/http\.Client\)\.PostForm must not be called` - req, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` cli.Do(req) req2, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) // OK cli.Do(req2) - req3, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req3, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req3 = req3.WithContext(ctx) cli.Do(req3) f2 := func(req *http.Request, ctx context.Context) *http.Request { return req } - req4, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req4, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req4 = f2(req4, ctx) - req41, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req41, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req41 = req41.WithContext(ctx) req41 = f2(req41, ctx) newRequest := http.NewRequest - req5, _ := newRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req5, _ := newRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` cli.Do(req5) - req51, _ := newRequest(http.MethodPost, url, nil) // OK + req51, _ := newRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req51 = req51.WithContext(ctx) cli.Do(req51) - req52, _ := newRequestPkg(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req52, _ := newRequestPkg(http.MethodPost, url, nil) // TODO: false negative `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` cli.Do(req52) type MyRequest = http.Request f3 := func(req *MyRequest, ctx context.Context) *MyRequest { return req } - req6, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req6, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req6 = f3(req6, ctx) - req61, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req61, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req61 = req61.WithContext(ctx) req61 = f3(req61, ctx) @@ -77,22 +62,22 @@ func Noctx() { f4 := func(req *MyRequest2, ctx context.Context) *MyRequest2 { return req } - req7, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req7, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req71 := MyRequest2(*req7) f4(&req71, ctx) - req72, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req72, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req72 = req72.WithContext(ctx) req73 := MyRequest2(*req7) f4(&req73, ctx) req8, _ := func() (*http.Request, error) { - return http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + return http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` }() cli.Do(req8) req82, _ := func() (*http.Request, error) { - req82, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req82, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req82 = req82.WithContext(ctx) return req82, nil }() @@ -101,34 +86,99 @@ func Noctx() { f5 := func(req, req2 *http.Request, ctx context.Context) (*http.Request, *http.Request) { return req, req2 } - req9, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req9, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req9, _ = f5(req9, req9, ctx) - req91, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req91, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req91 = req91.WithContext(ctx) req9, _ = f5(req91, req91, ctx) - req10, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req11, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req10, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req11, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req10, req11 = f5(req10, req11, ctx) - req101, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req111, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req101, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req111, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req111 = req111.WithContext(ctx) req101, req111 = f5(req101, req111, ctx) func() (*http.Request, *http.Request) { - req12, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req13, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req12, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req13, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` return req12, req13 }() func() (*http.Request, *http.Request) { - req14, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req15, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req14, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req15, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req15 = req15.WithContext(ctx) return req14, req15 }() + + req121, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req121.AddCookie(&http.Cookie{Name: "k", Value: "v"}) + req121 = req121.WithContext(context.WithValue(req121.Context(), struct{}{}, 0)) + cli.Do(req121) +} + +func _() { + const url = "http://example.com" + cli := &http.Client{} + + http.Get(url) // want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + _ = http.Get // OK + f := http.Get // OK + f(url) // want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + + http.Head(url) // want `net/http\.Head must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + http.Post(url, "", nil) // want `net/http\.Post must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + http.PostForm(url, nil) // want `net/http\.PostForm must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + + cli.Get(url) // want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + _ = cli.Get // OK + m := cli.Get // OK + m(url) // want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + + cli.Head(url) // want `\(\*net/http\.Client\)\.Head must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + cli.Post(url, "", nil) // want `\(\*net/http\.Client\)\.Post must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + cli.PostForm(url, nil) // want `\(\*net/http\.Client\)\.PostForm must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` } +func _() { + ctx := context.Background() + + db, _ := sql.Open("noctx", "noctx://") + + db.Exec("select * from testdata") // want `\(\*database/sql\.DB\)\.Exec must not be called. use \(\*database/sql\.DB\)\.ExecContext` + db.ExecContext(ctx, "select * from testdata") + + db.Ping() // want `\(\*database/sql\.DB\)\.Ping must not be called. use \(\*database/sql\.DB\)\.PingContext` + db.PingContext(ctx) + + db.Prepare("select * from testdata") // want `\(\*database/sql\.DB\)\.Prepare must not be called. use \(\*database/sql\.DB\)\.PrepareContext` + db.PrepareContext(ctx, "select * from testdata") + + db.Query("select * from testdata") // want `\(\*database/sql\.DB\)\.Query must not be called. use \(\*database/sql\.DB\)\.QueryContext` + db.QueryContext(ctx, "select * from testdata") + + db.QueryRow("select * from testdata") // want `\(\*database/sql\.DB\)\.QueryRow must not be called. use \(\*database/sql\.DB\)\.QueryRowContext` + db.QueryRowContext(ctx, "select * from testdata") + + // transactions + + tx, _ := db.Begin() + tx.Exec("select * from testdata") // want `\(\*database/sql\.Tx\)\.Exec must not be called. use \(\*database/sql\.Tx\)\.ExecContext` + tx.ExecContext(ctx, "select * from testdata") + + tx.Prepare("select * from testdata") // want `\(\*database/sql\.Tx\)\.Prepare must not be called. use \(\*database/sql\.Tx\)\.PrepareContext` + tx.PrepareContext(ctx, "select * from testdata") + + tx.Query("select * from testdata") // want `\(\*database/sql\.Tx\)\.Query must not be called. use \(\*database/sql\.Tx\)\.QueryContext` + tx.QueryContext(ctx, "select * from testdata") + + tx.QueryRow("select * from testdata") // want `\(\*database/sql\.Tx\)\.QueryRow must not be called. use \(\*database/sql\.Tx\)\.QueryRowContext` + tx.QueryRowContext(ctx, "select * from testdata") + + _ = tx.Commit() +} diff --git a/pkg/golinters/noctx/testdata/noctx_cgo.go b/pkg/golinters/noctx/testdata/noctx_cgo.go index 64f5ea6c0d49..63b15477d8c3 100644 --- a/pkg/golinters/noctx/testdata/noctx_cgo.go +++ b/pkg/golinters/noctx/testdata/noctx_cgo.go @@ -13,6 +13,7 @@ import "C" import ( "context" + "database/sql" "net/http" "unsafe" ) @@ -26,67 +27,51 @@ func _() { var newRequestPkg = http.NewRequest func _() { - const url = "http://example.com" + const url = "https://example.com" + cli := &http.Client{} ctx := context.Background() - http.Get(url) // want `net/http\.Get must not be called` - _ = http.Get // OK - f := http.Get // OK - f(url) // want `net/http\.Get must not be called` - - http.Head(url) // want `net/http\.Head must not be called` - http.Post(url, "", nil) // want `net/http\.Post must not be called` - http.PostForm(url, nil) // want `net/http\.PostForm must not be called` - cli.Get(url) // want `\(\*net/http\.Client\)\.Get must not be called` - _ = cli.Get // OK - m := cli.Get // OK - m(url) // want `\(\*net/http\.Client\)\.Get must not be called` - - cli.Head(url) // want `\(\*net/http\.Client\)\.Head must not be called` - cli.Post(url, "", nil) // want `\(\*net/http\.Client\)\.Post must not be called` - cli.PostForm(url, nil) // want `\(\*net/http\.Client\)\.PostForm must not be called` - - req, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` cli.Do(req) req2, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) // OK cli.Do(req2) - req3, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req3, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req3 = req3.WithContext(ctx) cli.Do(req3) f2 := func(req *http.Request, ctx context.Context) *http.Request { return req } - req4, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req4, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req4 = f2(req4, ctx) - req41, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req41, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req41 = req41.WithContext(ctx) req41 = f2(req41, ctx) newRequest := http.NewRequest - req5, _ := newRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req5, _ := newRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` cli.Do(req5) - req51, _ := newRequest(http.MethodPost, url, nil) // OK + req51, _ := newRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req51 = req51.WithContext(ctx) cli.Do(req51) - req52, _ := newRequestPkg(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req52, _ := newRequestPkg(http.MethodPost, url, nil) // TODO: false negative `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` cli.Do(req52) type MyRequest = http.Request f3 := func(req *MyRequest, ctx context.Context) *MyRequest { return req } - req6, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req6, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req6 = f3(req6, ctx) - req61, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req61, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req61 = req61.WithContext(ctx) req61 = f3(req61, ctx) @@ -94,22 +79,22 @@ func _() { f4 := func(req *MyRequest2, ctx context.Context) *MyRequest2 { return req } - req7, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req7, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req71 := MyRequest2(*req7) f4(&req71, ctx) - req72, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req72, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req72 = req72.WithContext(ctx) req73 := MyRequest2(*req7) f4(&req73, ctx) req8, _ := func() (*http.Request, error) { - return http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + return http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` }() cli.Do(req8) req82, _ := func() (*http.Request, error) { - req82, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req82, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req82 = req82.WithContext(ctx) return req82, nil }() @@ -118,33 +103,99 @@ func _() { f5 := func(req, req2 *http.Request, ctx context.Context) (*http.Request, *http.Request) { return req, req2 } - req9, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req9, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req9, _ = f5(req9, req9, ctx) - req91, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req91, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req91 = req91.WithContext(ctx) req9, _ = f5(req91, req91, ctx) - req10, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req11, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req10, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req11, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req10, req11 = f5(req10, req11, ctx) - req101, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req111, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req101, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req111, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req111 = req111.WithContext(ctx) req101, req111 = f5(req101, req111, ctx) func() (*http.Request, *http.Request) { - req12, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req13, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` + req12, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req13, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` return req12, req13 }() func() (*http.Request, *http.Request) { - req14, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext` - req15, _ := http.NewRequest(http.MethodPost, url, nil) // OK + req14, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req15, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` req15 = req15.WithContext(ctx) return req14, req15 }() + + req121, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext` + req121.AddCookie(&http.Cookie{Name: "k", Value: "v"}) + req121 = req121.WithContext(context.WithValue(req121.Context(), struct{}{}, 0)) + cli.Do(req121) +} + +func _() { + const url = "http://example.com" + cli := &http.Client{} + + http.Get(url) // want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + _ = http.Get // OK + f := http.Get // OK + f(url) // want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + + http.Head(url) // want `net/http\.Head must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + http.Post(url, "", nil) // want `net/http\.Post must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + http.PostForm(url, nil) // want `net/http\.PostForm must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)` + + cli.Get(url) // want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + _ = cli.Get // OK + m := cli.Get // OK + m(url) // want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + + cli.Head(url) // want `\(\*net/http\.Client\)\.Head must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + cli.Post(url, "", nil) // want `\(\*net/http\.Client\)\.Post must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` + cli.PostForm(url, nil) // want `\(\*net/http\.Client\)\.PostForm must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)` +} + +func _() { + ctx := context.Background() + + db, _ := sql.Open("noctx", "noctx://") + + db.Exec("select * from testdata") // want `\(\*database/sql\.DB\)\.Exec must not be called. use \(\*database/sql\.DB\)\.ExecContext` + db.ExecContext(ctx, "select * from testdata") + + db.Ping() // want `\(\*database/sql\.DB\)\.Ping must not be called. use \(\*database/sql\.DB\)\.PingContext` + db.PingContext(ctx) + + db.Prepare("select * from testdata") // want `\(\*database/sql\.DB\)\.Prepare must not be called. use \(\*database/sql\.DB\)\.PrepareContext` + db.PrepareContext(ctx, "select * from testdata") + + db.Query("select * from testdata") // want `\(\*database/sql\.DB\)\.Query must not be called. use \(\*database/sql\.DB\)\.QueryContext` + db.QueryContext(ctx, "select * from testdata") + + db.QueryRow("select * from testdata") // want `\(\*database/sql\.DB\)\.QueryRow must not be called. use \(\*database/sql\.DB\)\.QueryRowContext` + db.QueryRowContext(ctx, "select * from testdata") + + // transactions + + tx, _ := db.Begin() + tx.Exec("select * from testdata") // want `\(\*database/sql\.Tx\)\.Exec must not be called. use \(\*database/sql\.Tx\)\.ExecContext` + tx.ExecContext(ctx, "select * from testdata") + + tx.Prepare("select * from testdata") // want `\(\*database/sql\.Tx\)\.Prepare must not be called. use \(\*database/sql\.Tx\)\.PrepareContext` + tx.PrepareContext(ctx, "select * from testdata") + + tx.Query("select * from testdata") // want `\(\*database/sql\.Tx\)\.Query must not be called. use \(\*database/sql\.Tx\)\.QueryContext` + tx.QueryContext(ctx, "select * from testdata") + + tx.QueryRow("select * from testdata") // want `\(\*database/sql\.Tx\)\.QueryRow must not be called. use \(\*database/sql\.Tx\)\.QueryRowContext` + tx.QueryRowContext(ctx, "select * from testdata") + + _ = tx.Commit() }