dupword

Table of Contents

  1. 解決する問題
  2. 設定
  3. オプション
  4. サンプル
    1. 検出例
    2. 修正例
  5. 注意点
  6. 参考リンク

dupword はソースコード中の重複した単語(タイポ)を検出するツールです。

解決する問題

コメントや文字列リテラルで同じ単語が連続して書かれるタイポは、コードレビューで見落とされがちです。ドキュメントの品質低下や、意図しない文言がユーザーに表示される原因になります。

1
2
3
4
5
6
7
// Before: 重複した単語がある
// This function returns the the user name.
// ^^^ ^^^ "the" が重複
func GetUserName(id string) string {
return fmt.Sprintf("User name is is %s", id)
// ^^ ^^ "is" が重複
}
1
2
3
4
5
// After: 重複を修正
// This function returns the user name.
func GetUserName(id string) string {
return fmt.Sprintf("User name is %s", id)
}

設定

公式ドキュメント

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# .golangci.yml 設定例
version: "2"

linters:
enable:
- dupword
settings:
dupword:
keywords:
- "the"
- "and"
- "a"
- "is"
- "to"
- "in"
- "of"
comments-only: true

オプション

オプション デフォルト 説明
keywords []string [](全単語) チェック対象の単語リスト。空の場合はすべての重複単語を検出する
ignore []string [] チェック対象から除外する単語リスト
comments-only bool false コメントのみをチェックし、文字列リテラルはスキップする

サンプル

検出例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// dupword が警告するパターン

// 1. コメント内の重複
// The user should should be validated before saving.
// ^^^^^^ ^^^^^^ dupword: "should" is duplicated

// 2. 行をまたぐ重複
// This is the end of the
// the paragraph.
// ^^^ dupword: "the" is duplicated

// 3. 文字列リテラル内の重複(comments-only: false の場合)
func example() {
msg := "Please enter enter your password"
// ^^^^^ ^^^^^ dupword: "enter" is duplicated
_ = msg
}

修正例

1
2
3
4
5
6
7
8
// The user should be validated before saving.

// This is the end of the paragraph.

func example() {
msg := "Please enter your password"
_ = msg
}

注意点

  • keywords を指定すると検出対象がその単語に限定される。誤検知を減らしたい場合は theandis などの前置詞・冠詞に絞ると効果的
  • ignore で意図的な繰り返し("0C0C" などの 16 進数パターン等)を除外できる
  • comments-only: true にすると文字列リテラル内のチェックをスキップする。意図的な繰り返しが文字列に含まれる場合に有用

参考リンク