mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-28 17:16:24 +00:00
* update github.com/docker/cli * update github.com/docker/distribution * update github.com/docker/docker * update github.com/gin-gonic/gin * update github.com/golang-jwt/jwt/v4 * update github.com/golangci/golangci-lint * update github.com/gorilla/securecookie * update github.com/mattn/go-sqlite3 * update github.com/moby/moby * update github.com/prometheus/client_golang * update github.com/xanzy/go-gitlab
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package containedctx
|
|
|
|
import (
|
|
"go/ast"
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
"golang.org/x/tools/go/analysis/passes/inspect"
|
|
"golang.org/x/tools/go/ast/inspector"
|
|
)
|
|
|
|
const doc = "containedctx is a linter that detects struct contained context.Context field"
|
|
|
|
// Analyzer is the contanedctx analyzer
|
|
var Analyzer = &analysis.Analyzer{
|
|
Name: "containedctx",
|
|
Doc: doc,
|
|
Run: run,
|
|
Requires: []*analysis.Analyzer{
|
|
inspect.Analyzer,
|
|
},
|
|
}
|
|
|
|
func run(pass *analysis.Pass) (interface{}, error) {
|
|
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
|
|
|
nodeFilter := []ast.Node{
|
|
(*ast.StructType)(nil),
|
|
}
|
|
|
|
inspect.Preorder(nodeFilter, func(n ast.Node) {
|
|
switch structTyp := n.(type) {
|
|
case *ast.StructType:
|
|
if structTyp.Fields.List == nil {
|
|
return
|
|
}
|
|
for _, field := range structTyp.Fields.List {
|
|
selectorExpr, ok := field.Type.(*ast.SelectorExpr)
|
|
if !ok {
|
|
continue
|
|
}
|
|
xname, ok := selectorExpr.X.(*ast.Ident)
|
|
if !ok {
|
|
continue
|
|
}
|
|
selname := selectorExpr.Sel.Name
|
|
if xname.Name+"."+selname == "context.Context" {
|
|
pass.Reportf(field.Pos(), "found a struct that contains a context.Context field")
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
return nil, nil
|
|
}
|