Skip to content

Commit 2046ce8

Browse files
ldezCrocmagnon
authored andcommittedJan 16, 2025·
fix: cgo
1 parent 7b0afb1 commit 2046ce8

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
 

‎pkg/analyzer/analyzer.go

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
6262
return
6363
}
6464

65+
if body == nil {
66+
return
67+
}
68+
6569
assignStmt := findNestedContext(pass, node, body.List)
6670
if assignStmt == nil {
6771
return

‎pkg/analyzer/analyzer_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func TestAnalyzer(t *testing.T) {
2222
desc: "no func decl",
2323
dir: "no_structpointer",
2424
},
25+
{
26+
desc: "no func decl",
27+
dir: "cgo",
28+
},
2529
{
2630
desc: "func decl",
2731
dir: "common",

‎pkg/analyzer/testdata/src/cgo/cgo.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cgo
2+
3+
/*
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
void myprint(char* s) {
8+
printf("%d\n", s);
9+
}
10+
*/
11+
import "C"
12+
13+
import (
14+
"context"
15+
"unsafe"
16+
)
17+
18+
func _() {
19+
cs := C.CString("Hello from stdio\n")
20+
C.myprint(cs)
21+
C.free(unsafe.Pointer(cs))
22+
}
23+
24+
func _() {
25+
ctx := context.Background()
26+
27+
for i := 0; i < 10; i++ {
28+
ctx := context.WithValue(ctx, "key", i)
29+
ctx = context.WithValue(ctx, "other", "val")
30+
}
31+
32+
for i := 0; i < 10; i++ {
33+
ctx = context.WithValue(ctx, "key", i) // want "nested context in loop"
34+
ctx = context.WithValue(ctx, "other", "val")
35+
}
36+
37+
for item := range []string{"one", "two", "three"} {
38+
ctx = wrapContext(ctx) // want "nested context in loop"
39+
ctx := context.WithValue(ctx, "key", item)
40+
ctx = wrapContext(ctx)
41+
}
42+
43+
for {
44+
ctx = wrapContext(ctx) // want "nested context in loop"
45+
break
46+
}
47+
}
48+
49+
func wrapContext(ctx context.Context) context.Context {
50+
return context.WithoutCancel(ctx)
51+
}

0 commit comments

Comments
 (0)
Please sign in to comment.