4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RuboCop | Style/TrivialAccessors

Posted at

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,
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]
変換メソッドなど、例外的に許容するメソッド名を定義する

検証プログラム

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

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

.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
$ 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 に設定した場合

.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
$ 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 に設定した場合

.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

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

  def hoge?
    @hoge
  end
$ 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 に設定した場合

.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

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

  def hoge(hoge)
    @hoge = hoge
  end
$ 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 を追加した場合

.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

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

  def to_hoge
    @hoge
  end
$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まとめ記事

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?