| 1 |
--- |
| 2 |
title: Staticcheck 2020.1 release notes |
| 3 |
linkTitle: 2020.1 |
| 4 |
weight: -4 |
| 5 |
--- |
| 6 |
|
| 7 |
## Introduction to Staticcheck 2020.1 {#introduction} |
| 8 |
|
| 9 |
Staticcheck 2020.1 introduces UI improvements, speed enhancements, and |
| 10 |
a number of [new](#checks-new) as well as [improved](#checks-changed) checks. Additionally, it is the |
| 11 |
first release to support the upcoming Go 1.14. |
| 12 |
|
| 13 |
## UI improvements {#ui-improvements} |
| 14 |
|
| 15 |
We've improved the output of the `staticcheck` command as well as |
| 16 |
Staticcheck's integration with [gopls](https://github.com/golang/tools/tree/master/gopls) to make it easier to understand |
| 17 |
the problems that are being reported. |
| 18 |
|
| 19 |
Related information describes the source of a problem, or why |
| 20 |
Staticcheck believes that there is a problem. Take the following |
| 21 |
piece of code for example: |
| 22 |
|
| 23 |
```go |
| 24 |
func fn(x *int) { |
| 25 |
if x == nil { |
| 26 |
log.Println("x is nil, returning") |
| 27 |
} |
| 28 |
// lots of code here |
| 29 |
log.Println(*x) |
| 30 |
} |
| 31 |
``` |
| 32 |
|
| 33 |
Staticcheck 2020.1 will produce the following output: |
| 34 |
|
| 35 |
```text |
| 36 |
foo.go:6:14: possible nil pointer dereference (SA5011) |
| 37 |
foo.go:2:5: this check suggests that the pointer can be nil |
| 38 |
``` |
| 39 |
|
| 40 |
The actual problem that is being reported is the "possible nil pointer |
| 41 |
dereference". Staticcheck also explains why it believes that `x` might |
| 42 |
be nil, namely the comparison on line 2. |
| 43 |
|
| 44 |
When using the [`text`]({{< relref "/docs/running-staticcheck/cli/formatters#text" >}}) or [`stylish`]({{< relref "/docs/running-staticcheck/cli/formatters#stylish" >}}) formatters, related information will |
| 45 |
appear as indented lines. The [`json`]({{< relref "/docs/running-staticcheck/cli/formatters#json" >}}) formatter adds a new field |
| 46 |
`related` to problems, containing position information as well as the |
| 47 |
message. Editors that use gopls will also display the related |
| 48 |
information. |
| 49 |
|
| 50 |
Related information should make it easier to understand why Staticcheck |
| 51 |
is flagging code, and how to fix problems. |
| 52 |
|
| 53 |
Integration with gopls has seen some other improvements as well¹. We |
| 54 |
now emit better position information that more accurately reflects the |
| 55 |
true source of a problem. The most obvious example is that a missing |
| 56 |
package comment will no longer underline the entire file. Similarly, |
| 57 |
invalid function arguments will be highlighted individually, instead |
| 58 |
of highlighting the call as a whole. Finally, some problems can now be |
| 59 |
automatically fixed by using quick fixes. |
| 60 |
|
| 61 |
¹: due to the nature of Staticcheck's integration with gopls, gopls |
| 62 |
will need to update their dependency on Staticcheck before benefiting |
| 63 |
from these changes. |
| 64 |
|
| 65 |
## Better caching {#caching} |
| 66 |
|
| 67 |
The 2019.2 release introduced caching to Staticcheck, greatly speeding |
| 68 |
up repeated runs. However, the caching only applied to dependencies; |
| 69 |
the packages under analysis still had to be analyzed anew on every |
| 70 |
invocation to compute the list of problems. Staticcheck 2020.1 |
| 71 |
introduces caching of problems found, so that repeat runs for |
| 72 |
unchanged packages are virtually instantaneous. |
| 73 |
|
| 74 |
## Checks {#checks} |
| 75 |
### New checks {#checks-new} |
| 76 |
|
| 77 |
Numerous new checks have been added in this release: |
| 78 |
|
| 79 |
- {{< check "S1035" >}} flags redundant calls to `net/http.CanonicalHeaderKey`. |
| 80 |
- {{< check "S1036" >}} flags unnecessary guards around map accesses. |
| 81 |
- {{< check "S1037" >}} flags unnecessarily elaborate ways of sleeping. |
| 82 |
- {{< check "S1038" >}} flags unnecessary uses of `fmt.Sprintf`, such as `fmt.Println(fmt.Sprintf(...))`. |
| 83 |
- {{< check "S1039" >}} flags uses of `fmt.Sprint` with single string literals. |
| 84 |
- {{< check "SA1028" >}} flags uses of `sort.Slice` on non-slices. |
| 85 |
- {{< check "SA1029" >}} flags inappropriate keys in calls to context.WithValue. |
| 86 |
- {{< check "SA4022" >}} flags comparisons of the kind `if &x == nil`. |
| 87 |
- {{< check "SA5010" >}} flags impossible type assertions. |
| 88 |
- {{< check "SA5011" >}} flags potential nil pointer dereferences. |
| 89 |
- {{< check "ST1019" >}} flags duplicate imports. |
| 90 |
- {{< check "ST1020" >}} checks the documentation of exported functions. |
| 91 |
- {{< check "ST1021" >}} checks the documentation of exported types. |
| 92 |
- {{< check "ST1022" >}} checks the documentation of exported variables and constants. |
| 93 |
|
| 94 |
{{< check "ST1020" >}}, {{< check "ST1021" >}} and {{< check "ST1022" >}} are not enabled by default. |
| 95 |
|
| 96 |
### Changed checks {#checks-changed} |
| 97 |
|
| 98 |
Several checks have been improved: |
| 99 |
|
| 100 |
- {{< check "S1036" >}} detects more kinds of unnecessary guards around map accesses. |
| 101 |
- {{< check "S1008" >}} reports more easily understood diagnostics. |
| 102 |
- {{< check "S1025" >}} no longer suggests using `v.String()` instead of `fmt.Sprintf("%s", v)` when `v` is a `reflect.Value`. `fmt` gives special treatment to `reflect.Value` and the two results differ. |
| 103 |
- {{< check "SA1015" >}} no longer flags uses of `time.Tick` in packages that implement [Cobra](https://github.com/spf13/cobra) commands. |
| 104 |
- {{< check "SA1019" >}} no longer misses references to deprecated packages when said packages have been vendored. |
| 105 |
- {{< check "SA4000" >}} no longer flags comparisons of the kind `x == x` and `x != x` when `x` has a compound type involving floats. |
| 106 |
- {{< check "SA4003" >}} no longer flags comparisons of the kind `x <= 0` when `x` is an unsigned integer. While it is true that `x <= 0` can be written more specifically as `x == 0`, this is not a helpful suggestion in reality. A lot of people use `x <= 0` as a defensive measure, in case `x` ever becomes signed. Also, unlike all the other warnings made in the check, `x <= 0` is neither a tautology nor a contradiction, it is merely less precise than it could be. |
| 107 |
- {{< check "SA4016" >}} now detects silly bitwise ops of the form `x & k` where `k` is defined as `const k = iota`. |
| 108 |
- {{< check "SA4018" >}} no longer flags self-assignments involving side effects; for example, it won't flag `x[fn()] = x[fn()]` if `fn` isn't pure. |
| 109 |
- {{< check "SA5008" >}} now permits duplicate instances of various struct tags used by `github.com/jessevdk/go-flags`. |
| 110 |
- {{< check "SA5009" >}} now correctly recognizes that `unsafe.Pointer` is a pointer type that can be used with verbs such as `%p`. Furthermore, it validates calls to `golang.org/x/xerrors.Errorf`. |
| 111 |
- {{< check "SA5009" >}} now understands `fmt.Printf` verbs that were changed and added in Go 1.13. Specifically, it now recognizes the new `%O` verb, and allows the use of `%x` and `%X` on floats and complex numbers. |
| 112 |
- {{< check "ST1003" >}} has learned about several new initialisms. |
| 113 |
- {{< check "ST1011" >}} no longer misses variable declarations with inferred types. |
| 114 |
- {{< check "ST1016" >}} now ignores the names of method receivers of methods declared in generated files. |
| 115 |
- {{< check "ST1020" >}}, {{< check "ST1021" >}}, and {{< check "ST1022" >}} no longer enforce comment style in generated code. |
| 116 |
|
| 117 |
## General bug fixes {#bugs} |
| 118 |
|
| 119 |
The following bugs were fixed: |
| 120 |
|
| 121 |
- A race condition in the {{< check "U1000" >}} check could occasionally lead to sporadic false positives. |
| 122 |
- Some files generated by _goyacc_ weren't recognized as being generated. |
| 123 |
- `staticcheck` no longer fails to check packages that consist exclusively of tests. |
| 124 |
|
| 125 |
|
| 126 |
## Staticcheck 2020.1.1 release notes {#2020.1.1} |
| 127 |
|
| 128 |
The 2020.1 release neglected to update the version string stored in |
| 129 |
the binary, causing `staticcheck -version` to incorrectly emit `(no version)`. |
| 130 |
|
| 131 |
## Staticcheck 2020.1.2 release notes {#2020.1.2} |
| 132 |
|
| 133 |
The 2020.1.1 release incorrectly identified itself as version 2020.1. |
| 134 |
|
| 135 |
## Staticcheck 2020.1.3 release notes {#2020.1.3} |
| 136 |
|
| 137 |
This release fixes two bugs involving `//lint:ignore` directives: |
| 138 |
|
| 139 |
- When ignoring U1000 and checking a package that contains tests, |
| 140 |
Staticcheck would incorrectly complain that the linter directive |
| 141 |
didn't match any problems, even when it did. |
| 142 |
- On repeated runs, the position information for a <q>this linter directive didn't match anything</q> report |
| 143 |
would either be missing, or be wildly incorrect. |
| 144 |
|
| 145 |
## Staticcheck 2020.1.4 release notes {#2020.1.4} |
| 146 |
|
| 147 |
This release adds special handling for imports of the |
| 148 |
deprecated `github.com/golang/protobuf/proto` package. |
| 149 |
|
| 150 |
[github.com/golang/protobuf](https://github.com/golang/protobuf) |
| 151 |
has deprecated the `proto` package, but |
| 152 |
their `protoc-gen-go` still imports the package and uses |
| 153 |
one of its constants, <q>to enforce a weak dependency on a |
| 154 |
sufficiently new version of the legacy package</q>. |
| 155 |
|
| 156 |
Staticcheck would flag the import of this deprecated package in all |
| 157 |
code generated by protoc-gen-go. Instead of forcing the project to |
| 158 |
change their project structure, we choose to ignore such imports in |
| 159 |
code generated by protoc-gen-go. The import still gets flagged in code |
| 160 |
not generated by protoc-gen-go. |
| 161 |
|
| 162 |
You can find more information about this in the [upstream issue](https://github.com/golang/protobuf/issues/1077). |
| 163 |
|
| 164 |
## Staticcheck 2020.1.5 release notes {#2020.1.5} |
| 165 |
|
| 166 |
This release fixes a [crash in the pattern matching engine]({{< issueref 806 >}}) |
| 167 |
and a [false positive in SA4006]({{< issueref 733 >}}). |
| 168 |
|
| 169 |
## Staticcheck 2020.1.6 release notes {#2020.1.6} |
| 170 |
|
| 171 |
This release makes the following fixes and improvements: |
| 172 |
|
| 173 |
- Staticcheck no longer panics when encountering files that have the following comment: `// Code generated DO NOT EDIT.` |
| 174 |
- {{< check "SA4016" >}} no longer panics when checking bitwise operations that involve dot-imported identifiers. |
| 175 |
- Fixed the suggested fix offered by {{< check "S1004" >}}. |
| 176 |
- Fixed a false positive involving byte arrays in {{< check "SA5009" >}}. |
| 177 |
- Fixed a false positive involving named byte slice types in {{< check "SA5009" >}}. |
| 178 |
- Added another heuristic to avoid flagging function names in error messages in {{< check "ST1005" >}}. |
| 179 |
- {{< check "SA3000" >}} will no longer flag missing calls to `os.Exit` in `TestMain` functions if targeting Go 1.15 or newer. |
| 180 |
|
| 181 |
|