doc.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. // Package staticcheck contains analyzes that find bugs and performance issues.
  2. // Barring the rare false positive, any code flagged by these analyzes needs to be fixed.
  3. package staticcheck
  4. import "honnef.co/go/tools/analysis/lint"
  5. var Docs = lint.Markdownify(map[string]*lint.RawDocumentation{
  6. "SA1000": {
  7. Title: `Invalid regular expression`,
  8. Since: "2017.1",
  9. Severity: lint.SeverityError,
  10. MergeIf: lint.MergeIfAny,
  11. },
  12. "SA1001": {
  13. Title: `Invalid template`,
  14. Since: "2017.1",
  15. Severity: lint.SeverityError,
  16. MergeIf: lint.MergeIfAny,
  17. },
  18. "SA1002": {
  19. Title: `Invalid format in \'time.Parse\'`,
  20. Since: "2017.1",
  21. Severity: lint.SeverityError,
  22. MergeIf: lint.MergeIfAny,
  23. },
  24. "SA1003": {
  25. Title: `Unsupported argument to functions in \'encoding/binary\'`,
  26. Text: `The \'encoding/binary\' package can only serialize types with known sizes.
  27. This precludes the use of the \'int\' and \'uint\' types, as their sizes
  28. differ on different architectures. Furthermore, it doesn't support
  29. serializing maps, channels, strings, or functions.
  30. Before Go 1.8, \'bool\' wasn't supported, either.`,
  31. Since: "2017.1",
  32. Severity: lint.SeverityError,
  33. MergeIf: lint.MergeIfAny,
  34. },
  35. "SA1004": {
  36. Title: `Suspiciously small untyped constant in \'time.Sleep\'`,
  37. Text: `The \'time\'.Sleep function takes a \'time.Duration\' as its only argument.
  38. Durations are expressed in nanoseconds. Thus, calling \'time.Sleep(1)\'
  39. will sleep for 1 nanosecond. This is a common source of bugs, as sleep
  40. functions in other languages often accept seconds or milliseconds.
  41. The \'time\' package provides constants such as \'time.Second\' to express
  42. large durations. These can be combined with arithmetic to express
  43. arbitrary durations, for example \'5 * time.Second\' for 5 seconds.
  44. If you truly meant to sleep for a tiny amount of time, use
  45. \'n * time.Nanosecond\' to signal to Staticcheck that you did mean to sleep
  46. for some amount of nanoseconds.`,
  47. Since: "2017.1",
  48. Severity: lint.SeverityWarning,
  49. MergeIf: lint.MergeIfAny,
  50. },
  51. "SA1005": {
  52. Title: `Invalid first argument to \'exec.Command\'`,
  53. Text: `\'os/exec\' runs programs directly (using variants of the fork and exec
  54. system calls on Unix systems). This shouldn't be confused with running
  55. a command in a shell. The shell will allow for features such as input
  56. redirection, pipes, and general scripting. The shell is also
  57. responsible for splitting the user's input into a program name and its
  58. arguments. For example, the equivalent to
  59. ls / /tmp
  60. would be
  61. exec.Command("ls", "/", "/tmp")
  62. If you want to run a command in a shell, consider using something like
  63. the following – but be aware that not all systems, particularly
  64. Windows, will have a \'/bin/sh\' program:
  65. exec.Command("/bin/sh", "-c", "ls | grep Awesome")`,
  66. Since: "2017.1",
  67. Severity: lint.SeverityWarning,
  68. MergeIf: lint.MergeIfAny,
  69. },
  70. "SA1006": {
  71. Title: `\'Printf\' with dynamic first argument and no further arguments`,
  72. Text: `Using \'fmt.Printf\' with a dynamic first argument can lead to unexpected
  73. output. The first argument is a format string, where certain character
  74. combinations have special meaning. If, for example, a user were to
  75. enter a string such as
  76. Interest rate: 5%
  77. and you printed it with
  78. fmt.Printf(s)
  79. it would lead to the following output:
  80. Interest rate: 5%!(NOVERB).
  81. Similarly, forming the first parameter via string concatenation with
  82. user input should be avoided for the same reason. When printing user
  83. input, either use a variant of \'fmt.Print\', or use the \'%s\' Printf verb
  84. and pass the string as an argument.`,
  85. Since: "2017.1",
  86. Severity: lint.SeverityWarning,
  87. MergeIf: lint.MergeIfAny,
  88. },
  89. "SA1007": {
  90. Title: `Invalid URL in \'net/url.Parse\'`,
  91. Since: "2017.1",
  92. Severity: lint.SeverityError,
  93. MergeIf: lint.MergeIfAny,
  94. },
  95. "SA1008": {
  96. Title: `Non-canonical key in \'http.Header\' map`,
  97. Text: `Keys in \'http.Header\' maps are canonical, meaning they follow a specific
  98. combination of uppercase and lowercase letters. Methods such as
  99. \'http.Header.Add\' and \'http.Header.Del\' convert inputs into this canonical
  100. form before manipulating the map.
  101. When manipulating \'http.Header\' maps directly, as opposed to using the
  102. provided methods, care should be taken to stick to canonical form in
  103. order to avoid inconsistencies. The following piece of code
  104. demonstrates one such inconsistency:
  105. h := http.Header{}
  106. h["etag"] = []string{"1234"}
  107. h.Add("etag", "5678")
  108. fmt.Println(h)
  109. // Output:
  110. // map[Etag:[5678] etag:[1234]]
  111. The easiest way of obtaining the canonical form of a key is to use
  112. \'http.CanonicalHeaderKey\'.`,
  113. Since: "2017.1",
  114. Severity: lint.SeverityWarning,
  115. MergeIf: lint.MergeIfAny,
  116. },
  117. "SA1010": {
  118. Title: `\'(*regexp.Regexp).FindAll\' called with \'n == 0\', which will always return zero results`,
  119. Text: `If \'n >= 0\', the function returns at most \'n\' matches/submatches. To
  120. return all results, specify a negative number.`,
  121. Since: "2017.1",
  122. Severity: lint.SeverityWarning,
  123. MergeIf: lint.MergeIfAny, // MergeIfAny if we only flag literals, not named constants
  124. },
  125. "SA1011": {
  126. Title: `Various methods in the \"strings\" package expect valid UTF-8, but invalid input is provided`,
  127. Since: "2017.1",
  128. Severity: lint.SeverityError,
  129. MergeIf: lint.MergeIfAny,
  130. },
  131. "SA1012": {
  132. Title: `A nil \'context.Context\' is being passed to a function, consider using \'context.TODO\' instead`,
  133. Since: "2017.1",
  134. Severity: lint.SeverityWarning,
  135. MergeIf: lint.MergeIfAny,
  136. },
  137. "SA1013": {
  138. Title: `\'io.Seeker.Seek\' is being called with the whence constant as the first argument, but it should be the second`,
  139. Since: "2017.1",
  140. Severity: lint.SeverityWarning,
  141. MergeIf: lint.MergeIfAny,
  142. },
  143. "SA1014": {
  144. Title: `Non-pointer value passed to \'Unmarshal\' or \'Decode\'`,
  145. Since: "2017.1",
  146. Severity: lint.SeverityError,
  147. MergeIf: lint.MergeIfAny,
  148. },
  149. "SA1015": {
  150. Title: `Using \'time.Tick\' in a way that will leak. Consider using \'time.NewTicker\', and only use \'time.Tick\' in tests, commands and endless functions`,
  151. Since: "2017.1",
  152. Severity: lint.SeverityWarning,
  153. MergeIf: lint.MergeIfAny,
  154. },
  155. "SA1016": {
  156. Title: `Trapping a signal that cannot be trapped`,
  157. Text: `Not all signals can be intercepted by a process. Specifically, on
  158. UNIX-like systems, the \'syscall.SIGKILL\' and \'syscall.SIGSTOP\' signals are
  159. never passed to the process, but instead handled directly by the
  160. kernel. It is therefore pointless to try and handle these signals.`,
  161. Since: "2017.1",
  162. Severity: lint.SeverityWarning,
  163. MergeIf: lint.MergeIfAny,
  164. },
  165. "SA1017": {
  166. Title: `Channels used with \'os/signal.Notify\' should be buffered`,
  167. Text: `The \'os/signal\' package uses non-blocking channel sends when delivering
  168. signals. If the receiving end of the channel isn't ready and the
  169. channel is either unbuffered or full, the signal will be dropped. To
  170. avoid missing signals, the channel should be buffered and of the
  171. appropriate size. For a channel used for notification of just one
  172. signal value, a buffer of size 1 is sufficient.`,
  173. Since: "2017.1",
  174. Severity: lint.SeverityWarning,
  175. MergeIf: lint.MergeIfAny,
  176. },
  177. "SA1018": {
  178. Title: `\'strings.Replace\' called with \'n == 0\', which does nothing`,
  179. Text: `With \'n == 0\', zero instances will be replaced. To replace all
  180. instances, use a negative number, or use \'strings.ReplaceAll\'.`,
  181. Since: "2017.1",
  182. Severity: lint.SeverityWarning,
  183. MergeIf: lint.MergeIfAny, // MergeIfAny if we only flag literals, not named constants
  184. },
  185. "SA1019": {
  186. Title: `Using a deprecated function, variable, constant or field`,
  187. Since: "2017.1",
  188. Severity: lint.SeverityDeprecated,
  189. MergeIf: lint.MergeIfAny,
  190. },
  191. "SA1020": {
  192. Title: `Using an invalid host:port pair with a \'net.Listen\'-related function`,
  193. Since: "2017.1",
  194. Severity: lint.SeverityError,
  195. MergeIf: lint.MergeIfAny,
  196. },
  197. "SA1021": {
  198. Title: `Using \'bytes.Equal\' to compare two \'net.IP\'`,
  199. Text: `A \'net.IP\' stores an IPv4 or IPv6 address as a slice of bytes. The
  200. length of the slice for an IPv4 address, however, can be either 4 or
  201. 16 bytes long, using different ways of representing IPv4 addresses. In
  202. order to correctly compare two \'net.IP\'s, the \'net.IP.Equal\' method should
  203. be used, as it takes both representations into account.`,
  204. Since: "2017.1",
  205. Severity: lint.SeverityWarning,
  206. MergeIf: lint.MergeIfAny,
  207. },
  208. "SA1023": {
  209. Title: `Modifying the buffer in an \'io.Writer\' implementation`,
  210. Text: `\'Write\' must not modify the slice data, even temporarily.`,
  211. Since: "2017.1",
  212. Severity: lint.SeverityError,
  213. MergeIf: lint.MergeIfAny,
  214. },
  215. "SA1024": {
  216. Title: `A string cutset contains duplicate characters`,
  217. Text: `The \'strings.TrimLeft\' and \'strings.TrimRight\' functions take cutsets, not
  218. prefixes. A cutset is treated as a set of characters to remove from a
  219. string. For example,
  220. strings.TrimLeft("42133word", "1234")
  221. will result in the string \'"word"\' – any characters that are 1, 2, 3 or
  222. 4 are cut from the left of the string.
  223. In order to remove one string from another, use \'strings.TrimPrefix\' instead.`,
  224. Since: "2017.1",
  225. Severity: lint.SeverityWarning,
  226. MergeIf: lint.MergeIfAny,
  227. },
  228. "SA1025": {
  229. Title: `It is not possible to use \'(*time.Timer).Reset\''s return value correctly`,
  230. Since: "2019.1",
  231. Severity: lint.SeverityWarning,
  232. MergeIf: lint.MergeIfAny,
  233. },
  234. "SA1026": {
  235. Title: `Cannot marshal channels or functions`,
  236. Since: "2019.2",
  237. Severity: lint.SeverityError,
  238. MergeIf: lint.MergeIfAny,
  239. },
  240. "SA1027": {
  241. Title: `Atomic access to 64-bit variable must be 64-bit aligned`,
  242. Text: `On ARM, x86-32, and 32-bit MIPS, it is the caller's responsibility to
  243. arrange for 64-bit alignment of 64-bit words accessed atomically. The
  244. first word in a variable or in an allocated struct, array, or slice
  245. can be relied upon to be 64-bit aligned.
  246. You can use the structlayout tool to inspect the alignment of fields
  247. in a struct.`,
  248. Since: "2019.2",
  249. Severity: lint.SeverityWarning,
  250. MergeIf: lint.MergeIfAny,
  251. },
  252. "SA1028": {
  253. Title: `\'sort.Slice\' can only be used on slices`,
  254. Text: `The first argument of \'sort.Slice\' must be a slice.`,
  255. Since: "2020.1",
  256. Severity: lint.SeverityError,
  257. MergeIf: lint.MergeIfAny,
  258. },
  259. "SA1029": {
  260. Title: `Inappropriate key in call to \'context.WithValue\'`,
  261. Text: `The provided key must be comparable and should not be
  262. of type \'string\' or any other built-in type to avoid collisions between
  263. packages using context. Users of \'WithValue\' should define their own
  264. types for keys.
  265. To avoid allocating when assigning to an \'interface{}\',
  266. context keys often have concrete type \'struct{}\'. Alternatively,
  267. exported context key variables' static type should be a pointer or
  268. interface.`,
  269. Since: "2020.1",
  270. Severity: lint.SeverityWarning,
  271. MergeIf: lint.MergeIfAny,
  272. },
  273. "SA1030": {
  274. Title: `Invalid argument in call to a \'strconv\' function`,
  275. Text: `This check validates the format, number base and bit size arguments of
  276. the various parsing and formatting functions in \'strconv\'.`,
  277. Since: "2021.1",
  278. Severity: lint.SeverityError,
  279. MergeIf: lint.MergeIfAny,
  280. },
  281. "SA2000": {
  282. Title: `\'sync.WaitGroup.Add\' called inside the goroutine, leading to a race condition`,
  283. Since: "2017.1",
  284. Severity: lint.SeverityWarning,
  285. MergeIf: lint.MergeIfAny,
  286. },
  287. "SA2001": {
  288. Title: `Empty critical section, did you mean to defer the unlock?`,
  289. Text: `Empty critical sections of the kind
  290. mu.Lock()
  291. mu.Unlock()
  292. are very often a typo, and the following was intended instead:
  293. mu.Lock()
  294. defer mu.Unlock()
  295. Do note that sometimes empty critical sections can be useful, as a
  296. form of signaling to wait on another goroutine. Many times, there are
  297. simpler ways of achieving the same effect. When that isn't the case,
  298. the code should be amply commented to avoid confusion. Combining such
  299. comments with a \'//lint:ignore\' directive can be used to suppress this
  300. rare false positive.`,
  301. Since: "2017.1",
  302. Severity: lint.SeverityWarning,
  303. MergeIf: lint.MergeIfAny,
  304. },
  305. "SA2002": {
  306. Title: `Called \'testing.T.FailNow\' or \'SkipNow\' in a goroutine, which isn't allowed`,
  307. Since: "2017.1",
  308. Severity: lint.SeverityError,
  309. MergeIf: lint.MergeIfAny,
  310. },
  311. "SA2003": {
  312. Title: `Deferred \'Lock\' right after locking, likely meant to defer \'Unlock\' instead`,
  313. Since: "2017.1",
  314. Severity: lint.SeverityWarning,
  315. MergeIf: lint.MergeIfAny,
  316. },
  317. "SA3000": {
  318. Title: `\'TestMain\' doesn't call \'os.Exit\', hiding test failures`,
  319. Text: `Test executables (and in turn \"go test\") exit with a non-zero status
  320. code if any tests failed. When specifying your own \'TestMain\' function,
  321. it is your responsibility to arrange for this, by calling \'os.Exit\' with
  322. the correct code. The correct code is returned by \'(*testing.M).Run\', so
  323. the usual way of implementing \'TestMain\' is to end it with
  324. \'os.Exit(m.Run())\'.`,
  325. Since: "2017.1",
  326. Severity: lint.SeverityWarning,
  327. MergeIf: lint.MergeIfAny,
  328. },
  329. "SA3001": {
  330. Title: `Assigning to \'b.N\' in benchmarks distorts the results`,
  331. Text: `The testing package dynamically sets \'b.N\' to improve the reliability of
  332. benchmarks and uses it in computations to determine the duration of a
  333. single operation. Benchmark code must not alter \'b.N\' as this would
  334. falsify results.`,
  335. Since: "2017.1",
  336. Severity: lint.SeverityError,
  337. MergeIf: lint.MergeIfAny,
  338. },
  339. "SA4000": {
  340. Title: `Binary operator has identical expressions on both sides`,
  341. Since: "2017.1",
  342. Severity: lint.SeverityWarning,
  343. MergeIf: lint.MergeIfAny,
  344. },
  345. "SA4001": {
  346. Title: `\'&*x\' gets simplified to \'x\', it does not copy \'x\'`,
  347. Since: "2017.1",
  348. Severity: lint.SeverityWarning,
  349. MergeIf: lint.MergeIfAny,
  350. },
  351. "SA4003": {
  352. Title: `Comparing unsigned values against negative values is pointless`,
  353. Since: "2017.1",
  354. Severity: lint.SeverityWarning,
  355. MergeIf: lint.MergeIfAll,
  356. },
  357. "SA4004": {
  358. Title: `The loop exits unconditionally after one iteration`,
  359. Since: "2017.1",
  360. Severity: lint.SeverityWarning,
  361. MergeIf: lint.MergeIfAll,
  362. },
  363. "SA4005": {
  364. Title: `Field assignment that will never be observed. Did you mean to use a pointer receiver?`,
  365. Since: "2021.1",
  366. Severity: lint.SeverityWarning,
  367. MergeIf: lint.MergeIfAny,
  368. },
  369. "SA4006": {
  370. Title: `A value assigned to a variable is never read before being overwritten. Forgotten error check or dead code?`,
  371. Since: "2017.1",
  372. Severity: lint.SeverityWarning,
  373. MergeIf: lint.MergeIfAll,
  374. },
  375. "SA4008": {
  376. Title: `The variable in the loop condition never changes, are you incrementing the wrong variable?`,
  377. Since: "2017.1",
  378. Severity: lint.SeverityWarning,
  379. MergeIf: lint.MergeIfAll,
  380. },
  381. "SA4009": {
  382. Title: `A function argument is overwritten before its first use`,
  383. Since: "2017.1",
  384. Severity: lint.SeverityWarning,
  385. MergeIf: lint.MergeIfAny,
  386. },
  387. "SA4010": {
  388. Title: `The result of \'append\' will never be observed anywhere`,
  389. Since: "2017.1",
  390. Severity: lint.SeverityWarning,
  391. MergeIf: lint.MergeIfAll,
  392. },
  393. "SA4011": {
  394. Title: `Break statement with no effect. Did you mean to break out of an outer loop?`,
  395. Since: "2017.1",
  396. Severity: lint.SeverityWarning,
  397. MergeIf: lint.MergeIfAny,
  398. },
  399. "SA4012": {
  400. Title: `Comparing a value against NaN even though no value is equal to NaN`,
  401. Since: "2017.1",
  402. Severity: lint.SeverityWarning,
  403. MergeIf: lint.MergeIfAny,
  404. },
  405. "SA4013": {
  406. Title: `Negating a boolean twice (\'!!b\') is the same as writing \'b\'. This is either redundant, or a typo.`,
  407. Since: "2017.1",
  408. Severity: lint.SeverityWarning,
  409. MergeIf: lint.MergeIfAny,
  410. },
  411. "SA4014": {
  412. Title: `An if/else if chain has repeated conditions and no side-effects; if the condition didn't match the first time, it won't match the second time, either`,
  413. Since: "2017.1",
  414. Severity: lint.SeverityWarning,
  415. MergeIf: lint.MergeIfAll,
  416. },
  417. "SA4015": {
  418. Title: `Calling functions like \'math.Ceil\' on floats converted from integers doesn't do anything useful`,
  419. Since: "2017.1",
  420. Severity: lint.SeverityWarning,
  421. MergeIf: lint.MergeIfAll,
  422. },
  423. "SA4016": {
  424. Title: `Certain bitwise operations, such as \'x ^ 0\', do not do anything useful`,
  425. Since: "2017.1",
  426. Severity: lint.SeverityWarning,
  427. MergeIf: lint.MergeIfAny, // MergeIfAny if we only flag literals, not named constants
  428. },
  429. "SA4017": {
  430. Title: `A pure function's return value is discarded, making the call pointless`,
  431. Since: "2017.1",
  432. Severity: lint.SeverityWarning,
  433. MergeIf: lint.MergeIfAll,
  434. },
  435. "SA4018": {
  436. Title: `Self-assignment of variables`,
  437. Since: "2017.1",
  438. Severity: lint.SeverityWarning,
  439. MergeIf: lint.MergeIfAny,
  440. },
  441. "SA4019": {
  442. Title: `Multiple, identical build constraints in the same file`,
  443. Since: "2017.1",
  444. Severity: lint.SeverityWarning,
  445. MergeIf: lint.MergeIfAny,
  446. },
  447. "SA4020": {
  448. Title: `Unreachable case clause in a type switch`,
  449. Text: `In a type switch like the following
  450. type T struct{}
  451. func (T) Read(b []byte) (int, error) { return 0, nil }
  452. var v interface{} = T{}
  453. switch v.(type) {
  454. case io.Reader:
  455. // ...
  456. case T:
  457. // unreachable
  458. }
  459. the second case clause can never be reached because \'T\' implements
  460. \'io.Reader\' and case clauses are evaluated in source order.
  461. Another example:
  462. type T struct{}
  463. func (T) Read(b []byte) (int, error) { return 0, nil }
  464. func (T) Close() error { return nil }
  465. var v interface{} = T{}
  466. switch v.(type) {
  467. case io.Reader:
  468. // ...
  469. case io.ReadCloser:
  470. // unreachable
  471. }
  472. Even though \'T\' has a \'Close\' method and thus implements \'io.ReadCloser\',
  473. \'io.Reader\' will always match first. The method set of \'io.Reader\' is a
  474. subset of \'io.ReadCloser\'. Thus it is impossible to match the second
  475. case without matching the first case.
  476. Structurally equivalent interfaces
  477. A special case of the previous example are structurally identical
  478. interfaces. Given these declarations
  479. type T error
  480. type V error
  481. func doSomething() error {
  482. err, ok := doAnotherThing()
  483. if ok {
  484. return T(err)
  485. }
  486. return U(err)
  487. }
  488. the following type switch will have an unreachable case clause:
  489. switch doSomething().(type) {
  490. case T:
  491. // ...
  492. case V:
  493. // unreachable
  494. }
  495. \'T\' will always match before V because they are structurally equivalent
  496. and therefore \'doSomething()\''s return value implements both.`,
  497. Since: "2019.2",
  498. Severity: lint.SeverityWarning,
  499. MergeIf: lint.MergeIfAll,
  500. },
  501. "SA4021": {
  502. Title: `\"x = append(y)\" is equivalent to \"x = y\"`,
  503. Since: "2019.2",
  504. Severity: lint.SeverityWarning,
  505. MergeIf: lint.MergeIfAny,
  506. },
  507. "SA4022": {
  508. Title: `Comparing the address of a variable against nil`,
  509. Text: `Code such as \"if &x == nil\" is meaningless, because taking the address of a variable always yields a non-nil pointer.`,
  510. Since: "2020.1",
  511. Severity: lint.SeverityWarning,
  512. MergeIf: lint.MergeIfAny,
  513. },
  514. "SA4023": {
  515. Title: `Impossible comparison of interface value with untyped nil`,
  516. Text: `Under the covers, interfaces are implemented as two elements, a
  517. type T and a value V. V is a concrete value such as an int,
  518. struct or pointer, never an interface itself, and has type T. For
  519. instance, if we store the int value 3 in an interface, the
  520. resulting interface value has, schematically, (T=int, V=3). The
  521. value V is also known as the interface's dynamic value, since a
  522. given interface variable might hold different values V (and
  523. corresponding types T) during the execution of the program.
  524. An interface value is nil only if the V and T are both
  525. unset, (T=nil, V is not set), In particular, a nil interface will
  526. always hold a nil type. If we store a nil pointer of type *int
  527. inside an interface value, the inner type will be *int regardless
  528. of the value of the pointer: (T=*int, V=nil). Such an interface
  529. value will therefore be non-nil even when the pointer value V
  530. inside is nil.
  531. This situation can be confusing, and arises when a nil value is
  532. stored inside an interface value such as an error return:
  533. func returnsError() error {
  534. var p *MyError = nil
  535. if bad() {
  536. p = ErrBad
  537. }
  538. return p // Will always return a non-nil error.
  539. }
  540. If all goes well, the function returns a nil p, so the return
  541. value is an error interface value holding (T=*MyError, V=nil).
  542. This means that if the caller compares the returned error to nil,
  543. it will always look as if there was an error even if nothing bad
  544. happened. To return a proper nil error to the caller, the
  545. function must return an explicit nil:
  546. func returnsError() error {
  547. if bad() {
  548. return ErrBad
  549. }
  550. return nil
  551. }
  552. It's a good idea for functions that return errors always to use
  553. the error type in their signature (as we did above) rather than a
  554. concrete type such as \'*MyError\', to help guarantee the error is
  555. created correctly. As an example, \'os.Open\' returns an error even
  556. though, if not nil, it's always of concrete type *os.PathError.
  557. Similar situations to those described here can arise whenever
  558. interfaces are used. Just keep in mind that if any concrete value
  559. has been stored in the interface, the interface will not be nil.
  560. For more information, see The Laws of
  561. Reflection (https://golang.org/doc/articles/laws_of_reflection.html).
  562. This text has been copied from
  563. https://golang.org/doc/faq#nil_error, licensed under the Creative
  564. Commons Attribution 3.0 License.`,
  565. Since: "2020.2",
  566. Severity: lint.SeverityWarning,
  567. MergeIf: lint.MergeIfAny, // TODO should this be MergeIfAll?
  568. },
  569. "SA4024": {
  570. Title: `Checking for impossible return value from a builtin function`,
  571. Text: `Return values of the \'len\' and \'cap\' builtins cannot be negative.
  572. See https://golang.org/pkg/builtin/#len and https://golang.org/pkg/builtin/#cap.
  573. Example:
  574. if len(slice) < 0 {
  575. fmt.Println("unreachable code")
  576. }`,
  577. Since: "2021.1",
  578. Severity: lint.SeverityWarning,
  579. MergeIf: lint.MergeIfAny,
  580. },
  581. "SA4025": {
  582. Title: "Integer division of literals that results in zero",
  583. Text: `When dividing two integer constants, the result will
  584. also be an integer. Thus, a division such as \'2 / 3\' results in \'0\'.
  585. This is true for all of the following examples:
  586. _ = 2 / 3
  587. const _ = 2 / 3
  588. const _ float64 = 2 / 3
  589. _ = float64(2 / 3)
  590. Staticcheck will flag such divisions if both sides of the division are
  591. integer literals, as it is highly unlikely that the division was
  592. intended to truncate to zero. Staticcheck will not flag integer
  593. division involving named constants, to avoid noisy positives.
  594. `,
  595. Since: "2021.1",
  596. Severity: lint.SeverityWarning,
  597. MergeIf: lint.MergeIfAny,
  598. },
  599. "SA4026": {
  600. Title: "Go constants cannot express negative zero",
  601. Text: `In IEEE 754 floating point math, zero has a sign and can be positive
  602. or negative. This can be useful in certain numerical code.
  603. Go constants, however, cannot express negative zero. This means that
  604. the literals \'-0.0\' and \'0.0\' have the same ideal value (zero) and
  605. will both represent positive zero at runtime.
  606. To explicitly and reliably create a negative zero, you can use the
  607. \'math.Copysign\' function: \'math.Copysign(0, -1)\'.`,
  608. Since: "2021.1",
  609. Severity: lint.SeverityWarning,
  610. MergeIf: lint.MergeIfAny,
  611. },
  612. "SA4027": {
  613. Title: `\'(*net/url.URL).Query\' returns a copy, modifying it doesn't change the URL`,
  614. Text: `\'(*net/url.URL).Query\' parses the current value of \'net/url.URL.RawQuery\'
  615. and returns it as a map of type \'net/url.Values\'. Subsequent changes to
  616. this map will not affect the URL unless the map gets encoded and
  617. assigned to the URL's \'RawQuery\'.
  618. As a consequence, the following code pattern is an expensive no-op:
  619. \'u.Query().Add(key, value)\'.`,
  620. Since: "2021.1",
  621. Severity: lint.SeverityWarning,
  622. MergeIf: lint.MergeIfAny,
  623. },
  624. "SA4028": {
  625. Title: `\'x % 1\' is always zero`,
  626. Since: "2022.1",
  627. Severity: lint.SeverityWarning,
  628. MergeIf: lint.MergeIfAny, // MergeIfAny if we only flag literals, not named constants
  629. },
  630. "SA4029": {
  631. Title: "Ineffective attempt at sorting slice",
  632. Text: `
  633. \'sort.Float64Slice\', \'sort.IntSlice\', and \'sort.StringSlice\' are
  634. types, not functions. Doing \'x = sort.StringSlice(x)\' does nothing,
  635. especially not sort any values. The correct usage is
  636. \'sort.Sort(sort.StringSlice(x))\' or \'sort.StringSlice(x).Sort()\',
  637. but there are more convenient helpers, namely \'sort.Float64s\',
  638. \'sort.Ints\', and \'sort.Strings\'.
  639. `,
  640. Since: "2022.1",
  641. Severity: lint.SeverityWarning,
  642. MergeIf: lint.MergeIfAny,
  643. },
  644. "SA4030": {
  645. Title: "Ineffective attempt at generating random number",
  646. Text: `
  647. Functions in the \'math/rand\' package that accept upper limits, such
  648. as \'Intn\', generate random numbers in the half-open interval [0,n). In
  649. other words, the generated numbers will be \'>= 0\' and \'< n\' – they
  650. don't include \'n\'. \'rand.Intn(1)\' therefore doesn't generate \'0\'
  651. or \'1\', it always generates \'0\'.`,
  652. Since: "2022.1",
  653. Severity: lint.SeverityWarning,
  654. MergeIf: lint.MergeIfAny,
  655. },
  656. "SA4031": {
  657. Title: `Checking never-nil value against nil`,
  658. Since: "2022.1",
  659. Severity: lint.SeverityWarning,
  660. MergeIf: lint.MergeIfAny,
  661. },
  662. "SA5000": {
  663. Title: `Assignment to nil map`,
  664. Since: "2017.1",
  665. Severity: lint.SeverityError,
  666. MergeIf: lint.MergeIfAny,
  667. },
  668. "SA5001": {
  669. Title: `Deferring \'Close\' before checking for a possible error`,
  670. Since: "2017.1",
  671. Severity: lint.SeverityWarning,
  672. MergeIf: lint.MergeIfAny,
  673. },
  674. "SA5002": {
  675. Title: `The empty for loop (\"for {}\") spins and can block the scheduler`,
  676. Since: "2017.1",
  677. Severity: lint.SeverityWarning,
  678. MergeIf: lint.MergeIfAny,
  679. },
  680. "SA5003": {
  681. Title: `Defers in infinite loops will never execute`,
  682. Text: `Defers are scoped to the surrounding function, not the surrounding
  683. block. In a function that never returns, i.e. one containing an
  684. infinite loop, defers will never execute.`,
  685. Since: "2017.1",
  686. Severity: lint.SeverityWarning,
  687. MergeIf: lint.MergeIfAny,
  688. },
  689. "SA5004": {
  690. Title: `\"for { select { ...\" with an empty default branch spins`,
  691. Since: "2017.1",
  692. Severity: lint.SeverityWarning,
  693. MergeIf: lint.MergeIfAny,
  694. },
  695. "SA5005": {
  696. Title: `The finalizer references the finalized object, preventing garbage collection`,
  697. Text: `A finalizer is a function associated with an object that runs when the
  698. garbage collector is ready to collect said object, that is when the
  699. object is no longer referenced by anything.
  700. If the finalizer references the object, however, it will always remain
  701. as the final reference to that object, preventing the garbage
  702. collector from collecting the object. The finalizer will never run,
  703. and the object will never be collected, leading to a memory leak. That
  704. is why the finalizer should instead use its first argument to operate
  705. on the object. That way, the number of references can temporarily go
  706. to zero before the object is being passed to the finalizer.`,
  707. Since: "2017.1",
  708. Severity: lint.SeverityWarning,
  709. MergeIf: lint.MergeIfAny,
  710. },
  711. "SA5007": {
  712. Title: `Infinite recursive call`,
  713. Text: `A function that calls itself recursively needs to have an exit
  714. condition. Otherwise it will recurse forever, until the system runs
  715. out of memory.
  716. This issue can be caused by simple bugs such as forgetting to add an
  717. exit condition. It can also happen "on purpose". Some languages have
  718. tail call optimization which makes certain infinite recursive calls
  719. safe to use. Go, however, does not implement TCO, and as such a loop
  720. should be used instead.`,
  721. Since: "2017.1",
  722. Severity: lint.SeverityWarning,
  723. MergeIf: lint.MergeIfAny,
  724. },
  725. "SA5008": {
  726. Title: `Invalid struct tag`,
  727. Since: "2019.2",
  728. Severity: lint.SeverityWarning,
  729. MergeIf: lint.MergeIfAny,
  730. },
  731. "SA5009": {
  732. Title: `Invalid Printf call`,
  733. Since: "2019.2",
  734. Severity: lint.SeverityError,
  735. MergeIf: lint.MergeIfAny,
  736. },
  737. "SA5010": {
  738. Title: `Impossible type assertion`,
  739. Text: `Some type assertions can be statically proven to be
  740. impossible. This is the case when the method sets of both
  741. arguments of the type assertion conflict with each other, for
  742. example by containing the same method with different
  743. signatures.
  744. The Go compiler already applies this check when asserting from an
  745. interface value to a concrete type. If the concrete type misses
  746. methods from the interface, or if function signatures don't match,
  747. then the type assertion can never succeed.
  748. This check applies the same logic when asserting from one interface to
  749. another. If both interface types contain the same method but with
  750. different signatures, then the type assertion can never succeed,
  751. either.`,
  752. Since: "2020.1",
  753. Severity: lint.SeverityWarning,
  754. // Technically this should be MergeIfAll, but the Go compiler
  755. // already flags some impossible type assertions, so
  756. // MergeIfAny is consistent with the compiler.
  757. MergeIf: lint.MergeIfAny,
  758. },
  759. "SA5011": {
  760. Title: `Possible nil pointer dereference`,
  761. Text: `A pointer is being dereferenced unconditionally, while
  762. also being checked against nil in another place. This suggests that
  763. the pointer may be nil and dereferencing it may panic. This is
  764. commonly a result of improperly ordered code or missing return
  765. statements. Consider the following examples:
  766. func fn(x *int) {
  767. fmt.Println(*x)
  768. // This nil check is equally important for the previous dereference
  769. if x != nil {
  770. foo(*x)
  771. }
  772. }
  773. func TestFoo(t *testing.T) {
  774. x := compute()
  775. if x == nil {
  776. t.Errorf("nil pointer received")
  777. }
  778. // t.Errorf does not abort the test, so if x is nil, the next line will panic.
  779. foo(*x)
  780. }
  781. Staticcheck tries to deduce which functions abort control flow.
  782. For example, it is aware that a function will not continue
  783. execution after a call to \'panic\' or \'log.Fatal\'. However, sometimes
  784. this detection fails, in particular in the presence of
  785. conditionals. Consider the following example:
  786. func Log(msg string, level int) {
  787. fmt.Println(msg)
  788. if level == levelFatal {
  789. os.Exit(1)
  790. }
  791. }
  792. func Fatal(msg string) {
  793. Log(msg, levelFatal)
  794. }
  795. func fn(x *int) {
  796. if x == nil {
  797. Fatal("unexpected nil pointer")
  798. }
  799. fmt.Println(*x)
  800. }
  801. Staticcheck will flag the dereference of \'x\', even though it is perfectly
  802. safe. Staticcheck is not able to deduce that a call to
  803. Fatal will exit the program. For the time being, the easiest
  804. workaround is to modify the definition of Fatal like so:
  805. func Fatal(msg string) {
  806. Log(msg, levelFatal)
  807. panic("unreachable")
  808. }
  809. We also hard-code functions from common logging packages such as
  810. logrus. Please file an issue if we're missing support for a
  811. popular package.`,
  812. Since: "2020.1",
  813. Severity: lint.SeverityWarning,
  814. MergeIf: lint.MergeIfAny,
  815. },
  816. "SA5012": {
  817. Title: "Passing odd-sized slice to function expecting even size",
  818. Text: `Some functions that take slices as parameters expect the slices to have an even number of elements.
  819. Often, these functions treat elements in a slice as pairs.
  820. For example, \'strings.NewReplacer\' takes pairs of old and new strings,
  821. and calling it with an odd number of elements would be an error.`,
  822. Since: "2020.2",
  823. Severity: lint.SeverityError,
  824. MergeIf: lint.MergeIfAny,
  825. },
  826. "SA6000": {
  827. Title: `Using \'regexp.Match\' or related in a loop, should use \'regexp.Compile\'`,
  828. Since: "2017.1",
  829. Severity: lint.SeverityWarning,
  830. MergeIf: lint.MergeIfAny,
  831. },
  832. "SA6001": {
  833. Title: `Missing an optimization opportunity when indexing maps by byte slices`,
  834. Text: `Map keys must be comparable, which precludes the use of byte slices.
  835. This usually leads to using string keys and converting byte slices to
  836. strings.
  837. Normally, a conversion of a byte slice to a string needs to copy the data and
  838. causes allocations. The compiler, however, recognizes \'m[string(b)]\' and
  839. uses the data of \'b\' directly, without copying it, because it knows that
  840. the data can't change during the map lookup. This leads to the
  841. counter-intuitive situation that
  842. k := string(b)
  843. println(m[k])
  844. println(m[k])
  845. will be less efficient than
  846. println(m[string(b)])
  847. println(m[string(b)])
  848. because the first version needs to copy and allocate, while the second
  849. one does not.
  850. For some history on this optimization, check out commit
  851. f5f5a8b6209f84961687d993b93ea0d397f5d5bf in the Go repository.`,
  852. Since: "2017.1",
  853. Severity: lint.SeverityWarning,
  854. MergeIf: lint.MergeIfAny,
  855. },
  856. "SA6002": {
  857. Title: `Storing non-pointer values in \'sync.Pool\' allocates memory`,
  858. Text: `A \'sync.Pool\' is used to avoid unnecessary allocations and reduce the
  859. amount of work the garbage collector has to do.
  860. When passing a value that is not a pointer to a function that accepts
  861. an interface, the value needs to be placed on the heap, which means an
  862. additional allocation. Slices are a common thing to put in sync.Pools,
  863. and they're structs with 3 fields (length, capacity, and a pointer to
  864. an array). In order to avoid the extra allocation, one should store a
  865. pointer to the slice instead.
  866. See the comments on https://go-review.googlesource.com/c/go/+/24371
  867. that discuss this problem.`,
  868. Since: "2017.1",
  869. Severity: lint.SeverityWarning,
  870. MergeIf: lint.MergeIfAny,
  871. },
  872. "SA6003": {
  873. Title: `Converting a string to a slice of runes before ranging over it`,
  874. Text: `You may want to loop over the runes in a string. Instead of converting
  875. the string to a slice of runes and looping over that, you can loop
  876. over the string itself. That is,
  877. for _, r := range s {}
  878. and
  879. for _, r := range []rune(s) {}
  880. will yield the same values. The first version, however, will be faster
  881. and avoid unnecessary memory allocations.
  882. Do note that if you are interested in the indices, ranging over a
  883. string and over a slice of runes will yield different indices. The
  884. first one yields byte offsets, while the second one yields indices in
  885. the slice of runes.`,
  886. Since: "2017.1",
  887. Severity: lint.SeverityWarning,
  888. MergeIf: lint.MergeIfAny,
  889. },
  890. "SA6005": {
  891. Title: `Inefficient string comparison with \'strings.ToLower\' or \'strings.ToUpper\'`,
  892. Text: `Converting two strings to the same case and comparing them like so
  893. if strings.ToLower(s1) == strings.ToLower(s2) {
  894. ...
  895. }
  896. is significantly more expensive than comparing them with
  897. \'strings.EqualFold(s1, s2)\'. This is due to memory usage as well as
  898. computational complexity.
  899. \'strings.ToLower\' will have to allocate memory for the new strings, as
  900. well as convert both strings fully, even if they differ on the very
  901. first byte. strings.EqualFold, on the other hand, compares the strings
  902. one character at a time. It doesn't need to create two intermediate
  903. strings and can return as soon as the first non-matching character has
  904. been found.
  905. For a more in-depth explanation of this issue, see
  906. https://blog.digitalocean.com/how-to-efficiently-compare-strings-in-go/`,
  907. Since: "2019.2",
  908. Severity: lint.SeverityWarning,
  909. MergeIf: lint.MergeIfAny,
  910. },
  911. "SA9001": {
  912. Title: `Defers in range loops may not run when you expect them to`,
  913. Since: "2017.1",
  914. Severity: lint.SeverityWarning,
  915. MergeIf: lint.MergeIfAny,
  916. },
  917. "SA9002": {
  918. Title: `Using a non-octal \'os.FileMode\' that looks like it was meant to be in octal.`,
  919. Since: "2017.1",
  920. Severity: lint.SeverityWarning,
  921. MergeIf: lint.MergeIfAny,
  922. },
  923. "SA9003": {
  924. Title: `Empty body in an if or else branch`,
  925. Since: "2017.1",
  926. Severity: lint.SeverityWarning,
  927. MergeIf: lint.MergeIfAny,
  928. },
  929. "SA9004": {
  930. Title: `Only the first constant has an explicit type`,
  931. Text: `In a constant declaration such as the following:
  932. const (
  933. First byte = 1
  934. Second = 2
  935. )
  936. the constant Second does not have the same type as the constant First.
  937. This construct shouldn't be confused with
  938. const (
  939. First byte = iota
  940. Second
  941. )
  942. where \'First\' and \'Second\' do indeed have the same type. The type is only
  943. passed on when no explicit value is assigned to the constant.
  944. When declaring enumerations with explicit values it is therefore
  945. important not to write
  946. const (
  947. EnumFirst EnumType = 1
  948. EnumSecond = 2
  949. EnumThird = 3
  950. )
  951. This discrepancy in types can cause various confusing behaviors and
  952. bugs.
  953. Wrong type in variable declarations
  954. The most obvious issue with such incorrect enumerations expresses
  955. itself as a compile error:
  956. package pkg
  957. const (
  958. EnumFirst uint8 = 1
  959. EnumSecond = 2
  960. )
  961. func fn(useFirst bool) {
  962. x := EnumSecond
  963. if useFirst {
  964. x = EnumFirst
  965. }
  966. }
  967. fails to compile with
  968. ./const.go:11:5: cannot use EnumFirst (type uint8) as type int in assignment
  969. Losing method sets
  970. A more subtle issue occurs with types that have methods and optional
  971. interfaces. Consider the following:
  972. package main
  973. import "fmt"
  974. type Enum int
  975. func (e Enum) String() string {
  976. return "an enum"
  977. }
  978. const (
  979. EnumFirst Enum = 1
  980. EnumSecond = 2
  981. )
  982. func main() {
  983. fmt.Println(EnumFirst)
  984. fmt.Println(EnumSecond)
  985. }
  986. This code will output
  987. an enum
  988. 2
  989. as \'EnumSecond\' has no explicit type, and thus defaults to \'int\'.`,
  990. Since: "2019.1",
  991. Severity: lint.SeverityWarning,
  992. MergeIf: lint.MergeIfAny,
  993. },
  994. "SA9005": {
  995. Title: `Trying to marshal a struct with no public fields nor custom marshaling`,
  996. Text: `
  997. The \'encoding/json\' and \'encoding/xml\' packages only operate on exported
  998. fields in structs, not unexported ones. It is usually an error to try
  999. to (un)marshal structs that only consist of unexported fields.
  1000. This check will not flag calls involving types that define custom
  1001. marshaling behavior, e.g. via \'MarshalJSON\' methods. It will also not
  1002. flag empty structs.`,
  1003. Since: "2019.2",
  1004. Severity: lint.SeverityWarning,
  1005. MergeIf: lint.MergeIfAll,
  1006. },
  1007. "SA9006": {
  1008. Title: `Dubious bit shifting of a fixed size integer value`,
  1009. Text: `Bit shifting a value past its size will always clear the value.
  1010. For instance:
  1011. v := int8(42)
  1012. v >>= 8
  1013. will always result in 0.
  1014. This check flags bit shifting operations on fixed size integer values only.
  1015. That is, int, uint and uintptr are never flagged to avoid potential false
  1016. positives in somewhat exotic but valid bit twiddling tricks:
  1017. // Clear any value above 32 bits if integers are more than 32 bits.
  1018. func f(i int) int {
  1019. v := i >> 32
  1020. v = v << 32
  1021. return i-v
  1022. }`,
  1023. Since: "2020.2",
  1024. Severity: lint.SeverityWarning,
  1025. // Technically this should be MergeIfAll, because the type of
  1026. // v might be different for different build tags. Practically,
  1027. // don't write code that depends on that.
  1028. MergeIf: lint.MergeIfAny,
  1029. },
  1030. "SA9007": {
  1031. Title: "Deleting a directory that shouldn't be deleted",
  1032. Text: `
  1033. It is virtually never correct to delete system directories such as
  1034. /tmp or the user's home directory. However, it can be fairly easy to
  1035. do by mistake, for example by mistakingly using \'os.TempDir\' instead
  1036. of \'ioutil.TempDir\', or by forgetting to add a suffix to the result
  1037. of \'os.UserHomeDir\'.
  1038. Writing
  1039. d := os.TempDir()
  1040. defer os.RemoveAll(d)
  1041. in your unit tests will have a devastating effect on the stability of your system.
  1042. This check flags attempts at deleting the following directories:
  1043. - os.TempDir
  1044. - os.UserCacheDir
  1045. - os.UserConfigDir
  1046. - os.UserHomeDir
  1047. `,
  1048. Since: "2022.1",
  1049. Severity: lint.SeverityWarning,
  1050. MergeIf: lint.MergeIfAny,
  1051. },
  1052. "SA9008": {
  1053. Title: `\'else\' branch of a type assertion is probably not reading the right value`,
  1054. Text: `
  1055. When declaring variables as part of an \'if\' statement (like in \"if
  1056. foo := ...; foo {\"), the same variables will also be in the scope of
  1057. the \'else\' branch. This means that in the following example
  1058. if x, ok := x.(int); ok {
  1059. // ...
  1060. } else {
  1061. fmt.Println("unexpected type %T", x)
  1062. }
  1063. \'x\' in the \'else\' branch will refer to the \'x\' from \'x, ok
  1064. :=\'; it will not refer to the \'x\' that is being type-asserted. The
  1065. result of a failed type assertion is the zero value of the type that
  1066. is being asserted to, so \'x\' in the else branch will always have the
  1067. value \'0\' and the type \'int\'.
  1068. `,
  1069. Since: "2022.1",
  1070. Severity: lint.SeverityWarning,
  1071. MergeIf: lint.MergeIfAny,
  1072. },
  1073. })