Skip to content

Commit 7fdfd4d

Browse files
committed
feat: use parent position if current position invalid
1 parent fbe3efa commit 7fdfd4d

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

contextcheck.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -455,24 +455,16 @@ func (r *runner) collectCtxRef(f *ssa.Function, isHttpHandler bool) (refMap map[
455455
}
456456

457457
for instr := range storeInstrs {
458-
if !instr.Pos().IsValid() {
459-
continue
460-
}
461-
462458
if !checkedRefMap[instr.Val] {
463-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
459+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
464460
ok = false
465461
}
466462
}
467463

468464
for instr := range phiInstrs {
469-
if !instr.Pos().IsValid() {
470-
continue
471-
}
472-
473465
for _, v := range instr.Edges {
474466
if !checkedRefMap[v] {
475-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
467+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
476468
ok = false
477469
}
478470
}
@@ -559,10 +551,6 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {
559551

560552
for _, b := range f.Blocks {
561553
for _, instr := range b.Instrs {
562-
if !instr.Pos().IsValid() {
563-
continue
564-
}
565-
566554
tp, ok := r.getCtxType(instr)
567555
if !ok {
568556
continue
@@ -576,9 +564,9 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {
576564
if tp&CtxIn != 0 {
577565
if !refMap[instr] {
578566
if isHttpHandler {
579-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead")
567+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead")
580568
} else {
581-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
569+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
582570
}
583571
}
584572
}
@@ -592,13 +580,26 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {
592580
res, ok := r.getValue(key, ff)
593581
if ok {
594582
if !res.Valid {
595-
r.pass.Reportf(instr.Pos(), "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->"))
583+
r.Reportf(instr, "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->"))
596584
}
597585
}
598586
}
599587
}
600588
}
601589

590+
func (r *runner) Reportf(instr ssa.Instruction, format string, args ...interface{}) {
591+
pos := instr.Pos()
592+
if !pos.IsValid() && instr.Parent() != nil {
593+
pos = instr.Parent().Pos()
594+
}
595+
596+
if !pos.IsValid() {
597+
return
598+
}
599+
600+
r.pass.Reportf(pos, format, args...)
601+
}
602+
602603
func (r *runner) checkFuncWithoutCtx(f *ssa.Function, checkingMap map[string]bool) (ret bool) {
603604
ret = true
604605
orgKey := f.RelString(nil)

testdata/src/a/a.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func f13[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 |
158158

159159
/* ----------------- issue 21 ----------------- */
160160

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

0 commit comments

Comments
 (0)