Skip to content

Commit 30b02e6

Browse files
committed
feat: add contextcheck linter
1 parent 4ab17bd commit 30b02e6

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ require (
7474
github.com/spf13/viper v1.8.1
7575
github.com/ssgreg/nlreturn/v2 v2.1.0
7676
github.com/stretchr/testify v1.7.0
77+
github.com/sylvia7788/contextcheck v1.0.3 // indirect
7778
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
7879
github.com/tetafro/godot v1.4.9
7980
github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94

go.sum

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

pkg/golinters/contextcheck.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package golinters
2+
3+
import (
4+
"github.com/sylvia7788/contextcheck"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewContextCheck() *goanalysis.Linter {
11+
analyzer := contextcheck.NewAnalyzer()
12+
return goanalysis.NewLinter(
13+
"contextcheck",
14+
"check for using context.Background() and context.TODO() directly",
15+
[]*analysis.Analyzer{analyzer},
16+
nil,
17+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
18+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
506506
WithLoadForGoAnalysis().
507507
WithURL("https://github.com/Antonboom/errname").
508508
WithSince("v1.42.0"),
509+
linter.NewConfig(golinters.NewContextCheck()).
510+
WithPresets(linter.PresetBugs).
511+
WithLoadForGoAnalysis().
512+
WithURL("https://github.com/sylvia7788/contextcheck").
513+
WithSince("v1.0.0"),
509514

510515
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
511516
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/contextcheck.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//args: -Econtextcheck
2+
package testdata
3+
4+
import "context"
5+
6+
type MyString string
7+
8+
func contextcheckCase1(ctx context.Context) {
9+
funcWithoutCtx() // ERROR "Function `funcWithoutCtx` should pass the context parameter"
10+
}
11+
12+
func contextcheckCase2(ctx context.Context) {
13+
ctx = context.WithValue(ctx, MyString("aaa"), "aaaaaa")
14+
funcWithCtx(ctx)
15+
16+
defer func() {
17+
funcWithCtx(ctx)
18+
}()
19+
20+
func(ctx context.Context) {
21+
funcWithCtx(ctx)
22+
}(ctx)
23+
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\)`
25+
}
26+
27+
func contextcheckCase3(ctx context.Context) {
28+
func() {
29+
funcWithCtx(ctx)
30+
}()
31+
32+
ctx = context.Background() // ERROR `Invalid call to get new context, please replace it with another way, such as context.WithValue\(ctx, key, val\)`
33+
funcWithCtx(ctx)
34+
}
35+
36+
func funcWithCtx(ctx context.Context) {}
37+
38+
func funcWithoutCtx() {
39+
funcWithCtx(context.TODO())
40+
}

0 commit comments

Comments
 (0)