Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go1.24 go vetとtestsアナライザ

kuro
March 25, 2025

Go1.24 go vetとtestsアナライザ

golang.tokyo #38 で使った資料です。

参考リンク
・Go 1.24 Release Notes - The Go Programming Language
https://go.dev/doc/go1.24

・cmd/go: run vet automatically during 'go test' · Issue #18084 · golang/go · GitHub
https://github.com/golang/go/issues/18084

・cmd/vet: identify 100% reliable checks for testing · Issue #18085 · golang/go · GitHub
https://github.com/golang/go/issues/18085

・go/analysis/passes/tests: split out from vet
https://go-review.googlesource.com/c/tools/+/140457

・cmd/go/internal/test: add 'tests' vet check to 'go test' suite
https://go-review.googlesource.com/c/go/+/603476

・cmd/go: add -tests to list of vet checks run by "go test" #44251
https://github.com/golang/go/issues/44251

kuro

March 25, 2025
Tweet

More Decks by kuro

Other Decks in Programming

Transcript

  1. go vetとは - コードの潜在的な問題を静的解析で検出するツール - コンパイラでは検出できないものを検出する。 - go tool vet

    helpコマンドでアナライザの一覧が表示され る。 - 各アナライザはtools/go/analysis/passesにそれぞれ独 立して実装されている。
  2. go vetとは - go testコマンドを叩くとき、そのパッケージと対象となるソースファ イルに対してgo vetが実行される。 - high-confidenceなアナライザのみデフォルトで実行される。 (Go1.24時点ではatomic,

    bool, buildtags, directive, errorsas, ifaceassert, nilfunc, printf, stringintconv, tests) - go test側でどのアナライザをデフォルトにするか設定してある。
  3. go vetに入ったアップデート 1. 新しいtestsアナライザの追加 ◦ テストための関数の宣言における一般的なミスを検出 ◦ go testで実行されるアナライザのサブセットに含まれる。 2.

    printfアナライザの拡張 ◦ fmt.Printf(s) の形式の呼び出しを検出(sは非定数フォー マット文字列) ◦ %記号を含む可能性のある文字列に対する危険な呼び出 しを警告
  4. 3. buildtagアナライザの拡張 ◦ 無効なビルド制約を検出 ◦ 例://go:build go1.23.1 → //go:build go1.23

    4. copylockアナライザの拡張 ◦ 3-clause for loopで宣言された変数にsync.Mutexのような ロックが含まれる場合を検出 ◦ Go 1.22のループ変数動作変更に対応 go vetに入ったアップデート
  5. リリースノートには The new tests analyzer reports common mistakes in declarations

    of tests, fuzzers, benchmarks, and examples in test packages, such as malformed names, incorrect signatures, or examples that document non-existent identifiers. Some of these mistakes may cause tests not to run. This analyzer is among the subset of analyzers that are run by go test. https://go.dev/doc/go1.24#vet
  6. どのようにしてtestsアナライザが入ったのか - どうやらGo1.24からgo testでデフォルトで動くvetに なったらしい。。。 - issueだと「cmd/go: add -tests to

    list of vet checks run by "go test" #44251」 - Goモジュールプロキシのサンプルに対して testsアナ ラザを実行したところ1056のモジュールに対して、 242件検出され、その96%「ExampleXYZは未知の識 別子XYZを参照している」ものだった。 - その検出されたものチェックした結果、偽陽性が少な いと評価されてデフォルトに追加された。                         src/cmd/go/internal/test/test.go →
  7. そもそもどのような基準でデフォルトが決まったのか - Issue #18084: cmd/go: run vet automatically during 'go

    test' - go vetをベストプラクティスとしたい。 - なので、すでにあるもの(go test)と統合するべき(go fmtがエディターで自動実 行されるように) - vetが何かを検知するまで、ユーザーがgo testのプロセスの一部と気づかないく らい高速に動くように - Issue #18085: cmd/vet: identify 100% reliable checks for testing - 「100%信頼できる」チェックの定義 として、偽陽性(false positive)の可能性 があっても、コードを明確な方法で修正することでその問題を回避できる。
  8. まとめ - go vetのtestsアナライザがgo testコマンドの実行の際にデ フォルトで動くようになった。 - testsアナライザはテストが起動しなくなってしまうような間違い を検知する。 -

    go testでデフォルトで動くアナライザは、偽陽性が少なく、さら にその誤検出による問題を明確に回避できるものが採用され ている。
  9. 参考 - Go 1.24 Release Notes - The Go Programming

    Language - cmd/go: run vet automatically during 'go test' · Issue #18084 · golang/go · GitHub - cmd/vet: identify 100% reliable checks for testing · Issue #18085 · golang/go · GitHub - go/analysis/passes/tests: split out from vet - cmd/go/internal/test: add 'tests' vet check to 'go test' suite - cmd/go: add -tests to list of vet checks run by "go test" #44251