Skip to content

Commit 7fc2fe8

Browse files
authoredOct 4, 2021
feat: add contextcheck linter (#2216)
1 parent 2fb6563 commit 7fc2fe8

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed
 

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ require (
7777
github.com/spf13/viper v1.9.0
7878
github.com/ssgreg/nlreturn/v2 v2.2.1
7979
github.com/stretchr/testify v1.7.0
80+
github.com/sylvia7788/contextcheck v1.0.4
8081
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
8182
github.com/tetafro/godot v1.4.11
8283
github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94

‎go.sum

+16-2
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 the function whether use a non-inherited context",
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
@@ -529,6 +529,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
529529
WithPresets(linter.PresetStyle).
530530
WithLoadForGoAnalysis().
531531
WithURL("https://github.com/sivchari/tenv"),
532+
linter.NewConfig(golinters.NewContextCheck()).
533+
WithPresets(linter.PresetBugs).
534+
WithLoadForGoAnalysis().
535+
WithURL("https://github.com/sylvia7788/contextcheck").
536+
WithSince("v1.43.0"),
532537

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

‎test/testdata/contextcheck.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 "Non-inherited new context, use function like `context.WithXXX` instead"
25+
}
26+
27+
func contextcheckCase3(ctx context.Context) {
28+
func() {
29+
funcWithCtx(ctx)
30+
}()
31+
32+
ctx = context.Background() // ERROR "Non-inherited new context, use function like `context.WithXXX` instead"
33+
funcWithCtx(ctx)
34+
}
35+
36+
func contextcheckCase4(ctx context.Context) {
37+
ctx, cancel := getNewCtx(ctx)
38+
defer cancel()
39+
funcWithCtx(ctx)
40+
}
41+
42+
func funcWithCtx(ctx context.Context) {}
43+
44+
func funcWithoutCtx() {
45+
funcWithCtx(context.TODO())
46+
}
47+
48+
func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) {
49+
return context.WithCancel(ctx)
50+
}

0 commit comments

Comments
 (0)