From b2984ad28889e95e26ee02fdae85cad6c83aafb5 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Tue, 24 Mar 2020 13:58:12 +0300 Subject: [PATCH] fix: #27, when function returns io.Closer/io.ReadCloser --- passes/bodyclose/bodyclose.go | 14 ++++++++++ passes/bodyclose/testdata/src/a/issue27.go | 30 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 passes/bodyclose/testdata/src/a/issue27.go diff --git a/passes/bodyclose/bodyclose.go b/passes/bodyclose/bodyclose.go index 3c702ab..145d540 100644 --- a/passes/bodyclose/bodyclose.go +++ b/passes/bodyclose/bodyclose.go @@ -250,6 +250,20 @@ func (r *runner) isCloseCall(ccall ssa.Instruction) bool { } } } + + if returnOp, ok := cs.(*ssa.Return); ok { + for _, resultValue := range returnOp.Results { + if resultValue.Type().String() == "io.Closer" { + return true + } + } + } + } + } + case *ssa.Return: + for _, resultValue := range ccall.Results { + if resultValue.Type().String() == "io.ReadCloser" { + return true } } } diff --git a/passes/bodyclose/testdata/src/a/issue27.go b/passes/bodyclose/testdata/src/a/issue27.go new file mode 100644 index 0000000..b44b3e4 --- /dev/null +++ b/passes/bodyclose/testdata/src/a/issue27.go @@ -0,0 +1,30 @@ +package a + +import ( + "io" + "net/http" +) + +func issue27_1(url string) (io.ReadCloser, error) { // body should be closed by User + r, err := http.DefaultClient.Get(url) + if err != nil { + return nil, err + } + return r.Body, nil +} + +func issue27_2(url string) (io.Closer, error) { // body should be closed by User + r, err := http.DefaultClient.Get(url) + if err != nil { + return nil, err + } + return r.Body, nil +} + +func issue27_3(url string) (io.Closer, error) { // body should be closed by User + r, err := http.DefaultClient.Get(url) + if err != nil { + return nil, err + } + return r.Body, nil +}