Skip to content

Commit 82fe695

Browse files
authored
Merge pull request #22 from ldez/fix/report-without-filepath
fix: don't report issue related to invalid position
2 parents b4dc4b7 + 9f1f2d1 commit 82fe695

File tree

6 files changed

+66
-19
lines changed

6 files changed

+66
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616

1717
.idea
1818
.DS_Store
19+
20+
/contextcheck

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
.PHONY: clean test build
2+
3+
default: test build
4+
5+
clean:
6+
rm -rf dist/ cover.out
7+
8+
test: clean
9+
go test -v -cover ./...
10+
111
build:
2-
@GO111MODULE=on go build -ldflags '-s -w' -o contextcheck ./cmd/contextcheck/main.go
12+
go build -ldflags '-s -w' -o contextcheck ./cmd/contextcheck/main.go
313

414
install:
5-
@GO111MODULE=on go install -ldflags '-s -w' ./cmd/contextcheck
15+
go install -ldflags '-s -w' ./cmd/contextcheck

contextcheck.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package contextcheck
22

33
import (
44
"go/ast"
5+
"go/token"
56
"go/types"
67
"regexp"
78
"strconv"
@@ -68,6 +69,11 @@ var (
6869
pkgFactMu sync.RWMutex
6970
)
7071

72+
type element interface {
73+
Pos() token.Pos
74+
Parent() *ssa.Function
75+
}
76+
7177
type resInfo struct {
7278
Valid bool
7379
Funcs []string
@@ -456,15 +462,15 @@ func (r *runner) collectCtxRef(f *ssa.Function, isHttpHandler bool) (refMap map[
456462

457463
for instr := range storeInstrs {
458464
if !checkedRefMap[instr.Val] {
459-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
465+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
460466
ok = false
461467
}
462468
}
463469

464470
for instr := range phiInstrs {
465471
for _, v := range instr.Edges {
466472
if !checkedRefMap[v] {
467-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
473+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
468474
ok = false
469475
}
470476
}
@@ -564,9 +570,9 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {
564570
if tp&CtxIn != 0 {
565571
if !refMap[instr] {
566572
if isHttpHandler {
567-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead")
573+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead")
568574
} else {
569-
r.pass.Reportf(instr.Pos(), "Non-inherited new context, use function like `context.WithXXX` instead")
575+
r.Reportf(instr, "Non-inherited new context, use function like `context.WithXXX` instead")
570576
}
571577
}
572578
}
@@ -578,9 +584,11 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) {
578584

579585
key := ff.RelString(nil)
580586
res, ok := r.getValue(key, ff)
581-
if ok {
582-
if !res.Valid {
583-
r.pass.Reportf(instr.Pos(), "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->"))
587+
if ok && !res.Valid {
588+
if instr.Pos().IsValid() {
589+
r.Reportf(instr, "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->"))
590+
} else {
591+
r.Reportf(ff, "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->"))
584592
}
585593
}
586594
}
@@ -806,6 +814,20 @@ func (r *runner) setFact(key string, valid bool, funcs ...string) {
806814
}
807815
}
808816

817+
func (r *runner) Reportf(instr element, format string, args ...interface{}) {
818+
pos := instr.Pos()
819+
820+
if !pos.IsValid() && instr.Parent() != nil {
821+
pos = instr.Parent().Pos()
822+
}
823+
824+
if !pos.IsValid() {
825+
return
826+
}
827+
828+
r.pass.Reportf(pos, format, args...)
829+
}
830+
809831
// setPkgFact save fact to mem
810832
func setPkgFact(pkg *types.Package, fact ctxFact) {
811833
pkgFactMu.Lock()

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ go 1.20
44

55
require (
66
github.com/gostaticanalysis/analysisutil v0.7.1
7-
golang.org/x/tools v0.8.0
7+
golang.org/x/tools v0.19.0
88
)
99

1010
require (
1111
github.com/gostaticanalysis/comment v1.4.2 // indirect
12-
golang.org/x/mod v0.10.0 // indirect
13-
golang.org/x/sys v0.7.0 // indirect
12+
golang.org/x/mod v0.16.0 // indirect
1413
)

go.sum

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,18 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
2424
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
2525
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
2626
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
27-
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
28-
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
27+
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
28+
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
2929
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
3030
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
3131
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
3232
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3333
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
34-
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
34+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
3535
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3636
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3737
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3838
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
39-
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
40-
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4139
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4240
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
4341
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
@@ -46,8 +44,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
4644
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
4745
golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU=
4846
golang.org/x/tools v0.1.1-0.20210302220138-2ac05c832e1a/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU=
49-
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
50-
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
47+
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
48+
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
5149
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5250
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5351
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

testdata/src/a/a.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,19 @@ func f13[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 |
155155

156156
return b
157157
}
158+
159+
/* ----------------- issue 21 ----------------- */
160+
161+
func f16(ctx context.Context, k string) func() {
162+
return func() { // want "Function `f16\\$1` should pass the context parameter"
163+
f16(context.Background(), k)
164+
}
165+
}
166+
167+
func f17(ctx context.Context, k string) func() func() {
168+
return func() func() { // want "Function `f17\\$1->f17\\$1\\$1` should pass the context parameter"
169+
return func() {
170+
f16(context.Background(), k)
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)