Skip to content

Commit

Permalink
Merge pull request timakin#28 from tdakkota/fix/issue27
Browse files Browse the repository at this point in the history
Fix: timakin#27, when function returns io.Closer/io.ReadCloser
  • Loading branch information
timakin authored Apr 24, 2020
2 parents f7f2e9b + b2984ad commit cb62158
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions passes/bodyclose/bodyclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions passes/bodyclose/testdata/src/a/issue27.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit cb62158

Please sign in to comment.