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.

flag-param.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package rule
  2. import (
  3. "fmt"
  4. "github.com/mgechev/revive/lint"
  5. "go/ast"
  6. )
  7. // FlagParamRule lints given else constructs.
  8. type FlagParamRule struct{}
  9. // Apply applies the rule to given file.
  10. func (r *FlagParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
  11. var failures []lint.Failure
  12. onFailure := func(failure lint.Failure) {
  13. failures = append(failures, failure)
  14. }
  15. w := lintFlagParamRule{onFailure: onFailure}
  16. ast.Walk(w, file.AST)
  17. return failures
  18. }
  19. // Name returns the rule name.
  20. func (r *FlagParamRule) Name() string {
  21. return "flag-parameter"
  22. }
  23. type lintFlagParamRule struct {
  24. onFailure func(lint.Failure)
  25. }
  26. func (w lintFlagParamRule) Visit(node ast.Node) ast.Visitor {
  27. fd, ok := node.(*ast.FuncDecl)
  28. if !ok {
  29. return w
  30. }
  31. if fd.Body == nil {
  32. return nil // skip whole function declaration
  33. }
  34. for _, p := range fd.Type.Params.List {
  35. t := p.Type
  36. id, ok := t.(*ast.Ident)
  37. if !ok {
  38. continue
  39. }
  40. if id.Name != "bool" {
  41. continue
  42. }
  43. cv := conditionVisitor{p.Names, fd, w}
  44. ast.Walk(cv, fd.Body)
  45. }
  46. return w
  47. }
  48. type conditionVisitor struct {
  49. ids []*ast.Ident
  50. fd *ast.FuncDecl
  51. linter lintFlagParamRule
  52. }
  53. func (w conditionVisitor) Visit(node ast.Node) ast.Visitor {
  54. ifStmt, ok := node.(*ast.IfStmt)
  55. if !ok {
  56. return w
  57. }
  58. fselect := func(n ast.Node) bool {
  59. ident, ok := n.(*ast.Ident)
  60. if !ok {
  61. return false
  62. }
  63. for _, id := range w.ids {
  64. if ident.Name == id.Name {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. uses := pick(ifStmt.Cond, fselect, nil)
  71. if len(uses) < 1 {
  72. return w
  73. }
  74. w.linter.onFailure(lint.Failure{
  75. Confidence: 1,
  76. Node: w.fd.Type.Params,
  77. Category: "bad practice",
  78. Failure: fmt.Sprintf("parameter '%s' seems to be a control flag, avoid control coupling", uses[0]),
  79. })
  80. return nil
  81. }