rules.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package gorules
  17. import (
  18. "github.com/quasilyte/go-ruleguard/dsl"
  19. )
  20. // This is a collection of rules for ruleguard: https://github.com/quasilyte/go-ruleguard
  21. // Remove extra conversions: mdempsky/unconvert
  22. func unconvert(m dsl.Matcher) {
  23. m.Match("int($x)").Where(m["x"].Type.Is("int") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  24. m.Match("float32($x)").Where(m["x"].Type.Is("float32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  25. m.Match("float64($x)").Where(m["x"].Type.Is("float64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  26. // m.Match("byte($x)").Where(m["x"].Type.Is("byte")).Report("unnecessary conversion").Suggest("$x")
  27. // m.Match("rune($x)").Where(m["x"].Type.Is("rune")).Report("unnecessary conversion").Suggest("$x")
  28. m.Match("bool($x)").Where(m["x"].Type.Is("bool") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  29. m.Match("int8($x)").Where(m["x"].Type.Is("int8") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  30. m.Match("int16($x)").Where(m["x"].Type.Is("int16") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  31. m.Match("int32($x)").Where(m["x"].Type.Is("int32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  32. m.Match("int64($x)").Where(m["x"].Type.Is("int64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  33. m.Match("uint8($x)").Where(m["x"].Type.Is("uint8") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  34. m.Match("uint16($x)").Where(m["x"].Type.Is("uint16") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  35. m.Match("uint32($x)").Where(m["x"].Type.Is("uint32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  36. m.Match("uint64($x)").Where(m["x"].Type.Is("uint64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
  37. m.Match("time.Duration($x)").Where(m["x"].Type.Is("time.Duration") && !m["x"].Text.Matches("^[0-9]*$")).Report("unnecessary conversion").Suggest("$x")
  38. }
  39. // Don't use == or != with time.Time
  40. // https://github.com/dominikh/go-tools/issues/47 : Wontfix
  41. func timeeq(m dsl.Matcher) {
  42. m.Match("$t0 == $t1").Where(m["t0"].Type.Is("time.Time")).Report("using == with time.Time")
  43. m.Match("$t0 != $t1").Where(m["t0"].Type.Is("time.Time")).Report("using != with time.Time")
  44. m.Match(`map[$k]$v`).Where(m["k"].Type.Is("time.Time")).Report("map with time.Time keys are easy to misuse")
  45. }
  46. // err but no an error
  47. func errnoterror(m dsl.Matcher) {
  48. // Would be easier to check for all err identifiers instead, but then how do we get the type from m[] ?
  49. m.Match(
  50. "if $*_, err := $x; $err != nil { $*_ } else if $_ { $*_ }",
  51. "if $*_, err := $x; $err != nil { $*_ } else { $*_ }",
  52. "if $*_, err := $x; $err != nil { $*_ }",
  53. "if $*_, err = $x; $err != nil { $*_ } else if $_ { $*_ }",
  54. "if $*_, err = $x; $err != nil { $*_ } else { $*_ }",
  55. "if $*_, err = $x; $err != nil { $*_ }",
  56. "$*_, err := $x; if $err != nil { $*_ } else if $_ { $*_ }",
  57. "$*_, err := $x; if $err != nil { $*_ } else { $*_ }",
  58. "$*_, err := $x; if $err != nil { $*_ }",
  59. "$*_, err = $x; if $err != nil { $*_ } else if $_ { $*_ }",
  60. "$*_, err = $x; if $err != nil { $*_ } else { $*_ }",
  61. "$*_, err = $x; if $err != nil { $*_ }",
  62. ).
  63. Where(m["err"].Text == "err" && !m["err"].Type.Is("error") && m["x"].Text != "recover()").
  64. Report("err variable not error type")
  65. }
  66. // Identical if and else bodies
  67. func ifbodythenbody(m dsl.Matcher) {
  68. m.Match("if $*_ { $body } else { $body }").
  69. Report("identical if and else bodies")
  70. // Lots of false positives.
  71. // m.Match("if $*_ { $body } else if $*_ { $body }").
  72. // Report("identical if and else bodies")
  73. }
  74. // Odd inequality: A - B < 0 instead of !=
  75. // Too many false positives.
  76. /*
  77. func subtractnoteq(m dsl.Matcher) {
  78. m.Match("$a - $b < 0").Report("consider $a != $b")
  79. m.Match("$a - $b > 0").Report("consider $a != $b")
  80. m.Match("0 < $a - $b").Report("consider $a != $b")
  81. m.Match("0 > $a - $b").Report("consider $a != $b")
  82. }
  83. */
  84. // Self-assignment
  85. func selfassign(m dsl.Matcher) {
  86. m.Match("$x = $x").Report("useless self-assignment")
  87. }
  88. // Odd nested ifs
  89. func oddnestedif(m dsl.Matcher) {
  90. m.Match("if $x { if $x { $*_ }; $*_ }",
  91. "if $x == $y { if $x != $y {$*_ }; $*_ }",
  92. "if $x != $y { if $x == $y {$*_ }; $*_ }",
  93. "if $x { if !$x { $*_ }; $*_ }",
  94. "if !$x { if $x { $*_ }; $*_ }").
  95. Report("odd nested ifs")
  96. m.Match("for $x { if $x { $*_ }; $*_ }",
  97. "for $x == $y { if $x != $y {$*_ }; $*_ }",
  98. "for $x != $y { if $x == $y {$*_ }; $*_ }",
  99. "for $x { if !$x { $*_ }; $*_ }",
  100. "for !$x { if $x { $*_ }; $*_ }").
  101. Report("odd nested for/ifs")
  102. }
  103. // odd bitwise expressions
  104. func oddbitwise(m dsl.Matcher) {
  105. m.Match("$x | $x",
  106. "$x | ^$x",
  107. "^$x | $x").
  108. Report("odd bitwise OR")
  109. m.Match("$x & $x",
  110. "$x & ^$x",
  111. "^$x & $x").
  112. Report("odd bitwise AND")
  113. m.Match("$x &^ $x").
  114. Report("odd bitwise AND-NOT")
  115. }
  116. // odd sequence of if tests with return
  117. func ifreturn(m dsl.Matcher) {
  118. m.Match("if $x { return $*_ }; if $x {$*_ }").Report("odd sequence of if test")
  119. m.Match("if $x { return $*_ }; if !$x {$*_ }").Report("odd sequence of if test")
  120. m.Match("if !$x { return $*_ }; if $x {$*_ }").Report("odd sequence of if test")
  121. m.Match("if $x == $y { return $*_ }; if $x != $y {$*_ }").Report("odd sequence of if test")
  122. m.Match("if $x != $y { return $*_ }; if $x == $y {$*_ }").Report("odd sequence of if test")
  123. }
  124. func oddifsequence(m dsl.Matcher) {
  125. /*
  126. m.Match("if $x { $*_ }; if $x {$*_ }").Report("odd sequence of if test")
  127. m.Match("if $x == $y { $*_ }; if $y == $x {$*_ }").Report("odd sequence of if tests")
  128. m.Match("if $x != $y { $*_ }; if $y != $x {$*_ }").Report("odd sequence of if tests")
  129. m.Match("if $x < $y { $*_ }; if $y > $x {$*_ }").Report("odd sequence of if tests")
  130. m.Match("if $x <= $y { $*_ }; if $y >= $x {$*_ }").Report("odd sequence of if tests")
  131. m.Match("if $x > $y { $*_ }; if $y < $x {$*_ }").Report("odd sequence of if tests")
  132. m.Match("if $x >= $y { $*_ }; if $y <= $x {$*_ }").Report("odd sequence of if tests")
  133. */
  134. }
  135. // odd sequence of nested if tests
  136. func nestedifsequence(m dsl.Matcher) {
  137. /*
  138. m.Match("if $x < $y { if $x >= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
  139. m.Match("if $x <= $y { if $x > $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
  140. m.Match("if $x > $y { if $x <= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
  141. m.Match("if $x >= $y { if $x < $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
  142. */
  143. }
  144. // odd sequence of assignments
  145. func identicalassignments(m dsl.Matcher) {
  146. m.Match("$x = $y; $y = $x").Report("odd sequence of assignments")
  147. }
  148. func oddcompoundop(m dsl.Matcher) {
  149. m.Match("$x += $x + $_",
  150. "$x += $x - $_").
  151. Report("odd += expression")
  152. m.Match("$x -= $x + $_",
  153. "$x -= $x - $_").
  154. Report("odd -= expression")
  155. }
  156. func constswitch(m dsl.Matcher) {
  157. m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
  158. Where(m["x"].Const && !m["x"].Text.Matches(`^runtime\.`)).
  159. Report("constant switch")
  160. }
  161. func oddcomparisons(m dsl.Matcher) {
  162. m.Match(
  163. "$x - $y == 0",
  164. "$x - $y != 0",
  165. "$x - $y < 0",
  166. "$x - $y <= 0",
  167. "$x - $y > 0",
  168. "$x - $y >= 0",
  169. "$x ^ $y == 0",
  170. "$x ^ $y != 0",
  171. ).Report("odd comparison")
  172. }
  173. func oddmathbits(m dsl.Matcher) {
  174. m.Match(
  175. "64 - bits.LeadingZeros64($x)",
  176. "32 - bits.LeadingZeros32($x)",
  177. "16 - bits.LeadingZeros16($x)",
  178. "8 - bits.LeadingZeros8($x)",
  179. ).Report("odd math/bits expression: use bits.Len*() instead?")
  180. }
  181. // func floateq(m dsl.Matcher) {
  182. // m.Match(
  183. // "$x == $y",
  184. // "$x != $y",
  185. // ).
  186. // Where(m["x"].Type.Is("float32") && !m["x"].Const && !m["y"].Text.Matches("0(.0+)?") && !m.File().Name.Matches("floating_comparision.go")).
  187. // Report("floating point tested for equality")
  188. // m.Match(
  189. // "$x == $y",
  190. // "$x != $y",
  191. // ).
  192. // Where(m["x"].Type.Is("float64") && !m["x"].Const && !m["y"].Text.Matches("0(.0+)?") && !m.File().Name.Matches("floating_comparision.go")).
  193. // Report("floating point tested for equality")
  194. // m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
  195. // Where(m["x"].Type.Is("float32")).
  196. // Report("floating point as switch expression")
  197. // m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
  198. // Where(m["x"].Type.Is("float64")).
  199. // Report("floating point as switch expression")
  200. // }
  201. func badexponent(m dsl.Matcher) {
  202. m.Match(
  203. "2 ^ $x",
  204. "10 ^ $x",
  205. ).
  206. Report("caret (^) is not exponentiation")
  207. }
  208. func floatloop(m dsl.Matcher) {
  209. m.Match(
  210. "for $i := $x; $i < $y; $i += $z { $*_ }",
  211. "for $i = $x; $i < $y; $i += $z { $*_ }",
  212. ).
  213. Where(m["i"].Type.Is("float64")).
  214. Report("floating point for loop counter")
  215. m.Match(
  216. "for $i := $x; $i < $y; $i += $z { $*_ }",
  217. "for $i = $x; $i < $y; $i += $z { $*_ }",
  218. ).
  219. Where(m["i"].Type.Is("float32")).
  220. Report("floating point for loop counter")
  221. }
  222. func urlredacted(m dsl.Matcher) {
  223. m.Match(
  224. "log.Println($x, $*_)",
  225. "log.Println($*_, $x, $*_)",
  226. "log.Println($*_, $x)",
  227. "log.Printf($*_, $x, $*_)",
  228. "log.Printf($*_, $x)",
  229. "log.Println($x, $*_)",
  230. "log.Println($*_, $x, $*_)",
  231. "log.Println($*_, $x)",
  232. "log.Printf($*_, $x, $*_)",
  233. "log.Printf($*_, $x)",
  234. ).
  235. Where(m["x"].Type.Is("*url.URL")).
  236. Report("consider $x.Redacted() when outputting URLs")
  237. }
  238. func sprinterr(m dsl.Matcher) {
  239. m.Match(`fmt.Sprint($err)`,
  240. `fmt.Sprintf("%s", $err)`,
  241. `fmt.Sprintf("%v", $err)`,
  242. ).
  243. Where(m["err"].Type.Is("error")).
  244. Report("maybe call $err.Error() instead of fmt.Sprint()?")
  245. }
  246. // disable this check, because it can not apply to generic type
  247. //func largeloopcopy(m dsl.Matcher) {
  248. // m.Match(
  249. // `for $_, $v := range $_ { $*_ }`,
  250. // ).
  251. // Where(m["v"].Type.Size > 1024).
  252. // Report(`loop copies large value each iteration`)
  253. //}
  254. func joinpath(m dsl.Matcher) {
  255. m.Match(
  256. `strings.Join($_, "/")`,
  257. `strings.Join($_, "\\")`,
  258. "strings.Join($_, `\\`)",
  259. ).
  260. Report(`did you mean path.Join() or filepath.Join() ?`)
  261. }
  262. func readfull(m dsl.Matcher) {
  263. m.Match(`$n, $err := io.ReadFull($_, $slice)
  264. if $err != nil || $n != len($slice) {
  265. $*_
  266. }`,
  267. `$n, $err := io.ReadFull($_, $slice)
  268. if $n != len($slice) || $err != nil {
  269. $*_
  270. }`,
  271. `$n, $err = io.ReadFull($_, $slice)
  272. if $err != nil || $n != len($slice) {
  273. $*_
  274. }`,
  275. `$n, $err = io.ReadFull($_, $slice)
  276. if $n != len($slice) || $err != nil {
  277. $*_
  278. }`,
  279. `if $n, $err := io.ReadFull($_, $slice); $n != len($slice) || $err != nil {
  280. $*_
  281. }`,
  282. `if $n, $err := io.ReadFull($_, $slice); $err != nil || $n != len($slice) {
  283. $*_
  284. }`,
  285. `if $n, $err = io.ReadFull($_, $slice); $n != len($slice) || $err != nil {
  286. $*_
  287. }`,
  288. `if $n, $err = io.ReadFull($_, $slice); $err != nil || $n != len($slice) {
  289. $*_
  290. }`,
  291. ).Report("io.ReadFull() returns err == nil iff n == len(slice)")
  292. }
  293. func nilerr(m dsl.Matcher) {
  294. m.Match(
  295. `if err == nil { return err }`,
  296. `if err == nil { return $*_, err }`,
  297. ).
  298. Report(`return nil error instead of nil value`)
  299. }
  300. func mailaddress(m dsl.Matcher) {
  301. m.Match(
  302. "fmt.Sprintf(`\"%s\" <%s>`, $NAME, $EMAIL)",
  303. "fmt.Sprintf(`\"%s\"<%s>`, $NAME, $EMAIL)",
  304. "fmt.Sprintf(`%s <%s>`, $NAME, $EMAIL)",
  305. "fmt.Sprintf(`%s<%s>`, $NAME, $EMAIL)",
  306. `fmt.Sprintf("\"%s\"<%s>", $NAME, $EMAIL)`,
  307. `fmt.Sprintf("\"%s\" <%s>", $NAME, $EMAIL)`,
  308. `fmt.Sprintf("%s<%s>", $NAME, $EMAIL)`,
  309. `fmt.Sprintf("%s <%s>", $NAME, $EMAIL)`,
  310. ).
  311. Report("use net/mail Address.String() instead of fmt.Sprintf()").
  312. Suggest("(&mail.Address{Name:$NAME, Address:$EMAIL}).String()")
  313. }
  314. func errnetclosed(m dsl.Matcher) {
  315. m.Match(
  316. `strings.Contains($err.Error(), $text)`,
  317. ).
  318. Where(m["text"].Text.Matches("\".*closed network connection.*\"")).
  319. Report(`String matching against error texts is fragile; use net.ErrClosed instead`).
  320. Suggest(`errors.Is($err, net.ErrClosed)`)
  321. }
  322. func httpheaderadd(m dsl.Matcher) {
  323. m.Match(
  324. `$H.Add($KEY, $VALUE)`,
  325. ).
  326. Where(m["H"].Type.Is("http.Header")).
  327. Report("use http.Header.Set method instead of Add to overwrite all existing header values").
  328. Suggest(`$H.Set($KEY, $VALUE)`)
  329. }
  330. func hmacnew(m dsl.Matcher) {
  331. m.Match("hmac.New(func() hash.Hash { return $x }, $_)",
  332. `$f := func() hash.Hash { return $x }
  333. $*_
  334. hmac.New($f, $_)`,
  335. ).Where(m["x"].Pure).
  336. Report("invalid hash passed to hmac.New()")
  337. }
  338. func writestring(m dsl.Matcher) {
  339. m.Match(`io.WriteString($w, string($b))`).
  340. Where(m["b"].Type.Is("[]byte")).
  341. Suggest("$w.Write($b)")
  342. }
  343. func badlock(m dsl.Matcher) {
  344. // Shouldn't give many false positives without type filter
  345. // as Lock+Unlock pairs in combination with defer gives us pretty
  346. // a good chance to guess correctly. If we constrain the type to sync.Mutex
  347. // then it'll be harder to match embedded locks and custom methods
  348. // that may forward the call to the sync.Mutex (or other synchronization primitive).
  349. m.Match(`$mu.Lock(); defer $mu.RUnlock()`).Report(`maybe $mu.RLock() was intended?`)
  350. m.Match(`$mu.RLock(); defer $mu.Unlock()`).Report(`maybe $mu.Lock() was intended?`)
  351. }