Skip to content

Commit

Permalink
add containedctx linter
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari committed Nov 28, 2021
1 parent a5f0a4d commit 737a771
Show file tree
Hide file tree
Showing 5 changed files with 46 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.10
github.com/sirupsen/logrus v1.8.1
github.com/sivchari/containedctx v1.0.0
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.

23 changes: 23 additions & 0 deletions pkg/golinters/containedctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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

analyzers := []*analysis.Analyzer{
a,
}

return goanalysis.NewLinter(
a.Name,
a.Doc,
analyzers,
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 @@ -555,6 +555,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/breml/errchkjson"),
linter.NewConfig(golinters.NewContainedCtx()).
WithSince("1.44.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/sivchari/containedctx"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
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 737a771

Please sign in to comment.