pure.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package functions
  2. import (
  3. "honnef.co/go/tools/ssa"
  4. )
  5. func filterDebug(instr []ssa.Instruction) []ssa.Instruction {
  6. var out []ssa.Instruction
  7. for _, ins := range instr {
  8. if _, ok := ins.(*ssa.DebugRef); !ok {
  9. out = append(out, ins)
  10. }
  11. }
  12. return out
  13. }
  14. // IsStub reports whether a function is a stub. A function is
  15. // considered a stub if it has no instructions or exactly one
  16. // instruction, which must be either returning only constant values or
  17. // a panic.
  18. func IsStub(fn *ssa.Function) bool {
  19. if len(fn.Blocks) == 0 {
  20. return true
  21. }
  22. if len(fn.Blocks) > 1 {
  23. return false
  24. }
  25. instrs := filterDebug(fn.Blocks[0].Instrs)
  26. if len(instrs) != 1 {
  27. return false
  28. }
  29. switch instrs[0].(type) {
  30. case *ssa.Return:
  31. // Since this is the only instruction, the return value must
  32. // be a constant. We consider all constants as stubs, not just
  33. // the zero value. This does not, unfortunately, cover zero
  34. // initialised structs, as these cause additional
  35. // instructions.
  36. return true
  37. case *ssa.Panic:
  38. return true
  39. default:
  40. return false
  41. }
  42. }