nolintlint は //nolint ディレクティブの品質をチェックするツールです。
解決する問題
//nolint ディレクティブは Linter の警告を抑制する便利な仕組みですが、乱用すると問題が隠蔽されます。対象の Linter 名が指定されていない、理由が書かれていない、既に不要になったディレクティブが残っている、といった問題を検出します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
func BadExample() error { fmt.Println("debug") return nil }
func AnotherBad() { fmt.Println("hello") }
|
1 2 3 4 5 6 7
|
func GoodExample() error { fmt.Println("startup message") return nil }
|
設定
公式ドキュメント
1 2 3 4 5 6 7 8 9 10 11
| version: "2"
linters: enable: - nolintlint settings: nolintlint: require-explanation: true require-specific: true allow-unused: false
|
オプション
| オプション |
型 |
デフォルト |
説明 |
allow-unused |
bool |
false |
効果のない(不要な)//nolint ディレクティブを許可する |
require-explanation |
bool |
false |
//nolint ディレクティブに理由の記載を必須にする |
require-specific |
bool |
false |
抑制対象の Linter 名の指定を必須にする |
allow-no-explanation |
[]string |
[] |
理由の記載を免除する Linter のリスト |
サンプル
検出例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
func example1() {}
func example2() { _ = exec.Command("sh", "-c", userInput) }
func example3() { return }
|
修正例
1 2 3 4 5 6 7 8 9 10
|
func example2() { _ = exec.Command("sh", "-c", validatedInput) }
func example3() { return }
|
注意点
require-specific: true と require-explanation: true を併用するのが推奨。//nolint:linter // 理由 の形式を強制できる
allow-unused: false(デフォルト)にすると、Linter のルール変更やコード修正で不要になった //nolint を自動的に検出できる
allow-no-explanation に特定の Linter を指定すると、その Linter については理由の記載を免除できる
参考リンク