Skip to content

Goでenumの記述漏れを検出する

This content is a draft and will not be included in production builds.

golangci-lintexhaustive があるので、これを有効にするといい。exhaustructは型リテラルごとにデフォルトの挙動と個別の挙動を変えられるでもメモを書いていた。Goで構造体フィールド初期化漏れを検出するもあった。Goでinterfaceのtype switchに記述漏れがあったら検出するもある。いくつ書いているんだ。

linters:
enable:
- exhaustive
- exhaustruct
settings:
exhaustive:
check:
- switch
- map
explicit-exhaustive-switch: true
explicit-exhaustive-map: true

デフォルトは exhaustive:enforce コメントを付けても map を対象としないので、enable は必ず書いておく必要がある。

オプションの他に、exhaustive:enforceexhaustive:ignore コメントが用意されている。これも exhaustruct と同様にデフォルト無効にしておいて、必要な場面で exhaustive:enforce するのがいいと思う。

//exhaustive:enforce
var attributeOperators = map[attributeOperator]struct{}{
attributeEqualOperator: struct{}{},
}
//exhaustive:enforce
switch op {
case attributeEqualOperator:
return "="
}

おそらく実態はnishanths/exhaustiveだと思う。