Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

error-strings.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package rule
  2. import (
  3. "go/ast"
  4. "go/token"
  5. "strconv"
  6. "unicode"
  7. "unicode/utf8"
  8. "github.com/mgechev/revive/lint"
  9. )
  10. // ErrorStringsRule lints given else constructs.
  11. type ErrorStringsRule struct{}
  12. // Apply applies the rule to given file.
  13. func (r *ErrorStringsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
  14. var failures []lint.Failure
  15. fileAst := file.AST
  16. walker := lintErrorStrings{
  17. file: file,
  18. fileAst: fileAst,
  19. onFailure: func(failure lint.Failure) {
  20. failures = append(failures, failure)
  21. },
  22. }
  23. ast.Walk(walker, fileAst)
  24. return failures
  25. }
  26. // Name returns the rule name.
  27. func (r *ErrorStringsRule) Name() string {
  28. return "error-strings"
  29. }
  30. type lintErrorStrings struct {
  31. file *lint.File
  32. fileAst *ast.File
  33. onFailure func(lint.Failure)
  34. }
  35. func (w lintErrorStrings) Visit(n ast.Node) ast.Visitor {
  36. ce, ok := n.(*ast.CallExpr)
  37. if !ok {
  38. return w
  39. }
  40. if !isPkgDot(ce.Fun, "errors", "New") && !isPkgDot(ce.Fun, "fmt", "Errorf") {
  41. return w
  42. }
  43. if len(ce.Args) < 1 {
  44. return w
  45. }
  46. str, ok := ce.Args[0].(*ast.BasicLit)
  47. if !ok || str.Kind != token.STRING {
  48. return w
  49. }
  50. s, _ := strconv.Unquote(str.Value) // can assume well-formed Go
  51. if s == "" {
  52. return w
  53. }
  54. clean, conf := lintErrorString(s)
  55. if clean {
  56. return w
  57. }
  58. w.onFailure(lint.Failure{
  59. Node: str,
  60. Confidence: conf,
  61. Category: "errors",
  62. Failure: "error strings should not be capitalized or end with punctuation or a newline",
  63. })
  64. return w
  65. }
  66. func lintErrorString(s string) (isClean bool, conf float64) {
  67. const basicConfidence = 0.8
  68. const capConfidence = basicConfidence - 0.2
  69. first, firstN := utf8.DecodeRuneInString(s)
  70. last, _ := utf8.DecodeLastRuneInString(s)
  71. if last == '.' || last == ':' || last == '!' || last == '\n' {
  72. return false, basicConfidence
  73. }
  74. if unicode.IsUpper(first) {
  75. // People use proper nouns and exported Go identifiers in error strings,
  76. // so decrease the confidence of warnings for capitalization.
  77. if len(s) <= firstN {
  78. return false, capConfidence
  79. }
  80. // Flag strings starting with something that doesn't look like an initialism.
  81. if second, _ := utf8.DecodeRuneInString(s[firstN:]); !unicode.IsUpper(second) {
  82. return false, capConfidence
  83. }
  84. }
  85. return true, 0
  86. }