2018-12-31 02:18:41 +01:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import "honnef.co/go/tools/ssa"
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
// Terminates reports whether fn is supposed to return, that is if it
|
2018-12-31 02:18:41 +01:00
|
|
|
// has at least one theoretic path that returns from the function.
|
|
|
|
// Explicit panics do not count as terminating.
|
2020-05-09 15:44:17 +02:00
|
|
|
func Terminates(fn *ssa.Function) bool {
|
2018-12-31 02:18:41 +01:00
|
|
|
if fn.Blocks == nil {
|
|
|
|
// assuming that a function terminates is the conservative
|
|
|
|
// choice
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, block := range fn.Blocks {
|
|
|
|
if len(block.Instrs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := block.Instrs[len(block.Instrs)-1].(*ssa.Return); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|