File tree 5 files changed +90
-2
lines changed
5 files changed +90
-2
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,7 @@ require (
77
77
github.com/spf13/viper v1.9.0
78
78
github.com/ssgreg/nlreturn/v2 v2.2.1
79
79
github.com/stretchr/testify v1.7.0
80
+ github.com/sylvia7788/contextcheck v1.0.4
80
81
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
81
82
github.com/tetafro/godot v1.4.11
82
83
github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -529,6 +529,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
529
529
WithPresets (linter .PresetStyle ).
530
530
WithLoadForGoAnalysis ().
531
531
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" ),
532
537
533
538
linter .NewConfig (golinters .NewCheckBannedFunc ()).
534
539
WithPresets (linter .PresetStyle ),
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments