Gianguido Sorà 72e137e344 Added "bodyclose" support
This commit adds full support for bodyclose linter
(https://github.com/timakin/bodyclose), which checks if an `http.Body`
element is correctly closed after usage.

Since it can be used via `go/analysis', I followed the `govet' example
as suggested by https://github.com/golangci/golangci-lint/wiki/How-to-add-a-custom-linter.

This commit is fully tested, and contains a (flawed) test program which
calls `http.Get()' on `https://google.com' and does not closes its
corresponding `http.Body'.
2019-06-09 13:04:30 +03:00

32 lines
635 B
Go

package analysisutil
import "golang.org/x/tools/go/ssa"
// IfInstr returns *ssa.If which is contained in the block b.
// If the block b has not any if instruction, IfInstr returns nil.
func IfInstr(b *ssa.BasicBlock) *ssa.If {
if len(b.Instrs) == 0 {
return nil
}
ifinstr, ok := b.Instrs[len(b.Instrs)-1].(*ssa.If)
if !ok {
return nil
}
return ifinstr
}
// Phi returns phi values which are contained in the block b.
func Phi(b *ssa.BasicBlock) (phis []*ssa.Phi) {
for _, instr := range b.Instrs {
if phi, ok := instr.(*ssa.Phi); ok {
phis = append(phis, phi)
} else {
// no more phi
break
}
}
return
}