doc.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package simple
  2. import "honnef.co/go/tools/lint"
  3. var Docs = map[string]*lint.Documentation{
  4. "S1000": &lint.Documentation{
  5. Title: `Use plain channel send or receive instead of single-case select`,
  6. Text: `Select statements with a single case can be replaced with a simple
  7. send or receive.
  8. Before:
  9. select {
  10. case x := <-ch:
  11. fmt.Println(x)
  12. }
  13. After:
  14. x := <-ch
  15. fmt.Println(x)`,
  16. Since: "2017.1",
  17. },
  18. "S1001": &lint.Documentation{
  19. Title: `Replace for loop with call to copy`,
  20. Text: `Use copy() for copying elements from one slice to another.
  21. Before:
  22. for i, x := range src {
  23. dst[i] = x
  24. }
  25. After:
  26. copy(dst, src)`,
  27. Since: "2017.1",
  28. },
  29. "S1002": &lint.Documentation{
  30. Title: `Omit comparison with boolean constant`,
  31. Text: `Before:
  32. if x == true {}
  33. After:
  34. if x {}`,
  35. Since: "2017.1",
  36. },
  37. "S1003": &lint.Documentation{
  38. Title: `Replace call to strings.Index with strings.Contains`,
  39. Text: `Before:
  40. if strings.Index(x, y) != -1 {}
  41. After:
  42. if strings.Contains(x, y) {}`,
  43. Since: "2017.1",
  44. },
  45. "S1004": &lint.Documentation{
  46. Title: `Replace call to bytes.Compare with bytes.Equal`,
  47. Text: `Before:
  48. if bytes.Compare(x, y) == 0 {}
  49. After:
  50. if bytes.Equal(x, y) {}`,
  51. Since: "2017.1",
  52. },
  53. "S1005": &lint.Documentation{
  54. Title: `Drop unnecessary use of the blank identifier`,
  55. Text: `In many cases, assigning to the blank identifier is unnecessary.
  56. Before:
  57. for _ = range s {}
  58. x, _ = someMap[key]
  59. _ = <-ch
  60. After:
  61. for range s{}
  62. x = someMap[key]
  63. <-ch`,
  64. Since: "2017.1",
  65. },
  66. "S1006": &lint.Documentation{
  67. Title: `Use for { ... } for infinite loops`,
  68. Text: `For infinite loops, using for { ... } is the most idiomatic choice.`,
  69. Since: "2017.1",
  70. },
  71. "S1007": &lint.Documentation{
  72. Title: `Simplify regular expression by using raw string literal`,
  73. Text: `Raw string literals use ` + "`" + ` instead of " and do not support
  74. any escape sequences. This means that the backslash (\) can be used
  75. freely, without the need of escaping.
  76. Since regular expressions have their own escape sequences, raw strings
  77. can improve their readability.
  78. Before:
  79. regexp.Compile("\\A(\\w+) profile: total \\d+\\n\\z")
  80. After:
  81. regexp.Compile(` + "`" + `\A(\w+) profile: total \d+\n\z` + "`" + `)`,
  82. Since: "2017.1",
  83. },
  84. "S1008": &lint.Documentation{
  85. Title: `Simplify returning boolean expression`,
  86. Text: `Before:
  87. if <expr> {
  88. return true
  89. }
  90. return false
  91. After:
  92. return <expr>`,
  93. Since: "2017.1",
  94. },
  95. "S1009": &lint.Documentation{
  96. Title: `Omit redundant nil check on slices`,
  97. Text: `The len function is defined for all slices, even nil ones, which have
  98. a length of zero. It is not necessary to check if a slice is not nil
  99. before checking that its length is not zero.
  100. Before:
  101. if x != nil && len(x) != 0 {}
  102. After:
  103. if len(x) != 0 {}`,
  104. Since: "2017.1",
  105. },
  106. "S1010": &lint.Documentation{
  107. Title: `Omit default slice index`,
  108. Text: `When slicing, the second index defaults to the length of the value,
  109. making s[n:len(s)] and s[n:] equivalent.`,
  110. Since: "2017.1",
  111. },
  112. "S1011": &lint.Documentation{
  113. Title: `Use a single append to concatenate two slices`,
  114. Text: `Before:
  115. for _, e := range y {
  116. x = append(x, e)
  117. }
  118. After:
  119. x = append(x, y...)`,
  120. Since: "2017.1",
  121. },
  122. "S1012": &lint.Documentation{
  123. Title: `Replace time.Now().Sub(x) with time.Since(x)`,
  124. Text: `The time.Since helper has the same effect as using time.Now().Sub(x)
  125. but is easier to read.
  126. Before:
  127. time.Now().Sub(x)
  128. After:
  129. time.Since(x)`,
  130. Since: "2017.1",
  131. },
  132. "S1016": &lint.Documentation{
  133. Title: `Use a type conversion instead of manually copying struct fields`,
  134. Text: `Two struct types with identical fields can be converted between each
  135. other. In older versions of Go, the fields had to have identical
  136. struct tags. Since Go 1.8, however, struct tags are ignored during
  137. conversions. It is thus not necessary to manually copy every field
  138. individually.
  139. Before:
  140. var x T1
  141. y := T2{
  142. Field1: x.Field1,
  143. Field2: x.Field2,
  144. }
  145. After:
  146. var x T1
  147. y := T2(x)`,
  148. Since: "2017.1",
  149. },
  150. "S1017": &lint.Documentation{
  151. Title: `Replace manual trimming with strings.TrimPrefix`,
  152. Text: `Instead of using strings.HasPrefix and manual slicing, use the
  153. strings.TrimPrefix function. If the string doesn't start with the
  154. prefix, the original string will be returned. Using strings.TrimPrefix
  155. reduces complexity, and avoids common bugs, such as off-by-one
  156. mistakes.
  157. Before:
  158. if strings.HasPrefix(str, prefix) {
  159. str = str[len(prefix):]
  160. }
  161. After:
  162. str = strings.TrimPrefix(str, prefix)`,
  163. Since: "2017.1",
  164. },
  165. "S1018": &lint.Documentation{
  166. Title: `Use copy for sliding elements`,
  167. Text: `copy() permits using the same source and destination slice, even with
  168. overlapping ranges. This makes it ideal for sliding elements in a
  169. slice.
  170. Before:
  171. for i := 0; i < n; i++ {
  172. bs[i] = bs[offset+i]
  173. }
  174. After:
  175. copy(bs[:n], bs[offset:])`,
  176. Since: "2017.1",
  177. },
  178. "S1019": &lint.Documentation{
  179. Title: `Simplify make call by omitting redundant arguments`,
  180. Text: `The make function has default values for the length and capacity
  181. arguments. For channels and maps, the length defaults to zero.
  182. Additionally, for slices the capacity defaults to the length.`,
  183. Since: "2017.1",
  184. },
  185. "S1020": &lint.Documentation{
  186. Title: `Omit redundant nil check in type assertion`,
  187. Text: `Before:
  188. if _, ok := i.(T); ok && i != nil {}
  189. After:
  190. if _, ok := i.(T); ok {}`,
  191. Since: "2017.1",
  192. },
  193. "S1021": &lint.Documentation{
  194. Title: `Merge variable declaration and assignment`,
  195. Text: `Before:
  196. var x uint
  197. x = 1
  198. After:
  199. var x uint = 1`,
  200. Since: "2017.1",
  201. },
  202. "S1023": &lint.Documentation{
  203. Title: `Omit redundant control flow`,
  204. Text: `Functions that have no return value do not need a return statement as
  205. the final statement of the function.
  206. Switches in Go do not have automatic fallthrough, unlike languages
  207. like C. It is not necessary to have a break statement as the final
  208. statement in a case block.`,
  209. Since: "2017.1",
  210. },
  211. "S1024": &lint.Documentation{
  212. Title: `Replace x.Sub(time.Now()) with time.Until(x)`,
  213. Text: `The time.Until helper has the same effect as using x.Sub(time.Now())
  214. but is easier to read.
  215. Before:
  216. x.Sub(time.Now())
  217. After:
  218. time.Until(x)`,
  219. Since: "2017.1",
  220. },
  221. "S1025": &lint.Documentation{
  222. Title: `Don't use fmt.Sprintf("%s", x) unnecessarily`,
  223. Text: `In many instances, there are easier and more efficient ways of getting
  224. a value's string representation. Whenever a value's underlying type is
  225. a string already, or the type has a String method, they should be used
  226. directly.
  227. Given the following shared definitions
  228. type T1 string
  229. type T2 int
  230. func (T2) String() string { return "Hello, world" }
  231. var x string
  232. var y T1
  233. var z T2
  234. we can simplify the following
  235. fmt.Sprintf("%s", x)
  236. fmt.Sprintf("%s", y)
  237. fmt.Sprintf("%s", z)
  238. to
  239. x
  240. string(y)
  241. z.String()`,
  242. Since: "2017.1",
  243. },
  244. "S1028": &lint.Documentation{
  245. Title: `Simplify error construction with fmt.Errorf`,
  246. Text: `Before:
  247. errors.New(fmt.Sprintf(...))
  248. After:
  249. fmt.Errorf(...)`,
  250. Since: "2017.1",
  251. },
  252. "S1029": &lint.Documentation{
  253. Title: `Range over the string directly`,
  254. Text: `Ranging over a string will yield byte offsets and runes. If the offset
  255. isn't used, this is functionally equivalent to converting the string
  256. to a slice of runes and ranging over that. Ranging directly over the
  257. string will be more performant, however, as it avoids allocating a new
  258. slice, the size of which depends on the length of the string.
  259. Before:
  260. for _, r := range []rune(s) {}
  261. After:
  262. for _, r := range s {}`,
  263. Since: "2017.1",
  264. },
  265. "S1030": &lint.Documentation{
  266. Title: `Use bytes.Buffer.String or bytes.Buffer.Bytes`,
  267. Text: `bytes.Buffer has both a String and a Bytes method. It is never
  268. necessary to use string(buf.Bytes()) or []byte(buf.String()) – simply
  269. use the other method.`,
  270. Since: "2017.1",
  271. },
  272. "S1031": &lint.Documentation{
  273. Title: `Omit redundant nil check around loop`,
  274. Text: `You can use range on nil slices and maps, the loop will simply never
  275. execute. This makes an additional nil check around the loop
  276. unnecessary.
  277. Before:
  278. if s != nil {
  279. for _, x := range s {
  280. ...
  281. }
  282. }
  283. After:
  284. for _, x := range s {
  285. ...
  286. }`,
  287. Since: "2017.1",
  288. },
  289. "S1032": &lint.Documentation{
  290. Title: `Use sort.Ints(x), sort.Float64s(x), and sort.Strings(x)`,
  291. Text: `The sort.Ints, sort.Float64s and sort.Strings functions are easier to
  292. read than sort.Sort(sort.IntSlice(x)), sort.Sort(sort.Float64Slice(x))
  293. and sort.Sort(sort.StringSlice(x)).
  294. Before:
  295. sort.Sort(sort.StringSlice(x))
  296. After:
  297. sort.Strings(x)`,
  298. Since: "2019.1",
  299. },
  300. "S1033": &lint.Documentation{
  301. Title: `Unnecessary guard around call to delete`,
  302. Text: `Calling delete on a nil map is a no-op.`,
  303. Since: "2019.2",
  304. },
  305. "S1034": &lint.Documentation{
  306. Title: `Use result of type assertion to simplify cases`,
  307. Since: "2019.2",
  308. },
  309. }