You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unconditional-recursion.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package rule
  2. import (
  3. "go/ast"
  4. "github.com/mgechev/revive/lint"
  5. )
  6. // UnconditionalRecursionRule lints given else constructs.
  7. type UnconditionalRecursionRule struct{}
  8. // Apply applies the rule to given file.
  9. func (r *UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
  10. var failures []lint.Failure
  11. onFailure := func(failure lint.Failure) {
  12. failures = append(failures, failure)
  13. }
  14. w := lintUnconditionalRecursionRule{onFailure: onFailure}
  15. ast.Walk(w, file.AST)
  16. return failures
  17. }
  18. // Name returns the rule name.
  19. func (r *UnconditionalRecursionRule) Name() string {
  20. return "unconditional-recursion"
  21. }
  22. type funcDesc struct {
  23. reciverID *ast.Ident
  24. id *ast.Ident
  25. }
  26. func (fd *funcDesc) equal(other *funcDesc) bool {
  27. receiversAreEqual := (fd.reciverID == nil && other.reciverID == nil) || fd.reciverID != nil && other.reciverID != nil && fd.reciverID.Name == other.reciverID.Name
  28. idsAreEqual := (fd.id == nil && other.id == nil) || fd.id.Name == other.id.Name
  29. return receiversAreEqual && idsAreEqual
  30. }
  31. type funcStatus struct {
  32. funcDesc *funcDesc
  33. seenConditionalExit bool
  34. }
  35. type lintUnconditionalRecursionRule struct {
  36. onFailure func(lint.Failure)
  37. currentFunc *funcStatus
  38. }
  39. // Visit will traverse the file AST.
  40. // The rule is based in the following algorithm: inside each function body we search for calls to the function itself.
  41. // We do not search inside conditional control structures (if, for, switch, ...) because any recursive call inside them is conditioned
  42. // We do search inside conditional control structures are statements that will take the control out of the function (return, exit, panic)
  43. // If we find conditional control exits, it means the function is NOT unconditionally-recursive
  44. // If we find a recursive call before finding any conditional exit, a failure is generated
  45. // In resume: if we found a recursive call control-dependant from the entry point of the function then we raise a failure.
  46. func (w lintUnconditionalRecursionRule) Visit(node ast.Node) ast.Visitor {
  47. switch n := node.(type) {
  48. case *ast.FuncDecl:
  49. var rec *ast.Ident
  50. switch {
  51. case n.Recv == nil || n.Recv.NumFields() < 1 || len(n.Recv.List[0].Names) < 1:
  52. rec = nil
  53. default:
  54. rec = n.Recv.List[0].Names[0]
  55. }
  56. w.currentFunc = &funcStatus{&funcDesc{rec, n.Name}, false}
  57. case *ast.CallExpr:
  58. var funcID *ast.Ident
  59. var selector *ast.Ident
  60. switch c := n.Fun.(type) {
  61. case *ast.Ident:
  62. selector = nil
  63. funcID = c
  64. case *ast.SelectorExpr:
  65. var ok bool
  66. selector, ok = c.X.(*ast.Ident)
  67. if !ok { // a.b....Foo()
  68. return nil
  69. }
  70. funcID = c.Sel
  71. default:
  72. return w
  73. }
  74. if w.currentFunc != nil && // not in a func body
  75. !w.currentFunc.seenConditionalExit && // there is a conditional exit in the function
  76. w.currentFunc.funcDesc.equal(&funcDesc{selector, funcID}) {
  77. w.onFailure(lint.Failure{
  78. Category: "logic",
  79. Confidence: 1,
  80. Node: n,
  81. Failure: "unconditional recursive call",
  82. })
  83. }
  84. case *ast.IfStmt:
  85. w.updateFuncStatus(n.Body)
  86. w.updateFuncStatus(n.Else)
  87. return nil
  88. case *ast.SelectStmt:
  89. w.updateFuncStatus(n.Body)
  90. return nil
  91. case *ast.RangeStmt:
  92. w.updateFuncStatus(n.Body)
  93. return nil
  94. case *ast.TypeSwitchStmt:
  95. w.updateFuncStatus(n.Body)
  96. return nil
  97. case *ast.SwitchStmt:
  98. w.updateFuncStatus(n.Body)
  99. return nil
  100. case *ast.GoStmt:
  101. for _, a := range n.Call.Args {
  102. ast.Walk(w, a) // check if arguments have a recursive call
  103. }
  104. return nil // recursive async call is not an issue
  105. case *ast.ForStmt:
  106. if n.Cond != nil {
  107. return nil
  108. }
  109. // unconditional loop
  110. return w
  111. }
  112. return w
  113. }
  114. func (w *lintUnconditionalRecursionRule) updateFuncStatus(node ast.Node) {
  115. if node == nil || w.currentFunc == nil || w.currentFunc.seenConditionalExit {
  116. return
  117. }
  118. w.currentFunc.seenConditionalExit = w.hasControlExit(node)
  119. }
  120. var exitFunctions = map[string]map[string]bool{
  121. "os": map[string]bool{"Exit": true},
  122. "syscall": map[string]bool{"Exit": true},
  123. "log": map[string]bool{
  124. "Fatal": true,
  125. "Fatalf": true,
  126. "Fatalln": true,
  127. "Panic": true,
  128. "Panicf": true,
  129. "Panicln": true,
  130. },
  131. }
  132. func (w *lintUnconditionalRecursionRule) hasControlExit(node ast.Node) bool {
  133. // isExit returns true if the given node makes control exit the function
  134. isExit := func(node ast.Node) bool {
  135. switch n := node.(type) {
  136. case *ast.ReturnStmt:
  137. return true
  138. case *ast.CallExpr:
  139. if isIdent(n.Fun, "panic") {
  140. return true
  141. }
  142. se, ok := n.Fun.(*ast.SelectorExpr)
  143. if !ok {
  144. return false
  145. }
  146. id, ok := se.X.(*ast.Ident)
  147. if !ok {
  148. return false
  149. }
  150. fn := se.Sel.Name
  151. pkg := id.Name
  152. if exitFunctions[pkg] != nil && exitFunctions[pkg][fn] { // it's a call to an exit function
  153. return true
  154. }
  155. }
  156. return false
  157. }
  158. return len(pick(node, isExit, nil)) != 0
  159. }