This commit is contained in:
Fernandez Ludovic 2025-01-17 00:37:55 +01:00 committed by Gabriel Augendre
parent 7b0afb1f92
commit 2046ce80db
3 changed files with 59 additions and 0 deletions

View file

@ -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

View file

@ -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",

51
pkg/analyzer/testdata/src/cgo/cgo.go vendored Normal file
View 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)
}