Skip to content

Commit

Permalink
modify prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia7788 committed Sep 8, 2021
1 parent 5674a58 commit 9a38ba5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/golinters/contextcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions test/testdata/contextcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ 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) {
func() {
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)
}

Expand All @@ -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)
}

0 comments on commit 9a38ba5

Please sign in to comment.