File tree Expand file tree Collapse file tree 5 files changed +90
-2
lines changed
Expand file tree Collapse file tree 5 files changed +90
-2
lines changed Original file line number Diff line number Diff 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
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 {
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 linter .NewConfig (golinters .NewCheckBannedFunc ()).
534539 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