From c099f337be99e94cc35aea2d7976665f4b5529be Mon Sep 17 00:00:00 2001 From: sylvia7788 <1227977886@qq.com> Date: Sat, 4 Sep 2021 16:45:21 +0800 Subject: [PATCH] modify prompt --- go.sum | 8 +++----- pkg/golinters/contextcheck.go | 2 +- test/testdata/contextcheck.go | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/go.sum b/go.sum index 1ca33a126b9c..403a2c32334b 100644 --- a/go.sum +++ b/go.sum @@ -717,14 +717,12 @@ github.com/sylvia7788/contextcheck v1.0.4 h1:MsiVqROAdr0efZc/fOCt0c235qm9XJqHtWw github.com/sylvia7788/contextcheck v1.0.4/go.mod h1:vuPKJMQ7MQ91ZTqfdyreNKwZjyUg6KO+IebVyQDedZQ= github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b h1:HxLVTlqcHhFAz3nWUcuvpH7WuOMv8LQoCWmruLfFH2U= github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM= -github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= -github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA= github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0= github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag= github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY= -github.com/tetafro/godot v1.4.10 h1:BqGslJVcVV7coRukPF104O3AUXVJIS606l2KB7vo6Bc= -github.com/tetafro/godot v1.4.10/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= +github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= +github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94 h1:ig99OeTyDwQWhPe2iw9lwfQVF1KB3Q4fpP3X7/2VBG8= github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk= github.com/tklauser/go-sysconf v0.3.9 h1:JeUVdAOWhhxVcU6Eqr/ATFHgXk/mmiItdKeJPev3vTo= @@ -1110,9 +1108,9 @@ golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= golang.org/x/tools v0.1.1-0.20210302220138-2ac05c832e1a/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= diff --git a/pkg/golinters/contextcheck.go b/pkg/golinters/contextcheck.go index 2153705ea6c2..eb12ed4ef39f 100644 --- a/pkg/golinters/contextcheck.go +++ b/pkg/golinters/contextcheck.go @@ -11,7 +11,7 @@ func NewContextCheck() *goanalysis.Linter { analyzer := contextcheck.NewAnalyzer() return goanalysis.NewLinter( "contextcheck", - "check for using context.Background() and context.TODO() directly", + "check the function whether use a non-inherited context", []*analysis.Analyzer{analyzer}, nil, ).WithLoadMode(goanalysis.LoadModeTypesInfo) diff --git a/test/testdata/contextcheck.go b/test/testdata/contextcheck.go index 2c2957fe95d2..093e9337d0f7 100644 --- a/test/testdata/contextcheck.go +++ b/test/testdata/contextcheck.go @@ -21,7 +21,7 @@ func contextcheckCase2(ctx context.Context) { funcWithCtx(ctx) }(ctx) - funcWithCtx(context.Background()) // ERROR `The context param may be context.TODO\(\) or context.Background\(\), please replace it with another way, such as context.WithValue\(ctx, key, val\)` + funcWithCtx(context.Background()) // ERROR "Non-inherited new context, use function like `context.WithXXX` instead" } func contextcheckCase3(ctx context.Context) { @@ -29,7 +29,13 @@ func contextcheckCase3(ctx context.Context) { funcWithCtx(ctx) }() - ctx = context.Background() // ERROR `Invalid call to get new context, please replace it with another way, such as context.WithValue\(ctx, key, val\)` + ctx = context.Background() // ERROR "Non-inherited new context, use function like `context.WithXXX` instead" + funcWithCtx(ctx) +} + +func contextcheckCase4(ctx context.Context) { + ctx, cancel := getNewCtx(ctx) + defer cancel() funcWithCtx(ctx) } @@ -38,3 +44,7 @@ func funcWithCtx(ctx context.Context) {} func funcWithoutCtx() { funcWithCtx(context.TODO()) } + +func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) { + return context.WithCancel(ctx) +}