Skip to content

Commit 20c10da

Browse files
committed
modify prompt
1 parent 5a99232 commit 20c10da

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33

44
# contextcheck
55

6-
`contextcheck` is a static analysis tool, it is used to check a function whether use context.Background() or context.TODO() directly instead of the input ctx when calling the sub-function, or even don't pass the ctx, which will result in a broken call link.
6+
`contextcheck` is a static analysis tool, it is used to check the function whether use a non-inherited context, which will result in a broken call link.
77

88
For example:
99

1010
```go
1111
func call1(ctx context.Context) {
1212
...
13-
call2(context.Background()) // should use ctx
1413

15-
call3() // should pass the ctx
14+
ctx = getNewCtx(ctx)
15+
call2(ctx) // OK
16+
17+
call2(context.Background()) // Non-inherited new context, use function like `context.WithXXX` instead
18+
19+
call3() // Function `call3` should pass the context parameter
1620
...
1721
}
1822

@@ -24,6 +28,11 @@ func call3() {
2428
ctx := context.TODO()
2529
call2(ctx)
2630
}
31+
32+
func getNewCtx(ctx context.Context) (newCtx context.Context) {
33+
...
34+
return
35+
}
2736
```
2837

2938
## Installation
@@ -48,5 +57,5 @@ Invoke `contextcheck` with your package name
4857
```bash
4958
$ contextcheck ./...
5059
$ # or
51-
$ contextcheck github.com/you/yourproject/...
60+
$ go vet -vettool=`which contextcheck` ./...
5261
```

contextcheck.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func NewAnalyzer() *analysis.Analyzer {
1818
return &analysis.Analyzer{
1919
Name: "contextcheck",
20-
Doc: "checkInstr for using context.Background() and context.TODO() directly",
20+
Doc: "check the function whether use a non-inherited context",
2121
Run: NewRun(),
2222
Requires: []*analysis.Analyzer{
2323
buildssa.Analyzer,
@@ -247,15 +247,15 @@ func (r *runner) collectCtxRef(f *ssa.Function) (refMap map[ssa.Instruction]bool
247247

248248
for instr := range storeInstrs {
249249
if !checkedRefMap[instr.Val] {
250-
r.pass.Reportf(instr.Pos(), "Invalid call to get new context, please replace it with another way, such as context.WithValue(ctx, key, val)")
250+
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
251251
ok = false
252252
}
253253
}
254254

255255
for instr := range phiInstrs {
256256
for _, v := range instr.Edges {
257257
if !checkedRefMap[v] {
258-
r.pass.Reportf(instr.Pos(), "Invalid call to get new context, please replace it with another way, such as context.WithValue(ctx, key, val)")
258+
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
259259
ok = false
260260
}
261261
}
@@ -299,7 +299,7 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function) {
299299

300300
if tp&CtxIn != 0 {
301301
if !refMap[instr] {
302-
r.pass.Reportf(instr.Pos(), "The context param may be context.TODO() or context.Background(), please replace it with another way, such as context.WithValue(ctx, key, val)")
302+
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
303303
}
304304
}
305305

testdata/src/a/a.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func f1(ctx context.Context) {
4545
f2(ctx)
4646
}(ctx)
4747

48-
f2(context.Background()) // want `The context param may be context\.TODO\(\) or context\.Background\(\), please replace it with another way, such as context\.WithValue\(ctx, key, val\)`
48+
f2(context.Background()) // want "Non-inherited new context, use function like `context.WithXXX` instead"
4949

5050
thunk := MyInt.F
5151
thunk(0)
@@ -63,18 +63,29 @@ func f3() {
6363
func f4(ctx context.Context) {
6464
f2(ctx)
6565
ctx = context.Background()
66-
f2(ctx) // want `The context param may be context\.TODO\(\) or context\.Background\(\), please replace it with another way, such as context\.WithValue\(ctx, key, val\)`
66+
f2(ctx) // want "Non-inherited new context, use function like `context.WithXXX` instead"
6767
}
6868

6969
func f5(ctx context.Context) {
7070
func() {
7171
f2(ctx)
7272
}()
7373

74-
ctx = context.Background() // want `Invalid call to get new context, please replace it with another way, such as context\.WithValue\(ctx, key, val\)`
74+
ctx = context.Background() // want "Non-inherited new context, use function like `context.WithXXX` instead"
7575
f2(ctx)
7676
}
7777

7878
func f6() {
7979
f3() // want "Function `f3` should pass the context parameter"
8080
}
81+
82+
func f7(ctx context.Context) {
83+
ctx, cancel := getNewCtx(ctx)
84+
defer cancel()
85+
86+
f2(ctx) // OK
87+
}
88+
89+
func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) {
90+
return context.WithCancel(ctx)
91+
}

0 commit comments

Comments
 (0)