doc.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Package stylecheck contains analyzes that enforce style rules.
  2. // Most of the recommendations made are universally agreed upon by the wider Go community.
  3. // Some analyzes, however, implement stricter rules that not everyone will agree with.
  4. // In the context of Staticcheck, these analyzes are not enabled by default.
  5. //
  6. // For the most part it is recommended to follow the advice given by the analyzers that are enabled by default,
  7. // but you may want to disable additional analyzes on a case by case basis.
  8. package stylecheck
  9. import "honnef.co/go/tools/analysis/lint"
  10. var Docs = lint.Markdownify(map[string]*lint.RawDocumentation{
  11. "ST1000": {
  12. Title: `Incorrect or missing package comment`,
  13. Text: `Packages must have a package comment that is formatted according to
  14. the guidelines laid out in
  15. https://github.com/golang/go/wiki/CodeReviewComments#package-comments.`,
  16. Since: "2019.1",
  17. NonDefault: true,
  18. MergeIf: lint.MergeIfAny,
  19. },
  20. "ST1001": {
  21. Title: `Dot imports are discouraged`,
  22. Text: `Dot imports that aren't in external test packages are discouraged.
  23. The \'dot_import_whitelist\' option can be used to whitelist certain
  24. imports.
  25. Quoting Go Code Review Comments:
  26. > The \'import .\' form can be useful in tests that, due to circular
  27. > dependencies, cannot be made part of the package being tested:
  28. >
  29. > package foo_test
  30. >
  31. > import (
  32. > "bar/testutil" // also imports "foo"
  33. > . "foo"
  34. > )
  35. >
  36. > In this case, the test file cannot be in package foo because it
  37. > uses \'bar/testutil\', which imports \'foo\'. So we use the \'import .\'
  38. > form to let the file pretend to be part of package foo even though
  39. > it is not. Except for this one case, do not use \'import .\' in your
  40. > programs. It makes the programs much harder to read because it is
  41. > unclear whether a name like \'Quux\' is a top-level identifier in the
  42. > current package or in an imported package.`,
  43. Since: "2019.1",
  44. Options: []string{"dot_import_whitelist"},
  45. MergeIf: lint.MergeIfAny,
  46. },
  47. "ST1003": {
  48. Title: `Poorly chosen identifier`,
  49. Text: `Identifiers, such as variable and package names, follow certain rules.
  50. See the following links for details:
  51. - https://golang.org/doc/effective_go.html#package-names
  52. - https://golang.org/doc/effective_go.html#mixed-caps
  53. - https://github.com/golang/go/wiki/CodeReviewComments#initialisms
  54. - https://github.com/golang/go/wiki/CodeReviewComments#variable-names`,
  55. Since: "2019.1",
  56. NonDefault: true,
  57. Options: []string{"initialisms"},
  58. MergeIf: lint.MergeIfAny,
  59. },
  60. "ST1005": {
  61. Title: `Incorrectly formatted error string`,
  62. Text: `Error strings follow a set of guidelines to ensure uniformity and good
  63. composability.
  64. Quoting Go Code Review Comments:
  65. > Error strings should not be capitalized (unless beginning with
  66. > proper nouns or acronyms) or end with punctuation, since they are
  67. > usually printed following other context. That is, use
  68. > \'fmt.Errorf("something bad")\' not \'fmt.Errorf("Something bad")\', so
  69. > that \'log.Printf("Reading %s: %v", filename, err)\' formats without a
  70. > spurious capital letter mid-message.`,
  71. Since: "2019.1",
  72. MergeIf: lint.MergeIfAny,
  73. },
  74. "ST1006": {
  75. Title: `Poorly chosen receiver name`,
  76. Text: `Quoting Go Code Review Comments:
  77. > The name of a method's receiver should be a reflection of its
  78. > identity; often a one or two letter abbreviation of its type
  79. > suffices (such as "c" or "cl" for "Client"). Don't use generic
  80. > names such as "me", "this" or "self", identifiers typical of
  81. > object-oriented languages that place more emphasis on methods as
  82. > opposed to functions. The name need not be as descriptive as that
  83. > of a method argument, as its role is obvious and serves no
  84. > documentary purpose. It can be very short as it will appear on
  85. > almost every line of every method of the type; familiarity admits
  86. > brevity. Be consistent, too: if you call the receiver "c" in one
  87. > method, don't call it "cl" in another.`,
  88. Since: "2019.1",
  89. MergeIf: lint.MergeIfAny,
  90. },
  91. "ST1008": {
  92. Title: `A function's error value should be its last return value`,
  93. Text: `A function's error value should be its last return value.`,
  94. Since: `2019.1`,
  95. MergeIf: lint.MergeIfAny,
  96. },
  97. "ST1011": {
  98. Title: `Poorly chosen name for variable of type \'time.Duration\'`,
  99. Text: `\'time.Duration\' values represent an amount of time, which is represented
  100. as a count of nanoseconds. An expression like \'5 * time.Microsecond\'
  101. yields the value \'5000\'. It is therefore not appropriate to suffix a
  102. variable of type \'time.Duration\' with any time unit, such as \'Msec\' or
  103. \'Milli\'.`,
  104. Since: `2019.1`,
  105. MergeIf: lint.MergeIfAny,
  106. },
  107. "ST1012": {
  108. Title: `Poorly chosen name for error variable`,
  109. Text: `Error variables that are part of an API should be called \'errFoo\' or
  110. \'ErrFoo\'.`,
  111. Since: "2019.1",
  112. MergeIf: lint.MergeIfAny,
  113. },
  114. "ST1013": {
  115. Title: `Should use constants for HTTP error codes, not magic numbers`,
  116. Text: `HTTP has a tremendous number of status codes. While some of those are
  117. well known (200, 400, 404, 500), most of them are not. The \'net/http\'
  118. package provides constants for all status codes that are part of the
  119. various specifications. It is recommended to use these constants
  120. instead of hard-coding magic numbers, to vastly improve the
  121. readability of your code.`,
  122. Since: "2019.1",
  123. Options: []string{"http_status_code_whitelist"},
  124. MergeIf: lint.MergeIfAny,
  125. },
  126. "ST1015": {
  127. Title: `A switch's default case should be the first or last case`,
  128. Since: "2019.1",
  129. MergeIf: lint.MergeIfAny,
  130. },
  131. "ST1016": {
  132. Title: `Use consistent method receiver names`,
  133. Since: "2019.1",
  134. NonDefault: true,
  135. MergeIf: lint.MergeIfAny,
  136. },
  137. "ST1017": {
  138. Title: `Don't use Yoda conditions`,
  139. Text: `Yoda conditions are conditions of the kind \"if 42 == x\", where the
  140. literal is on the left side of the comparison. These are a common
  141. idiom in languages in which assignment is an expression, to avoid bugs
  142. of the kind \"if (x = 42)\". In Go, which doesn't allow for this kind of
  143. bug, we prefer the more idiomatic \"if x == 42\".`,
  144. Since: "2019.2",
  145. MergeIf: lint.MergeIfAny,
  146. },
  147. "ST1018": {
  148. Title: `Avoid zero-width and control characters in string literals`,
  149. Since: "2019.2",
  150. MergeIf: lint.MergeIfAny,
  151. },
  152. "ST1019": {
  153. Title: `Importing the same package multiple times`,
  154. Text: `Go allows importing the same package multiple times, as long as
  155. different import aliases are being used. That is, the following
  156. bit of code is valid:
  157. import (
  158. "fmt"
  159. fumpt "fmt"
  160. format "fmt"
  161. _ "fmt"
  162. )
  163. However, this is very rarely done on purpose. Usually, it is a
  164. sign of code that got refactored, accidentally adding duplicate
  165. import statements. It is also a rarely known feature, which may
  166. contribute to confusion.
  167. Do note that sometimes, this feature may be used
  168. intentionally (see for example
  169. https://github.com/golang/go/commit/3409ce39bfd7584523b7a8c150a310cea92d879d)
  170. – if you want to allow this pattern in your code base, you're
  171. advised to disable this check.`,
  172. Since: "2020.1",
  173. MergeIf: lint.MergeIfAny,
  174. },
  175. "ST1020": {
  176. Title: "The documentation of an exported function should start with the function's name",
  177. Text: `Doc comments work best as complete sentences, which
  178. allow a wide variety of automated presentations. The first sentence
  179. should be a one-sentence summary that starts with the name being
  180. declared.
  181. If every doc comment begins with the name of the item it describes,
  182. you can use the \'doc\' subcommand of the \'go\' tool and run the output
  183. through grep.
  184. See https://golang.org/doc/effective_go.html#commentary for more
  185. information on how to write good documentation.`,
  186. Since: "2020.1",
  187. NonDefault: true,
  188. MergeIf: lint.MergeIfAny,
  189. },
  190. "ST1021": {
  191. Title: "The documentation of an exported type should start with type's name",
  192. Text: `Doc comments work best as complete sentences, which
  193. allow a wide variety of automated presentations. The first sentence
  194. should be a one-sentence summary that starts with the name being
  195. declared.
  196. If every doc comment begins with the name of the item it describes,
  197. you can use the \'doc\' subcommand of the \'go\' tool and run the output
  198. through grep.
  199. See https://golang.org/doc/effective_go.html#commentary for more
  200. information on how to write good documentation.`,
  201. Since: "2020.1",
  202. NonDefault: true,
  203. MergeIf: lint.MergeIfAny,
  204. },
  205. "ST1022": {
  206. Title: "The documentation of an exported variable or constant should start with variable's name",
  207. Text: `Doc comments work best as complete sentences, which
  208. allow a wide variety of automated presentations. The first sentence
  209. should be a one-sentence summary that starts with the name being
  210. declared.
  211. If every doc comment begins with the name of the item it describes,
  212. you can use the \'doc\' subcommand of the \'go\' tool and run the output
  213. through grep.
  214. See https://golang.org/doc/effective_go.html#commentary for more
  215. information on how to write good documentation.`,
  216. Since: "2020.1",
  217. NonDefault: true,
  218. MergeIf: lint.MergeIfAny,
  219. },
  220. "ST1023": {
  221. Title: "Redundant type in variable declaration",
  222. Since: "2021.1",
  223. NonDefault: true,
  224. MergeIf: lint.MergeIfAll,
  225. },
  226. })