Skip to content

Commit

Permalink
feat: use parent position if current position invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 21, 2024
1 parent fbe3efa commit 7fdfd4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 18 additions & 17 deletions contextcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,24 +455,16 @@ func (r *runner) collectCtxRef(f *ssa.Function, isHttpHandler bool) (refMap map[
}

for instr := range storeInstrs {
if !instr.Pos().IsValid() {
continue
}

if !checkedRefMap[instr.Val] {
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
ok = false
}
}

for instr := range phiInstrs {
if !instr.Pos().IsValid() {
continue
}

for _, v := range instr.Edges {
if !checkedRefMap[v] {
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
ok = false
}
}
Expand Down Expand Up @@ -559,10 +551,6 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {

for _, b := range f.Blocks {
for _, instr := range b.Instrs {
if !instr.Pos().IsValid() {
continue
}

tp, ok := r.getCtxType(instr)
if !ok {
continue
Expand All @@ -576,9 +564,9 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {
if tp&CtxIn != 0 {
if !refMap[instr] {
if isHttpHandler {
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead")
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead")
} else {
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
}
}
}
Expand All @@ -592,13 +580,26 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {
res, ok := r.getValue(key, ff)
if ok {
if !res.Valid {
r.pass.Reportf(instr.Pos(), "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->"))
r.Reportf(instr, "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->"))
}
}
}
}
}

func (r *runner) Reportf(instr ssa.Instruction, format string, args ...interface{}) {
pos := instr.Pos()
if !pos.IsValid() && instr.Parent() != nil {
pos = instr.Parent().Pos()
}

if !pos.IsValid() {
return
}

r.pass.Reportf(pos, format, args...)
}

func (r *runner) checkFuncWithoutCtx(f *ssa.Function, checkingMap map[string]bool) (ret bool) {
ret = true
orgKey := f.RelString(nil)
Expand Down
2 changes: 1 addition & 1 deletion testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func f13[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 |

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

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

0 comments on commit 7fdfd4d

Please sign in to comment.