Skip to content

Commit

Permalink
add containedctx linter (#2382)
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari authored Jan 19, 2022
1 parent 1685402 commit 68f530a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
github.com/shirou/gopsutil/v3 v3.21.12
github.com/sirupsen/logrus v1.8.1
github.com/sivchari/containedctx v1.0.1
github.com/sivchari/tenv v1.4.7
github.com/sonatard/noctx v0.0.1
github.com/sourcegraph/go-diff v0.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/golinters/containedctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/sivchari/containedctx"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewContainedCtx() *goanalysis.Linter {
a := containedctx.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetPerformance, linter.PresetBugs).
WithURL("https://github.com/timakin/bodyclose"),

linter.NewConfig(golinters.NewContainedCtx()).
WithSince("1.44.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/sivchari/containedctx"),

linter.NewConfig(golinters.NewContextCheck()).
WithSince("v1.43.0").
WithPresets(linter.PresetBugs).
Expand Down
15 changes: 15 additions & 0 deletions test/testdata/containedctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// args: -Econtainedctx
package testdata

import "context"

type ok struct {
i int
s string
}

type ng struct {
ctx context.Context // ERROR "found a struct that contains a context.Context field"
}

type empty struct{}

0 comments on commit 68f530a

Please sign in to comment.