Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / doc / staticcheck.html
1 <!--(
2      "Title": "Staticcheck"
3 )-->
4
5
6 <h2 id="overview">Overview</h2>
7
8 <p>
9   Staticcheck is a static analysis toolset for the <a href="https://golang.org">Go programming language.</a>
10   It comes with a large number of checks,
11   integrates with various Go build systems
12   and offers enough customizability to fit into your workflows.
13 </p>
14
15 <h2 id="installation">Installation</h2>
16
17 <p>
18   There are various ways in which you can install staticcheck,
19   but they all boil down to obtaining the command located at <code>honnef.co/go/tools/cmd/staticcheck</code>
20 </p>
21
22 <p>
23   If you use Go modules, you can simply run <code>go get honnef.co/go/tools/cmd/staticcheck</code> to obtain the latest released version.
24   If you're still using a GOPATH-based workflow, then the above command will instead fetch the master branch.
25   It is suggested that you explicitly check out the latest release branch instead, which is currently <code>2020.1.5</code>.
26   One way of doing so would be as follows:
27 </p>
28
29 <pre><code>cd $GOPATH/src/honnef.co/go/tools/cmd/staticcheck
30 git checkout 2020.1.4
31 go get
32 go install
33 </code></pre>
34
35 <p>
36   Alternatively, you can <a href="https://github.com/dominikh/go-tools/releases">download pre-compiled binaries from GitHub.</a>
37 </p>
38
39 <p>
40   If you'd like to be notified of new releases, you can use GitHub's
41   <a href="https://help.github.com/en/articles/watching-and-unwatching-releases-for-a-repository"><em>Releases only</em> watches</a>.
42 </p>
43
44 <h2 id="running-the-tools">Running staticcheck</h2>
45
46 <p>
47   Staticcheck can be run on code in several ways,
48   mimicking the way the official Go tools work.
49   At the core, it expects to be run on well-formed Go packages.
50   The most common way of specifying packages is via their import paths.
51   One or more packages can be specified in a single command,
52   and the <code>...</code> glob operator is supported.
53   All of the following examples are valid invocations:
54
55   <pre><code>staticcheck github.com/example/foo
56 staticcheck github.com/example/foo github.com/example/bar
57 staticcheck github.com/example/...</code></pre>
58 </p>
59
60 <p>
61   In addition, a single package can be specified as a list of files:
62
63   <pre><code>staticcheck file1.go file2.go file3.go</code></pre>
64
65   Note that <strong>all</strong> files of the package need to be specified,
66   similar to how <code>go build</code> works.
67 </p>
68
69 <h2 id="configuration">Configuration</h2>
70
71 <p>
72   Various aspects of staticcheck can be customized with configuration files.
73 </p>
74
75 <p>
76   These files are placed in Go packages and apply recursively to the package tree rooted at the containing package.
77   For example, configuration placed in <code>pkg</code> will apply to <code>pkg</code>, <code>pkg/subpkg</code>, <code>pkg/subpkg/subsubpkg</code> and so on.
78 </p>
79
80 <p>
81   Configuration files in subpackages can override or inherit from settings of configuration files higher up the package tree.
82   Staticcheck's default configuration is represented as the virtual root of the configuration tree and can be inherited from.
83 </p>
84
85 <h3>Configuration format</h3>
86
87 <p>
88   Staticcheck configuration files are named <code>staticcheck.conf</code> and contain <a href="https://github.com/toml-lang/toml">TOML</a>.
89 </p>
90
91 <p>
92   Any set option will override the same option from further up the package tree,
93   whereas unset options will inherit their values.
94   Additionally, the special value <code>"inherit"</code> can be used to inherit values.
95   This is especially useful for array values, as it allows adding and removing values to the inherited option.
96 </p>
97
98 <p>
99   The special value <code>"all"</code> matches all possible values.
100   Currently, this is only used when enabling checks.
101 </p>
102
103 <p>
104   Values prefixed with a minus sign,
105   such as <code>"-S1000"</code>
106   will exclude values from a list.
107   This can be used in combination with <code>"all"</code> to express "all but",
108   or in combination with <code>"inherit"</code> to remove values from the inherited option.
109 </p>
110
111 <h3>Options</h3>
112
113 <p>
114   A list of all options and their explanations can be found on the <a href="/docs/options">Options</a> page.
115 </p>
116
117 <h3>Example configuration</h3>
118
119 <p>
120   The following example configuration is the textual representation of staticcheck's default configuration.
121 </p>
122
123 <pre><code>{{ option "checks" }} = ["all", "-{{ check "ST1000" }}", "-{{ check "ST1003" }}", "-{{ check "ST1016" }}"]
124 {{ option "initialisms" }} = ["ACL", "API", "ASCII", "CPU", "CSS", "DNS",
125         "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID",
126         "IP", "JSON", "QPS", "RAM", "RPC", "SLA",
127         "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL",
128         "UDP", "UI", "GID", "UID", "UUID", "URI",
129         "URL", "UTF8", "VM", "XML", "XMPP", "XSRF",
130         "XSS"]
131 {{ option "dot_import_whitelist" }} = []
132 {{ option "http_status_code_whitelist" }} = ["200", "400", "404", "500"]</code></pre>
133
134 <h2 id="cli">Command-line flags</h2>
135
136 <p>
137   In addition to configuration files, some aspects of staticcheck can be controlled via command-line flags.
138   These are settings that can vary between individual invocations or environments (CI, editors, ...) and shouldn't be stored in configuration files.
139 </p>
140
141 <table class="table">
142   <tr>
143     <th>Flag</th>
144     <th>Description</th>
145   </tr>
146   <tr>
147     <td>-checks</td>
148     <td>
149       Allows overriding the list of checks to run.
150       Has the same syntax as the <a href="/docs/options#checks"><code>checks</code></a> setting
151       in configuration files.
152     </td>
153   </tr>
154   <tr>
155     <td>-explain</td>
156     <td>
157       Print the description of a check.
158     </td>
159   </tr>
160   <tr>
161     <td>-f</td>
162     <td>
163       Select between the different <a href="/docs/formatters">output formats</a>.
164     </td>
165   </tr>
166   <tr>
167     <td>-fail</td>
168     <td>
169       Specify the list of checks which,
170       if they find any issues in your code,
171       should cause staticcheck to exit with a non-zero status.
172       This can be used, for example, to not fail your CI
173       pipeline because of possible code simplifications.
174     </td>
175   </tr>
176   <tr>
177     <td>-go</td>
178     <td>
179       Select the Go version to target.
180       See
181       <a href="#targeting-go-versions">Targeting Go versions</a>
182       for more details.
183     </td>
184   </tr>
185   <tr>
186     <td style="white-space: nowrap">-show-ignored</td>
187     <td>
188       Show all problems found,
189       even those that were ignored by linter directives.
190     </td>
191   </tr>
192   <tr>
193     <td>-tags</td>
194     <td>
195       Similar to <code>go build -tags</code>,
196       allows specifying the build tags to use.
197     </td>
198   </tr>
199   <tr>
200     <td>-tests</td>
201     <td>
202       Include tests in the analysis.
203     </td>
204   </tr>
205   <tr>
206     <td>-unused.whole-program</td>
207     <td>
208       Run unused in whole program mode.
209     </td>
210   </tr>
211   <tr>
212     <td>-version</td>
213     <td>
214       Display the version of staticcheck and exit.
215     </td>
216   </tr>
217 </table>
218
219 <h2 id="targeting-go-versions">Targeting Go versions</h2>
220
221 <p>
222   By default, staticcheck will make suggestions that are correct for the current version of Go.
223   If you're wishing to support older versions of Go,
224   not all suggestions are applicable –
225   some simplifications are only valid for newer versions of Go
226   and deprecated functions may not have had viable alternatives in older versions.
227 </p>
228
229 <p>
230   To target a specific Go version you can use the <code>-go</code> command line flag.
231   For example, with <code>-go 1.6</code>, only suggestions that are valid for Go 1.6 will be made.
232 </p>
233
234 <h2 id="ignoring-problems">Ignoring problems</h2>
235
236 <p>
237   In general, you shouldn't have to ignore problems reported by staticcheck.
238   Great care is taken to minimize the number of false positives and subjective suggestions.
239   Dubious code should be rewritten and genuine false positives should be reported so that they can be fixed.
240 </p>
241
242 <p>
243   The reality of things, however, is that not all corner cases can be taken into consideration.
244   Sometimes code just has to look weird enough to confuse tools,
245   and sometimes suggestions, though well-meant, just aren't applicable.
246   For those rare cases, there are several ways of ignoring unwanted problems.
247 </p>
248
249 <h3 id="line-based-linter-directives">Line-based linter directives</h3>
250
251 <p>
252   The most fine-grained way of ignoring reported problems is to annotate the offending lines of code with linter directives.
253 </p>
254
255 <p>
256   The <code>//lint:ignore Check1[,Check2,...,CheckN] reason</code> directive
257   ignores one or more checks on the following line of code.
258   The <code>reason</code> is a required field
259   that must describe why the checks should be ignored for that line of code.
260   This field acts as documentation for other people (including future you) reading the code.
261 </p>
262
263 <p>
264   Let's consider the following example,
265   which intentionally checks that the results of two identical function calls are not equal:
266
267   <pre><code>func TestNewEqual(t *testing.T) {
268   if errors.New("abc") == errors.New("abc") {
269     t.Errorf(`New("abc") == New("abc")`)
270   }
271 }</code></pre>
272 </p>
273
274 <p>
275   {{ check "SA4000" }} of staticcheck
276   will flag this code,
277   pointing out that the left and right side of <code>==</code> are identical –
278   usually indicative of a typo and a bug.
279 </p>
280
281 <p>
282   To silence this problem, we can use a linter directive:
283
284   <pre><code>func TestNewEqual(t *testing.T) {
285   //lint:ignore SA4000 we want to make sure that no two results of errors.New are ever the same
286   if errors.New("abc") == errors.New("abc") {
287     t.Errorf(`New("abc") == New("abc")`)
288   }
289 }</code></pre>
290 </p>
291
292 <h4>Maintenance of linter directives</h4>
293
294 <p>
295   It is crucial to update or remove outdated linter directives when code has been changed.
296   Staticcheck helps you with this by making unnecessary directives a problem of its own.
297   For example, for this (admittedly contrived) snippet of code
298
299   <pre><code>//lint:ignore SA1000 we love invalid regular expressions!
300 regexp.Compile(".+")</code></pre>
301
302   staticcheck will report the following:
303
304   <pre><code>tmp.go:1:2: this linter directive didn't match anything; should it be removed?</code></pre>
305 </p>
306
307 <p>
308   Checks that have been disabled via configuration files
309   will not cause directives to be considered unnecessary.
310 </p>
311
312 <h3 id="file-based-linter-directives">File-based linter directives</h3>
313
314 <p>
315   In some cases, you may want to disable checks for an entire file.
316   For example, code generation may leave behind a lot of unused code,
317   as it simplifies the generation process.
318   Instead of manually annotating every instance of unused code,
319   the code generator can inject a single, file-wide ignore directive to ignore the problem.
320 </p>
321
322 <p>
323   File-based linter directives look a lot like line-based ones:
324
325   <pre><code>//lint:file-ignore U1000 Ignore all unused code, it's generated</code></pre>
326 </p>
327
328 <p>
329   The only difference is that these comments aren't associated with any specific line of code.
330   Conventionally, these comments should be placed near the top of the file.
331 </p>
332
333 <p>
334   Unlike line-based directives, file-based ones will not be flagged for being unnecessary.
335 </p>
336
337 <h2 id="resource-usage">Resource usage</h2>
338
339 <p>
340   Static analysis is a rather resource intensive process,
341   having to apply expensive algorithms to a lot of data.
342   Depending on the complexity of the checked code,
343   this can result in many gigabytes of memory usage and minutes (if not hours) of CPU time.
344 </p>
345
346 <p>
347   To combat the time complexity of static analysis, staticcheck makes use of caching.
348   It reuses Go's build cache as well as its own facts cache to avoid analysing dependencies whenever possible.
349   In development environments, there is usually nothing to do to benefit from these caches.
350   In CI, however, you have to ensure that the caches persist across successive runs of CI.
351   The build cache and fact cache are stored beneath the <code>os.UserCacheDir()</code> directory, in <code>go-build</code> and <code>staticcheck</code> respectively.
352   On Linux, by default, these directories can be found in <code>~/.cache/go-build</code> and <code>~/.cache/staticcheck</code>.
353 </p>
354
355 <p>
356   The overall memory consumption of staticcheck is controlled by the degree of parallelism.
357   The more CPU cores a system has available, the more packages will be checked in parallel, increasing the total amount of memory needed.
358   Staticcheck respects the <code>GOMAXPROCS</code> environment variable to control the degree of parallelism.
359 </p>
360
361 <p>
362   Note that reducing <code>GOMAXPROCS</code> only benefits systems with a lot of cores and code bases with a lot of packages.
363   As <code>GOMAXPROCS</code> approaches 1, peak memory usage will be dominated by the most complex package in the code base.
364   Additionally, smaller code bases may have such interconnected dependencies that peak parallelism is never reached, or there may simply be fewer packages than cores.
365   For example, when checking 10 packages it makes no difference if GOMAXPROCS is set to 32 or 16, at most 10 packages can be processed in parallel.
366 </p>
367
368 <p>
369   Furthermore, a certain amount of type information per package needs to be retained until the end of the process,
370   which means that overall memory usage grows with the number of checked packages.
371   You can reduce this effect by disabling the U1000 and U1001 checks via the <code>-checks</code> command line flag (e.g. via <code>staticcheck -checks="inherit,-U1000,-U1001"</code>).
372 </p>
373
374 <p>
375   Finally, you can trade execution time for memory usage by setting the <code>GOGC</code> environment variable to a value less than 100.
376   This will run more frequent garbage collection, potentially lowering peak memory usage, at the cost of spending more CPU.
377 </p>
378
379
380 <h2>Checks</h2>
381
382 A list of all checks can be found on the <a href="/docs/checks">Checks</a> page.