Skip to content

Commit

Permalink
fix: cgo
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored and Crocmagnon committed Jan 16, 2025
1 parent 7b0afb1 commit 2046ce8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 51 additions & 0 deletions pkg/analyzer/testdata/src/cgo/cgo.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2046ce8

Please # to comment.