Skip to content

Commit 53bdd67

Browse files
committed
modify prompt
1 parent bd487e7 commit 53bdd67

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

go.sum

+9-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/contextcheck.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func NewContextCheck() *goanalysis.Linter {
1111
analyzer := contextcheck.NewAnalyzer()
1212
return goanalysis.NewLinter(
1313
"contextcheck",
14-
"check for using context.Background() and context.TODO() directly",
14+
"check the function whether use a non-inherited context",
1515
[]*analysis.Analyzer{analyzer},
1616
nil,
1717
).WithLoadMode(goanalysis.LoadModeTypesInfo)

test/testdata/contextcheck.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ func contextcheckCase2(ctx context.Context) {
2121
funcWithCtx(ctx)
2222
}(ctx)
2323

24-
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\)`
24+
funcWithCtx(context.Background()) // ERROR "Non-inherited new context, use function like `context.WithXXX` instead"
2525
}
2626

2727
func contextcheckCase3(ctx context.Context) {
2828
func() {
2929
funcWithCtx(ctx)
3030
}()
3131

32-
ctx = context.Background() // ERROR `Invalid call to get new context, please replace it with another way, such as context.WithValue\(ctx, key, val\)`
32+
ctx = context.Background() // ERROR "Non-inherited new context, use function like `context.WithXXX` instead"
33+
funcWithCtx(ctx)
34+
}
35+
36+
func contextcheckCase4(ctx context.Context) {
37+
ctx, cancel := getNewCtx(ctx)
38+
defer cancel()
3339
funcWithCtx(ctx)
3440
}
3541

@@ -38,3 +44,7 @@ func funcWithCtx(ctx context.Context) {}
3844
func funcWithoutCtx() {
3945
funcWithCtx(context.TODO())
4046
}
47+
48+
func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) {
49+
return context.WithCancel(ctx)
50+
}

0 commit comments

Comments
 (0)