mirror of
https://github.com/Crocmagnon/fatcontext.git
synced 2025-04-09 11:06:33 +02:00
Compare commits
15 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bc6bc802c2 | ||
ecae7266e2 | |||
![]() |
134c7a8397 | ||
e5c81ff0dd | |||
![]() |
a3efb8903a | ||
![]() |
96ab752057 | ||
![]() |
2cfdadfd85 | ||
![]() |
5a001f2fbe | ||
![]() |
6f8b43553d | ||
![]() |
3947c6f8d5 | ||
![]() |
b568e8acf1 | ||
![]() |
ed135bb8e7 | ||
![]() |
12e5409dea | ||
![]() |
2046ce80db | ||
![]() |
7b0afb1f92 |
16 changed files with 450 additions and 84 deletions
.github
.golangci.yml.pre-commit-config.yamlcmd/fatcontext
go.modgo.sumpkg/analyzer
4
.github/dependabot.yml
vendored
4
.github/dependabot.yml
vendored
|
@ -6,6 +6,10 @@ updates:
|
||||||
directory: "/"
|
directory: "/"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "monthly"
|
interval: "monthly"
|
||||||
|
groups:
|
||||||
|
github-actions:
|
||||||
|
patterns:
|
||||||
|
- "*" # Group all updates into a single larger pull request.
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/"
|
directory: "/"
|
||||||
schedule:
|
schedule:
|
||||||
|
|
2
.github/workflows/go.yml
vendored
2
.github/workflows/go.yml
vendored
|
@ -16,7 +16,7 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
go: ['1.22', '1.23']
|
go: [stable, oldstable]
|
||||||
os: [macos-latest, windows-latest, ubuntu-latest]
|
os: [macos-latest, windows-latest, ubuntu-latest]
|
||||||
name: build
|
name: build
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
4
.github/workflows/golangci-lint.yml
vendored
4
.github/workflows/golangci-lint.yml
vendored
|
@ -16,7 +16,7 @@ jobs:
|
||||||
golangci:
|
golangci:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go: ['1.22', '1.23']
|
go: [stable, oldstable]
|
||||||
name: lint
|
name: lint
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
@ -25,6 +25,6 @@ jobs:
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.go }}
|
go-version: ${{ matrix.go }}
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v6
|
uses: golangci/golangci-lint-action@v7
|
||||||
with:
|
with:
|
||||||
version: latest
|
version: latest
|
||||||
|
|
2
.github/workflows/goreleaser.yml
vendored
2
.github/workflows/goreleaser.yml
vendored
|
@ -17,6 +17,8 @@ jobs:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: stable
|
||||||
- name: Run GoReleaser
|
- name: Run GoReleaser
|
||||||
uses: goreleaser/goreleaser-action@v6
|
uses: goreleaser/goreleaser-action@v6
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -1,9 +1,18 @@
|
||||||
|
version: "2"
|
||||||
issues:
|
issues:
|
||||||
exclude-dirs:
|
fix: true
|
||||||
- contrib
|
|
||||||
linters:
|
linters:
|
||||||
|
default: all
|
||||||
|
disable:
|
||||||
|
- depguard
|
||||||
|
- exhaustruct
|
||||||
|
formatters:
|
||||||
enable:
|
enable:
|
||||||
- goimports
|
- goimports
|
||||||
linters-settings:
|
- gofmt
|
||||||
goimports:
|
- gofumpt
|
||||||
local-prefixes: "github.com/Crocmagnon/fatcontext"
|
- golines
|
||||||
|
settings:
|
||||||
|
goimports:
|
||||||
|
local-prefixes:
|
||||||
|
- github.com/Crocmagnon/fatcontext
|
||||||
|
|
|
@ -9,7 +9,7 @@ repos:
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- repo: https://github.com/golangci/golangci-lint
|
- repo: https://github.com/golangci/golangci-lint
|
||||||
rev: v1.63.4
|
rev: v2.0.2
|
||||||
hooks:
|
hooks:
|
||||||
- id: golangci-lint-full
|
- id: golangci-lint-full
|
||||||
- repo: local
|
- repo: local
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package main runs the analyzer. It's the CLI entrypoint.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
8
go.mod
8
go.mod
|
@ -1,10 +1,10 @@
|
||||||
module github.com/Crocmagnon/fatcontext
|
module github.com/Crocmagnon/fatcontext
|
||||||
|
|
||||||
go 1.22.0
|
go 1.23.0
|
||||||
|
|
||||||
require golang.org/x/tools v0.28.0
|
require golang.org/x/tools v0.31.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
golang.org/x/mod v0.22.0 // indirect
|
golang.org/x/mod v0.24.0 // indirect
|
||||||
golang.org/x/sync v0.10.0 // indirect
|
golang.org/x/sync v0.12.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
12
go.sum
12
go.sum
|
@ -1,8 +1,8 @@
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
|
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
||||||
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
|
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
|
||||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
|
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
|
||||||
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
|
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package analyzer contains everything related to the linter analysis.
|
||||||
package analyzer
|
package analyzer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -15,25 +16,31 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FlagCheckStructPointers is a possible flag for the analyzer.
|
||||||
|
// Exported to make it usable in golangci-lint.
|
||||||
const FlagCheckStructPointers = "check-struct-pointers"
|
const FlagCheckStructPointers = "check-struct-pointers"
|
||||||
|
|
||||||
|
// NewAnalyzer returns a fatcontext analyzer.
|
||||||
func NewAnalyzer() *analysis.Analyzer {
|
func NewAnalyzer() *analysis.Analyzer {
|
||||||
r := &runner{}
|
rnnr := &runner{}
|
||||||
|
|
||||||
flags := flag.NewFlagSet("fatcontext", flag.ExitOnError)
|
flags := flag.NewFlagSet("fatcontext", flag.ExitOnError)
|
||||||
flags.BoolVar(&r.DetectInStructPointers, FlagCheckStructPointers, false,
|
flags.BoolVar(&rnnr.DetectInStructPointers, FlagCheckStructPointers, false,
|
||||||
"set to true to detect potential fat contexts in struct pointers")
|
"set to true to detect potential fat contexts in struct pointers")
|
||||||
|
|
||||||
return &analysis.Analyzer{
|
return &analysis.Analyzer{
|
||||||
Name: "fatcontext",
|
Name: "fatcontext",
|
||||||
Doc: "detects nested contexts in loops and function literals",
|
Doc: "detects nested contexts in loops and function literals",
|
||||||
Run: r.run,
|
Run: rnnr.run,
|
||||||
Flags: *flags,
|
Flags: *flags,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var errUnknown = errors.New("unknown node type")
|
var (
|
||||||
|
errUnknown = errors.New("unknown node type")
|
||||||
|
errInvalidAnalysis = errors.New("invalid analysis")
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
categoryInLoop = "nested context in loop"
|
categoryInLoop = "nested context in loop"
|
||||||
|
@ -47,7 +54,10 @@ type runner struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
|
func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
|
||||||
inspctr := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
inspctr, typeValid := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
||||||
|
if !typeValid {
|
||||||
|
return nil, errInvalidAnalysis
|
||||||
|
}
|
||||||
|
|
||||||
nodeFilter := []ast.Node{
|
nodeFilter := []ast.Node{
|
||||||
(*ast.ForStmt)(nil),
|
(*ast.ForStmt)(nil),
|
||||||
|
@ -62,6 +72,10 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if body == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
assignStmt := findNestedContext(pass, node, body.List)
|
assignStmt := findNestedContext(pass, node, body.List)
|
||||||
if assignStmt == nil {
|
if assignStmt == nil {
|
||||||
return
|
return
|
||||||
|
@ -82,14 +96,18 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil //nolint:nilnil // we have no result to send to other analyzers
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *runner) shouldIgnoreReport(category string) bool {
|
func (r *runner) shouldIgnoreReport(category string) bool {
|
||||||
return category == categoryInStructPointer && !r.DetectInStructPointers
|
return category == categoryInStructPointer && !r.DetectInStructPointers
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *runner) getSuggestedFixes(pass *analysis.Pass, assignStmt *ast.AssignStmt, category string) []analysis.SuggestedFix {
|
func (r *runner) getSuggestedFixes(
|
||||||
|
pass *analysis.Pass,
|
||||||
|
assignStmt *ast.AssignStmt,
|
||||||
|
category string,
|
||||||
|
) []analysis.SuggestedFix {
|
||||||
switch category {
|
switch category {
|
||||||
case categoryInStructPointer, categoryUnsupported:
|
case categoryInStructPointer, categoryUnsupported:
|
||||||
return nil
|
return nil
|
||||||
|
@ -156,31 +174,9 @@ func getBody(node ast.Node) (*ast.BlockStmt, error) {
|
||||||
func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *ast.AssignStmt {
|
func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *ast.AssignStmt {
|
||||||
for _, stmt := range stmts {
|
for _, stmt := range stmts {
|
||||||
// Recurse if necessary
|
// Recurse if necessary
|
||||||
switch typedStmt := stmt.(type) {
|
stmtList := getStmtList(stmt)
|
||||||
case *ast.BlockStmt:
|
if found := findNestedContext(pass, node, stmtList); found != nil {
|
||||||
if found := findNestedContext(pass, node, typedStmt.List); found != nil {
|
return found
|
||||||
return found
|
|
||||||
}
|
|
||||||
case *ast.IfStmt:
|
|
||||||
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
|
|
||||||
return found
|
|
||||||
}
|
|
||||||
case *ast.SwitchStmt:
|
|
||||||
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
|
|
||||||
return found
|
|
||||||
}
|
|
||||||
case *ast.CaseClause:
|
|
||||||
if found := findNestedContext(pass, node, typedStmt.Body); found != nil {
|
|
||||||
return found
|
|
||||||
}
|
|
||||||
case *ast.SelectStmt:
|
|
||||||
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
|
|
||||||
return found
|
|
||||||
}
|
|
||||||
case *ast.CommClause:
|
|
||||||
if found := findNestedContext(pass, node, typedStmt.Body); found != nil {
|
|
||||||
return found
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actually check for nested context
|
// Actually check for nested context
|
||||||
|
@ -222,28 +218,48 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// render returns the pretty-print of the given node
|
func getStmtList(stmt ast.Stmt) []ast.Stmt {
|
||||||
|
switch typedStmt := stmt.(type) {
|
||||||
|
case *ast.BlockStmt:
|
||||||
|
return typedStmt.List
|
||||||
|
case *ast.IfStmt:
|
||||||
|
return typedStmt.Body.List
|
||||||
|
case *ast.SwitchStmt:
|
||||||
|
return typedStmt.Body.List
|
||||||
|
case *ast.CaseClause:
|
||||||
|
return typedStmt.Body
|
||||||
|
case *ast.SelectStmt:
|
||||||
|
return typedStmt.Body.List
|
||||||
|
case *ast.CommClause:
|
||||||
|
return typedStmt.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// render returns the pretty-print of the given node.
|
||||||
func render(fset *token.FileSet, x interface{}) ([]byte, error) {
|
func render(fset *token.FileSet, x interface{}) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := printer.Fprint(&buf, fset, x); err != nil {
|
if err := printer.Fprint(&buf, fset, x); err != nil {
|
||||||
return nil, fmt.Errorf("printing node: %w", err)
|
return nil, fmt.Errorf("printing node: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isContextFunction(exp ast.Expr, fnName ...string) bool {
|
func isContextFunction(exp ast.Expr, fnName ...string) bool {
|
||||||
call, ok := exp.(*ast.CallExpr)
|
call, typeValid := exp.(*ast.CallExpr)
|
||||||
if !ok {
|
if !typeValid {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
selector, ok := call.Fun.(*ast.SelectorExpr)
|
selector, typeValid := call.Fun.(*ast.SelectorExpr)
|
||||||
if !ok {
|
if !typeValid {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
ident, ok := selector.X.(*ast.Ident)
|
ident, typeValid := selector.X.(*ast.Ident)
|
||||||
if !ok {
|
if !typeValid {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,16 +287,17 @@ func isWithinLoop(exp ast.Expr, node ast.Node, pass *analysis.Pass) bool {
|
||||||
|
|
||||||
func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
|
func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
|
||||||
for {
|
for {
|
||||||
switch n := node.(type) {
|
switch typedNode := node.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
return n
|
return typedNode
|
||||||
case *ast.IndexExpr:
|
case *ast.IndexExpr:
|
||||||
node = n.X
|
node = typedNode.X
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
if sel, ok := pass.TypesInfo.Selections[n]; ok && sel.Indirect() {
|
if sel, ok := pass.TypesInfo.Selections[typedNode]; ok && sel.Indirect() {
|
||||||
return nil // indirected (pointer) roots don't imply a (safe) copy
|
return nil // indirected (pointer) roots don't imply a (safe) copy
|
||||||
}
|
}
|
||||||
node = n.X
|
|
||||||
|
node = typedNode.X
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -288,9 +305,10 @@ func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPointer(pass *analysis.Pass, exp ast.Node) bool {
|
func isPointer(pass *analysis.Pass, exp ast.Node) bool {
|
||||||
switch n := exp.(type) {
|
switch n := exp.(type) { //nolint:gocritic // Future-proofing with switch instead of if.
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
sel, ok := pass.TypesInfo.Selections[n]
|
sel, ok := pass.TypesInfo.Selections[n]
|
||||||
|
|
||||||
return ok && sel.Indirect()
|
return ok && sel.Indirect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package analyzer_test
|
package analyzer_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/tools/go/analysis/analysistest"
|
"golang.org/x/tools/go/analysis/analysistest"
|
||||||
|
@ -11,27 +9,59 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAnalyzer(t *testing.T) {
|
func TestAnalyzer(t *testing.T) {
|
||||||
wd, err := os.Getwd()
|
t.Parallel()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to get wd: %s", err)
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
dir string
|
||||||
|
options map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "no func decl",
|
||||||
|
dir: "common",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "no func decl",
|
||||||
|
dir: "no_structpointer",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "func decl",
|
||||||
|
dir: "common",
|
||||||
|
options: map[string]string{
|
||||||
|
analyzer.FlagCheckStructPointers: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "func decl",
|
||||||
|
dir: "structpointer",
|
||||||
|
options: map[string]string{
|
||||||
|
analyzer.FlagCheckStructPointers: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
testdata := filepath.Join(wd, "testdata")
|
|
||||||
|
|
||||||
t.Run("no func decl", func(t *testing.T) {
|
for _, test := range testCases {
|
||||||
an := analyzer.NewAnalyzer()
|
t.Run(test.desc+"_"+test.dir, func(t *testing.T) {
|
||||||
analysistest.Run(t, testdata, an, "./common")
|
t.Parallel()
|
||||||
analysistest.Run(t, testdata, an, "./no_structpointer")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("func decl", func(t *testing.T) {
|
anlzr := analyzer.NewAnalyzer()
|
||||||
an := analyzer.NewAnalyzer()
|
|
||||||
|
|
||||||
err := an.Flags.Set(analyzer.FlagCheckStructPointers, "true")
|
for k, v := range test.options {
|
||||||
if err != nil {
|
err := anlzr.Flags.Set(k, v)
|
||||||
t.Fatal(err)
|
if err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
analysistest.Run(t, testdata, an, "./common")
|
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), anlzr, test.dir)
|
||||||
analysistest.Run(t, testdata, an, "./structpointer")
|
})
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAnalyzer_cgo(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
a := analyzer.NewAnalyzer()
|
||||||
|
|
||||||
|
analysistest.Run(t, analysistest.TestData(), a, "cgo")
|
||||||
}
|
}
|
||||||
|
|
51
pkg/analyzer/testdata/src/cgo/cgo.go
vendored
Normal file
51
pkg/analyzer/testdata/src/cgo/cgo.go
vendored
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package cgo
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void myprint(char* s) {
|
||||||
|
printf("%d\n", s);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
cs := C.CString("Hello from stdio\n")
|
||||||
|
C.myprint(cs)
|
||||||
|
C.free(unsafe.Pointer(cs))
|
||||||
|
}
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx := context.WithValue(ctx, "key", i)
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx = context.WithValue(ctx, "key", i) // want "nested context in loop"
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for item := range []string{"one", "two", "three"} {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
ctx := context.WithValue(ctx, "key", item)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapContext(ctx context.Context) context.Context {
|
||||||
|
return context.WithoutCancel(ctx)
|
||||||
|
}
|
251
pkg/analyzer/testdata/src/common/example.go.golden
vendored
Normal file
251
pkg/analyzer/testdata/src/common/example.go.golden
vendored
Normal file
|
@ -0,0 +1,251 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func example() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx := context.WithValue(ctx, "key", i)
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx := context.WithValue(ctx, "key", i) // want "nested context in loop"
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for item := range []string{"one", "two", "three"} {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
ctx := context.WithValue(ctx, "key", item)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// not fooled by shadowing in nested blocks
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
default:
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// detects contexts wrapped in function literals (this is risky as function literals can be called multiple times)
|
||||||
|
_ = func() {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in function literal"
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is fine because the context is created in the loop
|
||||||
|
for {
|
||||||
|
if ctx := context.Background(); doSomething() != nil {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx2 := context.Background()
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
if doSomething() != nil {
|
||||||
|
ctx2 = wrapContext(ctx2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapContext(ctx context.Context) context.Context {
|
||||||
|
return context.WithoutCancel(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doSomething() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// storing contexts in a struct isn't recommended, but local copies of a non-pointer struct should act like local copies of a context.
|
||||||
|
func inStructs(ctx context.Context) {
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := struct{ Ctx context.Context }{ctx}
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "key", i)
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := []struct{ Ctx context.Context }{{ctx}}
|
||||||
|
c[0].Ctx = context.WithValue(c[0].Ctx, "key", i)
|
||||||
|
c[0].Ctx = context.WithValue(c[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
c := struct{ Ctx context.Context }{ctx}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := c
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "key", i)
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
pc := &struct{ Ctx context.Context }{ctx}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := pc
|
||||||
|
c.Ctx := context.WithValue(c.Ctx, "key", i) // want "nested context in loop"
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
r := []struct{ Ctx context.Context }{{ctx}}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
r[0].Ctx := context.WithValue(r[0].Ctx, "key", i) // want "nested context in loop"
|
||||||
|
r[0].Ctx = context.WithValue(r[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
rp := []*struct{ Ctx context.Context }{{ctx}}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
rp[0].Ctx := context.WithValue(rp[0].Ctx, "key", i) // want "nested context in loop"
|
||||||
|
rp[0].Ctx = context.WithValue(rp[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func inVariousNestedBlocks(ctx context.Context) {
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
if true {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
switch err {
|
||||||
|
default:
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
{
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware could run on every request, bloating the request parameter level context and causing a memory leak
|
||||||
|
func badMiddleware(ctx context.Context) func() error {
|
||||||
|
return func() error {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in function literal"
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware is fine, as it doesn't modify the context of parent function
|
||||||
|
func okMiddleware(ctx context.Context) func() error {
|
||||||
|
return func() error {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware is fine, as it only modifies the context passed to it
|
||||||
|
func okMiddleware2(ctx context.Context) func(ctx context.Context) error {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func doSomethingWithCtx(ctx context.Context) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCasesInit(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
ctx context.Context
|
||||||
|
}{
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
ctx: context.WithValue(context.Background(), "key", "value"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run("some test", func(t *testing.T) {
|
||||||
|
if tc.ctx == nil {
|
||||||
|
tc.ctx = context.Background()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue