| 1 |
--- |
| 2 |
title: Staticcheck 2024.1 release notes |
| 3 |
linkTitle: "2024.1 (v0.5.0)" |
| 4 |
weight: -9 |
| 5 |
--- |
| 6 |
|
| 7 |
## Backwards incompatible changes |
| 8 |
|
| 9 |
Staticcheck 2024.1 contains the following backwards incompatible changes: |
| 10 |
|
| 11 |
- The `keyify` utility has been removed. The recommended alternative is gopls. |
| 12 |
- `staticcheck -merge` now exits with a non-zero status if any problems have |
| 13 |
been found. |
| 14 |
|
| 15 |
## Improved Go 1.22 support |
| 16 |
|
| 17 |
This release updates Staticcheck's database of deprecated standard library APIs |
| 18 |
to cover the Go 1.22 release. Furthermore, checks have been updated to correctly |
| 19 |
handle the new "for" loop variable scoping behavior as well as ranging over |
| 20 |
integers. |
| 21 |
|
| 22 |
## Added Go 1.23 support |
| 23 |
|
| 24 |
Staticcheck 2024.1 has full support for iterators / range-over-func. |
| 25 |
Furthermore, {{< check "SA1015" >}} will skip any code targeting Go 1.23 or |
| 26 |
newer, as it is now possible to use `time.Tick` without leaking memory. |
| 27 |
|
| 28 |
## Improved handling of Go versions |
| 29 |
|
| 30 |
Go 1.21 more rigorously defined the meaning of the `go` directive in `go.mod` |
| 31 |
files, as well as its interactions with `//go:build go1.N` build constraints. |
| 32 |
The `go` directive now specifies a _minimum_ Go version for the module. |
| 33 |
Furthermore, it sets the language version that is in effect, which may change |
| 34 |
the semantics of Go code. For example, before Go 1.22, loop variables were |
| 35 |
reused across iterations, but since Go 1.22, loop variables only exist for the |
| 36 |
duration of an iteration. Modules that specify `go 1.22` will use the new |
| 37 |
semantics, while modules that specify an older version will not. |
| 38 |
|
| 39 |
Individual files can both upgrade and downgrade their language version by using |
| 40 |
`//go:build go1.N` directives. In a module that requires Go 1.22, a file |
| 41 |
specifying Go 1.21 will experience the old loop variable semantics, and vice |
| 42 |
versa. Because the Go module as a whole still specifies a minimum version, even |
| 43 |
files specifying an older version will have access to the standard library of |
| 44 |
the minimum version. |
| 45 |
|
| 46 |
Staticcheck 2024.1 takes all of this into consideration when analyzing the |
| 47 |
behavior of Go code, when determining which checks are applicable, and when |
| 48 |
making suggestions. Older versions of Staticcheck were already aware of Go |
| 49 |
versions, but 2024.1 works on a more fine-grained, per-file basis, and |
| 50 |
differentiates between the pre- and post-1.21 semantics of the `go` directive. |
| 51 |
|
| 52 |
The `-go` command line flag continues to exist. It will override any |
| 53 |
module-based version selection. This is primarily useful for Go modules that |
| 54 |
target older Go versions (because here, the `go` directive didn't specify a |
| 55 |
_minimum_ version), or when working outside of Go modules. |
| 56 |
|
| 57 |
To prevent misinterpreting code, Staticcheck now refuses to analyze modules that |
| 58 |
require a version of Go that is newer than that with which Staticcheck was |
| 59 |
built. |
| 60 |
|
| 61 |
## Checks |
| 62 |
|
| 63 |
### New checks |
| 64 |
|
| 65 |
The following checks have been added: |
| 66 |
|
| 67 |
- {{< check "SA1031" >}} flags overlapping destination and source slices passed |
| 68 |
to certain encoding functions. |
| 69 |
- {{< check "SA1032" >}} flags calls to |
| 70 |
[`errors.Is`](https://pkg.go.dev/errors#Is) where the two arguments have been |
| 71 |
swapped. |
| 72 |
- {{< check "SA4032" >}} flags impossible comparisons of |
| 73 |
[runtime.GOOS](https://pkg.go.dev/runtime#GOOS) and |
| 74 |
[runtime.GOARCH](https://pkg.go.dev/runtime#GOARCH) based on the file's build |
| 75 |
tags. |
| 76 |
- {{< check "SA6006" >}} flags `io.WriteString(w, string(b))` as it would be |
| 77 |
both simpler and more efficient to use `w.Write(b)`. |
| 78 |
- {{< check "SA9009" >}} flags comments that look like they intend to be |
| 79 |
compiler directives but which aren't due to extraneous whitespace. |
| 80 |
|
| 81 |
### Changed checks |
| 82 |
|
| 83 |
The following checks have been improved: |
| 84 |
|
| 85 |
- {{< check "QF1001" >}} no longer panics on expressions involving "key: value" |
| 86 |
pairs ({{< issue "1484" >}}). |
| 87 |
- {{< check "S1008" >}} now understands that some built-in functions never |
| 88 |
return negative values. For example, it now negates `len(x) > 0` as `len(x) == |
| 89 |
0` ({{< issue "1422" >}}). |
| 90 |
- {{< check "S1009" >}} now flags unnecessary nil checks that involve selector |
| 91 |
expressions ({{< issue "1527" >}}). |
| 92 |
- {{< check "S1017" >}} no longer flags `if else` branches ({{< issue "1447" |
| 93 |
>}}). |
| 94 |
- {{< check "SA1006" >}} now detects more Printf-like functions from the |
| 95 |
standard library ({{< issue "1528" >}}). |
| 96 |
- {{< check "SA1015" >}} now skips any code targeting Go 1.23 or newer ({{< |
| 97 |
issue "1558" >}}). |
| 98 |
- {{< check "SA1029" >}} now flags uses of the empty struct (`struct{}`) as |
| 99 |
context keys ({{< issue "1504" >}}). |
| 100 |
- {{< check "SA4003" >}} now flags pointless integer comparisons that involve |
| 101 |
literals, not just constants from the `math` package ({{< issue "1470" >}}). |
| 102 |
- {{< check "SA4015" >}} now supports conversions that involve generics. |
| 103 |
- {{< check "SA4023" >}} no longer panics on type sets that contain arrays ({{< |
| 104 |
issue "1397" >}}). |
| 105 |
- {{< check "SA5001" >}} now emits a clearer message ({{< issue "1489" >}}). |
| 106 |
- {{< check "SA9003" >}} has been disabled by default because of too many noisy |
| 107 |
positives ({{< issue "321" >}}). |
| 108 |
- {{< check "ST1000" >}} now permits punctuation following the package name, as |
| 109 |
in "Package pkg, which ..." ({{< issue "1452" >}}). |
| 110 |
- {{< check "ST1018" >}} now accepts variation selectors in emoji and certain |
| 111 |
Arabic formatting characters in string literals ({{< issue "1456" >}}). |
| 112 |
- {{< check "ST1020" >}} no longer flags comments that start with a deprecation |
| 113 |
notice ({{< issue "1378" >}}). |
| 114 |
- {{< check "U1000" >}} handles generic interfaces slightly better, reducing the |
| 115 |
number of false positives. |
| 116 |
- Due to improvements in the intermediate representation, various checks may now |
| 117 |
detect more problems. |
| 118 |
|
| 119 |
## Miscellaneous changes and fixes |
| 120 |
|
| 121 |
- The `keyify` utility has been deleted. This functionality is provided by gopls |
| 122 |
nowadays. |
| 123 |
- `staticcheck -merge` now exits with a non-zero exit status if any problems |
| 124 |
were found. This matches the behavior of non-merge uses. |
| 125 |
- Malformed `staticcheck.conf` files now cause more useful errors to be emitted. |
| 126 |
- Labeled statements with blank labels no longer cause panics. |
| 127 |
- Functions with named return parameters that never actually return no longer |
| 128 |
cause panics ({{< issue "1533" >}}). |
| 129 |
|
| 130 |
## Staticcheck 2024.1.1 release notes {#2024.1.1} |
| 131 |
|
| 132 |
This release fixes the detection of the used Go version when Go was compiled |
| 133 |
with experimental features such as `rangefunc` or `boringcrypto` ({{< issue |
| 134 |
"1586" >}}). |