Skip to content

Commit 737a771

Browse files
committed
add containedctx linter
1 parent a5f0a4d commit 737a771

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-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.10
7575
github.com/sirupsen/logrus v1.8.1
76+
github.com/sivchari/containedctx v1.0.0
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

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
analyzers := []*analysis.Analyzer{
14+
a,
15+
}
16+
17+
return goanalysis.NewLinter(
18+
a.Name,
19+
a.Doc,
20+
analyzers,
21+
nil,
22+
).WithLoadMode(goanalysis.LoadModeSyntax)
23+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
555555
WithPresets(linter.PresetBugs).
556556
WithLoadForGoAnalysis().
557557
WithURL("https://github.com/breml/errchkjson"),
558+
linter.NewConfig(golinters.NewContainedCtx()).
559+
WithSince("1.44.0").
560+
WithPresets(linter.PresetStyle).
561+
WithLoadForGoAnalysis().
562+
WithURL("https://github.com/sivchari/containedctx"),
558563

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

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)