RuboCop | Style/TrivialAccessors

# RuboCop | Style/TrivialAccessors

## 概要

RuboCopの「Style/TrivialAccessors」警告について。

attr_reader/writer/accessor で代用できるような些細なアクセッサを
自分で定義していないかチェックする。

## TrivialAccessors

### 設定値一覧

|設定対象|対象|内容|デフォルト|
|:-----|:--|:---|:---:|
|ExactNameMatch|true|インスタンス変数名とアクセサのメソッド名が統一されている場合のみアクセサと判断してチェックする|--|
|ExactNameMatch|false|インスタンス変数名とアクセサのメソッド名が統一されている場合以外もアクセサと判断してチェックする|○|
|AllowPredicates|true|?付きのメソッド名は単純なアクセサでも許容する|--|
|AllowPredicates|false|?付きのメソッド名も他のメソッドと同様に検証する|○|
|AllowDSLWriters|true|DSL用のwriterメソッドを許容する|--|
|AllowDSLWriters|false|DSL用のwriterメソッドを許容しない|○|
|Whitelist|[to_ary,<br />to_a,<br />to_c,<br />to_enum,<br />to_h,<br />to_hash,<br />to_i,<br />to_int,<br />to_io,<br />to_open,<br />to_path,<br />to_proc,<br />to_r,<br />to_regexp,<br />to_str,<br />to_s,<br />to_sym]|変換メソッドなど、例外的に許容するメソッド名を定義する|○|

### 検証プログラム

~~~ruby:trivial_accessors.rb
# Hoge
class Hoge
  def hoge
    @hoge
  end

  def hoge=(hoge)
    @hoge = hoge
  end

  def hoge(hoge)
    @hoge = hoge
  end

  def higee
    @hige
  end

  def hoge?
    @hoge
  end

  def to_s
    @hoge
  end

  def to_hoge
    @hoge
  end
end
~~~

### 実行結果 デフォルト の場合

~~~yaml:.rubocop.yml
TrivialAccessors:
  ExactNameMatch: false
  AllowPredicates: false
  AllowDSLWriters: false
  Whitelist:
    - to_ary
    - to_a
    - to_c
    - to_enum
    - to_h
    - to_hash
    - to_i
    - to_int
    - to_io
    - to_open
    - to_path
    - to_proc
    - to_r
    - to_regexp
    - to_str
    - to_s
    - to_sym
~~~

~~~bash
$ rubocop trivial_accessors.rb
Inspecting 1 file
C

Offenses:

trivial_accessors.rb:3:3: C: Use attr_reader to define trivial reader methods.
  def hoge
  ^^^
trivial_accessors.rb:7:3: C: Use attr_writer to define trivial writer methods.
  def hoge=(hoge)
  ^^^
trivial_accessors.rb:11:3: C: Use attr_writer to define trivial writer methods.
  def hoge(hoge)
  ^^^
trivial_accessors.rb:15:3: C: Use attr_reader to define trivial reader methods.
  def higee
  ^^^
trivial_accessors.rb:19:3: C: Use attr_reader to define trivial reader methods.
  def hoge?
  ^^^
trivial_accessors.rb:27:3: C: Use attr_reader to define trivial reader methods.
  def to_hoge
  ^^^

1 file inspected, 6 offenses detected
~~~

### 実行結果 ExactNameMatch を *true* に設定した場合

~~~yaml:.rubocop.yml
TrivialAccessors:
  ExactNameMatch: true
  AllowPredicates: false
  AllowDSLWriters: false
  Whitelist:
    - to_ary
    - to_a
    - to_c
    - to_enum
    - to_h
    - to_hash
    - to_i
    - to_int
    - to_io
    - to_open
    - to_path
    - to_proc
    - to_r
    - to_regexp
    - to_str
    - to_s
    - to_sym
~~~

~~~bash
$ rubocop trivial_accessors.rb
Inspecting 1 file
C

Offenses:

trivial_accessors.rb:3:3: C: Use attr_reader to define trivial reader methods.
  def hoge
  ^^^
trivial_accessors.rb:7:3: C: Use attr_writer to define trivial writer methods.
  def hoge=(hoge)
  ^^^
trivial_accessors.rb:11:3: C: Use attr_writer to define trivial writer methods.
  def hoge(hoge)
  ^^^

1 file inspected, 3 offenses detected
~~~

### 実行結果 AllowPredicates を *true* に設定した場合

