Skip to content

Commit

Permalink
modify prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia7788 committed Sep 4, 2021
1 parent 5a99232 commit 20c10da
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@

# contextcheck

`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.
`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.

For example:

```go
func call1(ctx context.Context) {
...
call2(context.Background()) // should use ctx

call3() // should pass the ctx
ctx = getNewCtx(ctx)
call2(ctx) // OK

call2(context.Background()) // Non-inherited new context, use function like `context.WithXXX` instead

call3() // Function `call3` should pass the context parameter
...
}

Expand All @@ -24,6 +28,11 @@ func call3() {
ctx := context.TODO()
call2(ctx)
}

func getNewCtx(ctx context.Context) (newCtx context.Context) {
...
return
}
```

## Installation
Expand All @@ -48,5 +57,5 @@ Invoke `contextcheck` with your package name
```bash
$ contextcheck ./...
$ # or
$ contextcheck github.com/you/yourproject/...
$ go vet -vettool=`which contextcheck` ./...
```
8 changes: 4 additions & 4 deletions contextcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func NewAnalyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "contextcheck",
Doc: "checkInstr for using context.Background() and context.TODO() directly",
Doc: "check the function whether use a non-inherited context",
Run: NewRun(),
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
Expand Down Expand Up @@ -247,15 +247,15 @@ func (r *runner) collectCtxRef(f *ssa.Function) (refMap map[ssa.Instruction]bool

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

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

if tp&CtxIn != 0 {
if !refMap[instr] {
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)")
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
}
}

Expand Down
17 changes: 14 additions & 3 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func f1(ctx context.Context) {
f2(ctx)
}(ctx)

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\)`
f2(context.Background()) // want "Non-inherited new context, use function like `context.WithXXX` instead"

thunk := MyInt.F
thunk(0)
Expand All @@ -63,18 +63,29 @@ func f3() {
func f4(ctx context.Context) {
f2(ctx)
ctx = context.Background()
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\)`
f2(ctx) // want "Non-inherited new context, use function like `context.WithXXX` instead"
}

func f5(ctx context.Context) {
func() {
f2(ctx)
}()

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

func f6() {
f3() // want "Function `f3` should pass the context parameter"
}

func f7(ctx context.Context) {
ctx, cancel := getNewCtx(ctx)
defer cancel()

f2(ctx) // OK
}

func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) {
return context.WithCancel(ctx)
}

0 comments on commit 20c10da

Please sign in to comment.