Skip to content

Commit

Permalink
add containedctx linter (golangci#2382)
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari authored and SeigeC committed Apr 4, 2023
1 parent f404ba5 commit d07d8e3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
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
@@ -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
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
@@ -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 d07d8e3

Please sign in to comment.