diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 6aed4f4..42c06f4 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -62,6 +62,10 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) { return } + if body == nil { + return + } + assignStmt := findNestedContext(pass, node, body.List) if assignStmt == nil { return diff --git a/pkg/analyzer/analyzer_test.go b/pkg/analyzer/analyzer_test.go index 7ea8e42..0012522 100644 --- a/pkg/analyzer/analyzer_test.go +++ b/pkg/analyzer/analyzer_test.go @@ -22,6 +22,10 @@ func TestAnalyzer(t *testing.T) { desc: "no func decl", dir: "no_structpointer", }, + { + desc: "no func decl", + dir: "cgo", + }, { desc: "func decl", dir: "common", diff --git a/pkg/analyzer/testdata/src/cgo/cgo.go b/pkg/analyzer/testdata/src/cgo/cgo.go new file mode 100644 index 0000000..c87b575 --- /dev/null +++ b/pkg/analyzer/testdata/src/cgo/cgo.go @@ -0,0 +1,51 @@ +package cgo + +/* + #include + #include + + 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) +}