~~~yaml:.rubocop.yml
TrivialAccessors:
  ExactNameMatch: false
  AllowPredicates: true
  AllowDSLWriters: false
  Whitelist:
    - to_ary
    - to_a
    - to_c
    - to_enum
    - to_h
    - to_hash
    - to_i
    - to_int
    - to_io
    - to_open
    - to_path
    - to_proc
    - to_r
    - to_regexp
    - to_str
    - to_s
    - to_sym
~~~

下記が許容されるようになっていることが確認できる

~~~ruby
  def hoge?
    @hoge
  end
~~~

~~~bash
$ rubocop trivial_accessors.rb
Inspecting 1 file
C

Offenses:

trivial_accessors.rb:3:3: C: Use attr_reader to define trivial reader methods.
  def hoge
  ^^^
trivial_accessors.rb:7:3: C: Use attr_writer to define trivial writer methods.
  def hoge=(hoge)
  ^^^
trivial_accessors.rb:11:3: C: Use attr_writer to define trivial writer methods.
  def hoge(hoge)
  ^^^
trivial_accessors.rb:15:3: C: Use attr_reader to define trivial reader methods.
  def higee
  ^^^
trivial_accessors.rb:27:3: C: Use attr_reader to define trivial reader methods.
  def to_hoge
  ^^^

1 file inspected, 5 offenses detected
~~~

### 実行結果 AllowDSLWriters を *true* に設定した場合

~~~yaml:.rubocop.yml
TrivialAccessors:
  ExactNameMatch: false
  AllowPredicates: false
  AllowDSLWriters: true
  Whitelist:
    - to_ary
    - to_a
    - to_c
    - to_enum
    - to_h
    - to_hash
    - to_i
    - to_int
    - to_io
    - to_open
    - to_path
    - to_proc
    - to_r
    - to_regexp
    - to_str
    - to_s
    - to_sym
~~~

下記が許容されるようになっていることが確認できる

~~~ruby
  def hoge(hoge)
    @hoge = hoge
  end
~~~

~~~bash
$ rubocop trivial_accessors.rb
Inspecting 1 file
C

Offenses:

trivial_accessors.rb:3:3: C: Use attr_reader to define trivial reader methods.
  def hoge
  ^^^
trivial_accessors.rb:7:3: C: Use attr_writer to define trivial writer methods.
  def hoge=(hoge)
  ^^^
trivial_accessors.rb:15:3: C: Use attr_reader to define trivial reader methods.
  def higee
  ^^^
trivial_accessors.rb:19:3: C: Use attr_reader to define trivial reader methods.
  def hoge?
  ^^^
trivial_accessors.rb:27:3: C: Use attr_reader to define trivial reader methods.
  def to_hoge
  ^^^

1 file inspected, 5 offenses detected
~~~

### 実行結果 Whitelist に *to_hoge* を追加した場合

~~~yaml:.rubocop.yml
TrivialAccessors:
  ExactNameMatch: false
  AllowPredicates: false
  AllowDSLWriters: false
  Whitelist:
    - to_ary
    - to_a
    - to_c
    - to_enum
    - to_h
    - to_hash
    - to_i
    - to_int
    - to_io
    - to_open
    - to_path
    - to_proc
    - to_r
    - to_regexp
    - to_str
    - to_s
    - to_sym
    - to_hoge
~~~

下記が許容されるようになっていることが確認できる

~~~ruby
  def to_hoge
    @hoge
  end
~~~

~~~bash
$rubocop trivial_accessors.rb
Inspecting 1 file
C

Offenses:

trivial_accessors.rb:3:3: C: Use attr_reader to define trivial reader methods.
  def hoge
  ^^^
trivial_accessors.rb:7:3: C: Use attr_writer to define trivial writer methods.
  def hoge=(hoge)
  ^^^
trivial_accessors.rb:11:3: C: Use attr_writer to define trivial writer methods.
  def hoge(hoge)
  ^^^
trivial_accessors.rb:15:3: C: Use attr_reader to define trivial reader methods.
  def higee
  ^^^
trivial_accessors.rb:19:3: C: Use attr_reader to define trivial reader methods.
  def hoge?
  ^^^

1 file inspected, 5 offenses detected
~~~

## 対象コード

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/trivial_accessors.rb
https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/style/trivial_accessors_spec.rb

## 補足
この警告は rubocop -a で修正可能です。

## RuboCopまとめ記事

http://qiita.com/tbpgr/items/edbfecb6a6789dd54f47