From 7c9b63365b73bc3c4a6ea614bce2f38270de2599 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 --- pkg/golinters/contextcheck.go | 2 +- test/testdata/contextcheck.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) 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) +}