| 1 |
--- |
| 2 |
title: Staticcheck 2019.2 release notes |
| 3 |
linkTitle: 2019.2 |
| 4 |
weight: -3 |
| 5 |
--- |
| 6 |
|
| 7 |
|
| 8 |
## Performance improvements {#performance} |
| 9 |
|
| 10 |
Staticcheck 2019.2 brings major performance improvements and a |
| 11 |
reduction in memory usage. |
| 12 |
|
| 13 |
Staticcheck has been redesigned to only keep those packages in memory that are actively being processed. |
| 14 |
This allows for much larger workspaces to be checked in one go. |
| 15 |
While previously it may have been necessary to split a list of packages into many invocations of `staticcheck`, |
| 16 |
this is now handled intelligently and efficiently by Staticcheck itself. |
| 17 |
|
| 18 |
In particular, memory usage is now closely tied to parallelism: |
| 19 |
having more CPU cores available allows for more packages to be processed in parallel, |
| 20 |
which increases the number of packages held in memory at any one time. |
| 21 |
Not only does this make good use of available resources – |
| 22 |
systems with more CPU cores also tend to have more memory available – |
| 23 |
it also exposes a single, easy to use knob for trading execution time for memory use. |
| 24 |
By setting `GOMAXPROCS` to a value lower than the number of available cores, |
| 25 |
memory usage of Staticcheck will be reduced, at the cost of taking longer to complete. |
| 26 |
|
| 27 |
We've observed reductions in memory usage of 2x to 8x when checking large code bases. |
| 28 |
|
| 29 |
<table class="table"> |
| 30 |
<thead> |
| 31 |
<tr> |
| 32 |
<th>Package</th> |
| 33 |
<th>2019.1.1</th> |
| 34 |
<th>2019.2¹</th> |
| 35 |
<th>Change</th> |
| 36 |
</tr> |
| 37 |
</thead> |
| 38 |
|
| 39 |
<tr> |
| 40 |
<td>net/http</td> |
| 41 |
<td>3.543 s / 677 MB</td> |
| 42 |
<td>3.747 s / 254 MB</td> |
| 43 |
<td>+5.76% / -62.48%</td> |
| 44 |
</tr> |
| 45 |
|
| 46 |
<tr> |
| 47 |
<td>strconv</td> |
| 48 |
<td>1.628 s / 294 MB</td> |
| 49 |
<td>1.678 s / 118 MB</td> |
| 50 |
<td>+3.07% / -59.86%</td> |
| 51 |
</tr> |
| 52 |
|
| 53 |
<tr> |
| 54 |
<td>image/color</td> |
| 55 |
<td>1.304 s / 225 MB</td> |
| 56 |
<td>1.702 s / 138 MB</td> |
| 57 |
<td>+30.52% / -38.67%</td> |
| 58 |
</tr> |
| 59 |
|
| 60 |
<tr> |
| 61 |
<td>std</td> |
| 62 |
<td>26.234 s / 3987 MB</td> |
| 63 |
<td>19.444 s / 1054 MB</td> |
| 64 |
<td>-25.88% / -73.56%</td> |
| 65 |
</tr> |
| 66 |
|
| 67 |
<tr> |
| 68 |
<td>github.com/cockroachdb/cockroach/pkg/...</td> |
| 69 |
<td>88.644 s / 15959 MB</td> |
| 70 |
<td>93.798 s / 4156 MB</td> |
| 71 |
<td>+5.81% / -73.96%</td> |
| 72 |
</tr> |
| 73 |
|
| 74 |
<tfoot> |
| 75 |
<tr> |
| 76 |
<td colspan="4"> |
| 77 |
¹: The fact cache was empty for all benchmarks. |
| 78 |
</td> |
| 79 |
</tr> |
| 80 |
</tfoot> |
| 81 |
</table> |
| 82 |
|
| 83 |
In addition, Staticcheck now employs caching to speed up repeated checking of packages. |
| 84 |
In the past, when checking a package, all of its dependencies had to be loaded from source and analyzed. |
| 85 |
Now, we can make use of Go's build cache, as well as cache our own analysis facts. |
| 86 |
This makes Staticcheck behave a lot more like `go build`, where repeated builds are much faster. |
| 87 |
|
| 88 |
| Package | Uncached | Cached | Change | |
| 89 |
|------------------------------------------|--------------------|--------------------|------------------------| |
| 90 |
| net/http | 3.747 s / 254 MB | 1.545 s / 195 MB | -58.77% / -23.23% | |
| 91 |
| strconv | 1.678 s / 118 MB | 0.495 s / 57 MB | -70.5% / -51.69% | |
| 92 |
| image/color | 1.702 s / 138 MB | 0.329 s / 31 MB | -80.67% / -77.54% | |
| 93 |
| std | 19.444 s / 1054 MB | 15.099 s / 887 MB | -22.35% / -15.84% | |
| 94 |
| github.com/cockroachdb/cockroach/pkg/... | 93.798 s / 4156 MB | 47.205 s / 2516 MB | -49.67% / -39.46% | |
| 95 |
|
| 96 |
This combination of improvements not only compensates for the |
| 97 |
increased memory usage that 2019.1 introduced, it also brings the |
| 98 |
memory usage and execution times way below the levels of those seen in the |
| 99 |
2017.2 release, which had previously been our most efficient |
| 100 |
release. |
| 101 |
|
| 102 |
It should be noted that all of these improvements are part of the `staticcheck` command itself, not the individual checks. |
| 103 |
Tools such as golangci-lint will have to replicate our efforts to benefit from these improvements. |
| 104 |
|
| 105 |
## The go/analysis framework {#go-analysis} |
| 106 |
|
| 107 |
Part of the redesign of Staticcheck involved porting our code to the [go/analysis](https://godoc.org/golang.org/x/tools/go/analysis) framework. |
| 108 |
|
| 109 |
The go/analysis framework is a framework for writing static analysis tools such as Staticcheck and go vet. |
| 110 |
It provides an API that enables interoperability between different analyses and analysis drivers – drivers being the code that actually executes analyses. |
| 111 |
The intention is that any driver can trivially use any analysis that is implemented using go/analysis. |
| 112 |
|
| 113 |
With the exception of {{< check "U1000" >}}, all of our checks are now go/analysis analyses. Furthermore, the `staticcheck` command is now a go/analysis driver. |
| 114 |
|
| 115 |
With our move to this framework, we enable other drivers to reuse our checks without having to patch them. |
| 116 |
This should be of particular interest to golangci-lint, which previously took to patching Staticcheck, sometimes in subtly incorrect ways. |
| 117 |
Another high-profile go/analysis driver is gopls, the Go language server. It will now be much easier for gopls to use Staticcheck to analyze code, should it so desire. |
| 118 |
|
| 119 |
Theoretically it would also allow us to use third-party analyses as part of Staticcheck. |
| 120 |
Due to quality control reasons, however, we will likely refrain from doing so. |
| 121 |
Nonetheless it would be trivial for users to maintain internal forks of `cmd/staticcheck` that use third-party analyses. |
| 122 |
|
| 123 |
## Improvements to the CLI {#cli} |
| 124 |
|
| 125 |
We've made several minor improvements to the command-line interface of `staticcheck` that improve usability and debuggability. |
| 126 |
|
| 127 |
### SIGINFO handler {#cli-siginfo} |
| 128 |
|
| 129 |
Upon receiving the SIGINFO signal – or SIGUSR1 on platforms that lack |
| 130 |
SIGINFO – Staticcheck will dump statistics, such as the current phase |
| 131 |
and how many packages are left to analyze. |
| 132 |
|
| 133 |
```text |
| 134 |
Packages: 37/619 initial, 38/1011 total; Workers: 8/8; Problems: 73 |
| 135 |
``` |
| 136 |
|
| 137 |
### Explaining checks {#cli-explain} |
| 138 |
|
| 139 |
Using the new `-explain` flag, a check's documentation can be displayed right in the terminal, |
| 140 |
eliminating the need to visit the website. |
| 141 |
|
| 142 |
```text |
| 143 |
$ staticcheck -explain S1007 |
| 144 |
Simplify regular expression by using raw string literal |
| 145 |
|
| 146 |
Raw string literals use ` instead of " and do not support |
| 147 |
any escape sequences. This means that the backslash (\) can be used |
| 148 |
freely, without the need of escaping. |
| 149 |
|
| 150 |
Since regular expressions have their own escape sequences, raw strings |
| 151 |
can improve their readability. |
| 152 |
|
| 153 |
Before: |
| 154 |
|
| 155 |
regexp.Compile("\\A(\\w+) profile: total \\d+\\n\\z") |
| 156 |
|
| 157 |
After: |
| 158 |
|
| 159 |
regexp.Compile(`\A(\w+) profile: total \d+\n\z`) |
| 160 |
|
| 161 |
Available since |
| 162 |
2017.1 |
| 163 |
``` |
| 164 |
|
| 165 |
### -debug.version {#cli-debug-version} |
| 166 |
|
| 167 |
The `-debug.version` flag causes `staticcheck` to print |
| 168 |
detailed version information, such as the Go version used to compile |
| 169 |
it, as well as the versions of all dependencies if built using Go |
| 170 |
modules. This feature is intended for debugging issues, and we will |
| 171 |
ask for its output from users who file issues. |
| 172 |
|
| 173 |
```text |
| 174 |
$ staticcheck -debug.version |
| 175 |
staticcheck (devel, v0.0.0-20190602125119-5a4a2f4a438d) |
| 176 |
|
| 177 |
Compiled with Go version: go1.12.5 |
| 178 |
Main module: |
| 179 |
honnef.co/go/[email protected] (sum: h1:U5vSGN1Bjr0Yd/4pRcp8iRUCs3S5TIPzoAeTEFV2aiU=) |
| 180 |
Dependencies: |
| 181 |
github.com/BurntSushi/[email protected] (sum: h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=) |
| 182 |
golang.org/x/[email protected] (sum: h1:mnQlcVx8Qq8L70HV0DxUGuiuAtiEHTwF1gYJE/EL9nU=) |
| 183 |
``` |
| 184 |
|
| 185 |
### Enabling unused's whole program mode {#cli-unused} |
| 186 |
|
| 187 |
When we merged `unused` into `staticcheck`, we lost the ability to specify the `-exported` flag to report unused exported identifiers. |
| 188 |
Staticcheck 2019.2 restores this ability with the new `-unused.whole-program` flag. |
| 189 |
|
| 190 |
### Range information in diagnostics {#cli-ranges} |
| 191 |
|
| 192 |
Many of our checks now emit `[start, end]` ranges for findings instead of just positions. |
| 193 |
These ranges can be accessed via the `json` output formatter, as well as by using `go/analysis.Diagnostic` directly, such as in gopls. |
| 194 |
|
| 195 |
Note that not all checks are able to emit range information. |
| 196 |
|
| 197 |
## Installing Staticcheck as a module {#module} |
| 198 |
|
| 199 |
As part of the 2019.2 release, we've turned Staticcheck into a Go module. |
| 200 |
From now on, if using Go modules, you can install specific versions of Staticcheck with `go get honnef.co/go/tools/cmd/staticcheck@<version>`, |
| 201 |
though do note that older releases do not have a `go.mod` file. |
| 202 |
You can still download them as modules, but Go will record indirect dependencies in the main module's `go.mod` file, and no minimum versions are specified. |
| 203 |
|
| 204 |
Staticcheck will not use Semantic Versioning for its releases. |
| 205 |
It is our belief that Semver is a poor fit for applications and is more suited towards libraries. |
| 206 |
For example, almost every release of Staticcheck has backwards incompatible changes to some APIs that aren't meant for public consumption, |
| 207 |
but which we expose nevertheless so that tinkerers can use them. |
| 208 |
|
| 209 |
However, we use so-called _pre-release versions_ of the form `v0.0.0-2019.2`. |
| 210 |
These allow us to embed our versioning scheme in that of Semver, with correct sorting and updating of versions. |
| 211 |
Furthermore, these versions ensure that `go get ...`, |
| 212 |
if not specifying an explicit version (that is, if using the query `latest`), |
| 213 |
will install the latest released version of Staticcheck and not the master branch. |
| 214 |
|
| 215 |
While you can use these pre-release version numbers directly, you can also use the canonical versions of the form `2019.2` instead. |
| 216 |
The Go tool will automatically translate these versions to the appropriate pre-releases. |
| 217 |
|
| 218 |
To install the master branch, use `go get honnef.co/go/tools/cmd/staticcheck@master` |
| 219 |
|
| 220 |
## Removal of deprecated functionality {#deprecated} |
| 221 |
|
| 222 |
Staticcheck 2019.1 deprecated the `unused`, `gosimple`, and `megacheck` |
| 223 |
utilities, as they have been merged into `staticcheck`. Furthermore, it deprecated the `-ignore` flag, |
| 224 |
which has been replaced by [linter directives]({{< relref "/docs/#ignoring-problems" >}}). |
| 225 |
|
| 226 |
This release no longer includes these deprecated utilities, nor does |
| 227 |
it provide the deprecated flag. |
| 228 |
|
| 229 |
## Checks {#checks} |
| 230 |
|
| 231 |
### New checks {#checks-new} |
| 232 |
|
| 233 |
Numerous new checks have been added in this release: |
| 234 |
|
| 235 |
- {{< check "S1033" >}} flags unnecessary guards around calls to `delete`. |
| 236 |
- {{< check "S1034" >}} simplifies type switches involving redundant type assertions. |
| 237 |
- {{< check "SA1026" >}} flags attempts at marshaling invalid types. |
| 238 |
- {{< check "SA1027" >}} flags incorrectly aligned atomic accesses. |
| 239 |
- {{< check "SA4020" >}} flags unreachable case clauses in type switches. |
| 240 |
- {{< check "SA4021" >}} flags calls to append with a single argument, as `x = append(y)` is equivalent to `x = y`. |
| 241 |
- {{< check "SA5008" >}} flags certain kinds of invalid struct tags. |
| 242 |
- {{< check "SA5009" >}} verifies the correctness of Printf calls. |
| 243 |
- {{< check "SA6005" >}} flags inefficient string comparisons involving `strings.ToLower` |
| 244 |
or `strings.ToUpper` when they can be replaced with `strings.EqualFold`. |
| 245 |
- {{< check "SA9005" >}} flags attempts at marshaling structs with no public fields nor custom marshaling. |
| 246 |
- {{< check "ST1017" >}} flags so-called [yoda conditions](https://en.wikipedia.org/wiki/Yoda_conditions), |
| 247 |
which take the form of `if 42 == x`. |
| 248 |
- {{< check "ST1018" >}} flags string literals containing zero-width characters. |
| 249 |
|
| 250 |
|
| 251 |
### Changed checks {#checks-changed} |
| 252 |
|
| 253 |
Several checks have been improved: |
| 254 |
|
| 255 |
- {{< check "SA1019" >}} now flags imports of deprecated packages. |
| 256 |
- {{< check "SA4000" >}} no longer flags comparisons between custom float types. Additionally, it avoids a false positive caused by cgo. |
| 257 |
- {{< check "SA4006" >}} no longer flags unused values in code generated by goyacc. This avoids noise caused by the nature of the generated state machine. |
| 258 |
- {{< check "ST1005" >}} no longer flags error messages that start with capitalized type names. |
| 259 |
- {{< check "ST1006" >}} no longer flags receiver names in generated code. |
| 260 |
- {{< check "SA5002" >}} no longer suggests replacing `for false {` with `for {`. |
| 261 |
- Added "SIP" and "RTP" as default initialisms to {{< check "ST1003" >}}. |
| 262 |
- {{< check "SA1006" >}}, {{< check "SA4003" >}}, {{< check "S1017" >}}, and {{< check "S1020" >}} match more code patterns. |
| 263 |
- {{< check "S1021" >}} is less eager to merge declarations and assignments when multiple assignments are involved. |
| 264 |
- {{< check "U1000" >}} has been rewritten, eliminating a variety of false positives. |
| 265 |
|
| 266 |
## Sustainable open source and a personal plea {#sustainable-open-source} |
| 267 |
|
| 268 |
Staticcheck is an open source project developed primarily by me, Dominik Honnef, in my free time. |
| 269 |
While this model of software development has gotten increasingly common, it is not very sustainable. |
| 270 |
Time has to be split between open source work and paid work to sustain one's life. |
| 271 |
This is made especially unfortunate by the fact that hundreds of companies rely on open source each day, |
| 272 |
but few consider giving back to it, even though it would directly benefit their businesses, |
| 273 |
ensuring that the software they rely on keeps being developed. |
| 274 |
|
| 275 |
I have long been soliciting donations for Staticcheck [on Patreon](https://www.patreon.com/dominikh) to make its development more sustainable. |
| 276 |
A fair number of individuals have generously pledged their support and I am very grateful to them. |
| 277 |
Unfortunately, only few companies support Staticcheck's development, and I'd like for that to change. |
| 278 |
|
| 279 |
To people who are familiar with Patreon, it might've always seemed like an odd choice for a software project. |
| 280 |
Patreon focuses on art and creative work, and on individuals supporting said work, not companies. |
| 281 |
I am therefore excited to announce my participation in [GitHub Sponsors](https://github.com/sponsors), |
| 282 |
a new way of supporting developers, directly on GitHub. |
| 283 |
|
| 284 |
GitHub Sponsors allows you to easily support developers by sponsoring them on a monthly basis, [via a few simple clicks.](https://github.com/users/dominikh/sponsorship) |
| 285 |
It is fully integrated with the platform and can use your existing billing information, making it an effortless process. |
| 286 |
**To encourage more company sponsorships I offer to display your company's logo prominently on |
| 287 |
[Staticcheck's website](https://staticcheck.dev/)** |
| 288 |
for |
| 289 |
[$250 USD a month](https://github.com/users/dominikh/sponsorship?utf8=%E2%9C%93&tier_id=MDIyOk1hcmtldHBsYWNlTGlzdGluZ1BsYW4yNTAy&editing=false), |
| 290 |
to show my appreciation for your contribution and to show to the world how much you care about code quality. |
| 291 |
|
| 292 |
Please don't hesitate [contacting me directly](mailto:[email protected]) if neither GitHub Sponsors nor Patreon seem suitable to you but you'd like to support me nevertheless. |
| 293 |
I am sure we can work something out. |
| 294 |
|
| 295 |
## Staticcheck 2019.2.1 release notes {#2019.2.1} |
| 296 |
|
| 297 |
The 2019.2 release has an unfortunate bug that prevents Staticcheck from running on 32-bit architectures, causing it to crash unconditionally. |
| 298 |
This release fixes that crash. |
| 299 |
|
| 300 |
## Staticcheck 2019.2.2 release notes {#2019.2.2} |
| 301 |
|
| 302 |
Staticcheck 2019.2.2 contains the following user-visible fixes: |
| 303 |
|
| 304 |
- {{< check "S1008" >}} now skips if/else statements where both branches return the same value. |
| 305 |
- {{< check "SA4006" >}} now considers a value read when a switch statement reads it, even if the switch statement has no branches. |
| 306 |
- 2019.2 introduced a bug that made it impossible to enable non-default checks via configuration files. This is now possible again. |
| 307 |
- 2019.2 introduced a bug that made the `-tags` command line argument ineffective, making it impossible to pass in build tags. This is now possible again. |
| 308 |
- From this release onward, we will use pseudo versions of the form |
| 309 |
`v0.0.1-<year>.<minor>` instead of `v0.0.0-<year>.<minor>`. |
| 310 |
This fixes an issue where `go get` would prefer an older commit over a newer released version due to the way versions sort. |
| 311 |
|
| 312 |
## Staticcheck 2019.2.3 release notes {#2019.2.3} |
| 313 |
|
| 314 |
Staticcheck 2019.2.3 is a re-release of 2019.2.2. |
| 315 |
Its pre-built binaries, which can be found on GitHub, |
| 316 |
have been built with Go 1.13, to enable checking of code that uses language features introduced in Go 1.13. |