diff --git a/.gitignore b/.gitignore index fc1b400..1c2ffa5 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ .idea .DS_Store + +/contextcheck diff --git a/Makefile b/Makefile index 9321e9d..613d35e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,15 @@ +.PHONY: clean test build + +default: test build + +clean: + rm -rf dist/ cover.out + +test: clean + go test -v -cover ./... + build: - @GO111MODULE=on go build -ldflags '-s -w' -o contextcheck ./cmd/contextcheck/main.go + go build -ldflags '-s -w' -o contextcheck ./cmd/contextcheck/main.go install: - @GO111MODULE=on go install -ldflags '-s -w' ./cmd/contextcheck + go install -ldflags '-s -w' ./cmd/contextcheck diff --git a/contextcheck.go b/contextcheck.go index c9ad010..50e11f3 100644 --- a/contextcheck.go +++ b/contextcheck.go @@ -2,6 +2,7 @@ package contextcheck import ( "go/ast" + "go/token" "go/types" "regexp" "strconv" @@ -68,6 +69,11 @@ var ( pkgFactMu sync.RWMutex ) +type element interface { + Pos() token.Pos + Parent() *ssa.Function +} + type resInfo struct { Valid bool Funcs []string @@ -456,7 +462,7 @@ func (r *runner) collectCtxRef(f *ssa.Function, isHttpHandler bool) (refMap map[ for instr := range storeInstrs { 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 } } @@ -464,7 +470,7 @@ func (r *runner) collectCtxRef(f *ssa.Function, isHttpHandler bool) (refMap map[ for instr := range phiInstrs { 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 } } @@ -564,9 +570,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") } } } @@ -578,9 +584,11 @@ func (r *runner) checkFuncWithCtx(f *ssa.Function, tp entryType) { key := ff.RelString(nil) 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), "->")) + if ok && !res.Valid { + if instr.Pos().IsValid() { + r.Reportf(instr, "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->")) + } else { + r.Reportf(ff, "Function `%s` should pass the context parameter", strings.Join(reverse(res.Funcs), "->")) } } } @@ -806,6 +814,20 @@ func (r *runner) setFact(key string, valid bool, funcs ...string) { } } +func (r *runner) Reportf(instr element, 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...) +} + // setPkgFact save fact to mem func setPkgFact(pkg *types.Package, fact ctxFact) { pkgFactMu.Lock() diff --git a/go.mod b/go.mod index 666545f..a702fc9 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,10 @@ go 1.20 require ( github.com/gostaticanalysis/analysisutil v0.7.1 - golang.org/x/tools v0.8.0 + golang.org/x/tools v0.19.0 ) require ( github.com/gostaticanalysis/comment v1.4.2 // indirect - golang.org/x/mod v0.10.0 // indirect - golang.org/x/sys v0.7.0 // indirect + golang.org/x/mod v0.16.0 // indirect ) diff --git a/go.sum b/go.sum index f213be7..898832e 100644 --- a/go.sum +++ b/go.sum @@ -24,20 +24,18 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= -golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 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 golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= golang.org/x/tools v0.1.1-0.20210302220138-2ac05c832e1a/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= -golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y= -golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4= +golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= +golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/testdata/src/a/a.go b/testdata/src/a/a.go index 4c12875..adb8f85 100644 --- a/testdata/src/a/a.go +++ b/testdata/src/a/a.go @@ -155,3 +155,19 @@ func f13[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | return b } + +/* ----------------- issue 21 ----------------- */ + +func f16(ctx context.Context, k string) func() { + return func() { // want "Function `f16\\$1` should pass the context parameter" + f16(context.Background(), k) + } +} + +func f17(ctx context.Context, k string) func() func() { + return func() func() { // want "Function `f17\\$1->f17\\$1\\$1` should pass the context parameter" + return func() { + f16(context.Background(), k) + } + } +}