Skip to content

Commit

Permalink
modify prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia7788 committed Sep 23, 2021
1 parent 43c3b76 commit c099f33
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 3 additions & 5 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 c099f33

Please sign in to comment.