Skip to content

Commit

Permalink
feat: improve position for closure
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 21, 2024
1 parent 7fdfd4d commit 3cc6ce5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions contextcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,22 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {

func (r *runner) Reportf(instr ssa.Instruction, format string, args ...interface{}) {
pos := instr.Pos()

if !pos.IsValid() && instr.Block() != nil {
if closure, ok := instr.(*ssa.MakeClosure); ok {
for _, in := range closure.Block().Instrs {
if !in.Pos().IsValid() {
continue
}

if r, ok := in.(*ssa.Return); ok {
pos = r.Pos()
break
}
}
}
}

if !pos.IsValid() && instr.Parent() != nil {
pos = instr.Parent().Pos()
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ func f13[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 |

/* ----------------- issue 21 ----------------- */

func f16(ctx context.Context, k string) func() { // want "Function `f16\\$1` should pass the context parameter"
return func() {
func f16(ctx context.Context, k string) func() {
return func() { // want "Function `f16\\$1` should pass the context parameter"
f16(context.Background(), k)
}
}

0 comments on commit 3cc6ce5

Please sign in to comment.