Skip to content

Commit 68f530a

Browse files
authored
add containedctx linter (#2382)
1 parent 1685402 commit 68f530a

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ require (
7373
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
7474
github.com/shirou/gopsutil/v3 v3.21.12
7575
github.com/sirupsen/logrus v1.8.1
76+
github.com/sivchari/containedctx v1.0.1
7677
github.com/sivchari/tenv v1.4.7
7778
github.com/sonatard/noctx v0.0.1
7879
github.com/sourcegraph/go-diff v0.6.1

go.sum

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

pkg/golinters/containedctx.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/sivchari/containedctx"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewContainedCtx() *goanalysis.Linter {
11+
a := containedctx.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
183183
WithPresets(linter.PresetPerformance, linter.PresetBugs).
184184
WithURL("https://github.com/timakin/bodyclose"),
185185

186+
linter.NewConfig(golinters.NewContainedCtx()).
187+
WithSince("1.44.0").
188+
WithPresets(linter.PresetStyle).
189+
WithURL("https://github.com/sivchari/containedctx"),
190+
186191
linter.NewConfig(golinters.NewContextCheck()).
187192
WithSince("v1.43.0").
188193
WithPresets(linter.PresetBugs).

test/testdata/containedctx.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// args: -Econtainedctx
2+
package testdata
3+
4+
import "context"
5+
6+
type ok struct {
7+
i int
8+
s string
9+
}
10+
11+
type ng struct {
12+
ctx context.Context // ERROR "found a struct that contains a context.Context field"
13+
}
14+
15+
type empty struct{}

0 commit comments

Comments
 (0)