LoginSignup
7
7

More than 5 years have passed since last update.

RuboCop | Style/RedundantReturn

Posted at

RuboCop | Style/RedundantReturn

概要

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

Rubyでは最後のステートメントの return は省略可能であるため、
省略できる return を省略しているかどうかチェックする。

デフォルトは複数の戻り値に対する return に関しても警告を出す。

設定値一覧

設定対象 対象 内容 デフォルト
AllowMultipleReturnValues false 複数の戻り値に対する return に関しても警告を出す -
AllowMultipleReturnValues true 複数の戻り値に対する return に関しても警告を出さない

RedundantReturn

各設定値での検証結果をまとめます。

検証プログラム

raise_args
def hoge1
  return 'msg'
end

def hoge2(msg)
  return msg
end

def hoge3(msg)
  return msg.upcase, msg.downcase
end

def hoge4(msg)
  return 'five' if msg.size == 5
  msg
end

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

.rubocop.yml
RedundantReturn:
  AllowMultipleReturnValues: false
Inspecting 1 file
C

Offenses:

redundant_return.rb:2:3: C: Redundant return detected.
  return 'msg'
  ^^^^^^
redundant_return.rb:6:3: C: Redundant return detected.
  return msg
  ^^^^^^
redundant_return.rb:10:3: C: Redundant return detected.
  return msg.upcase, msg.downcase
  ^^^^^^

1 file inspected, 3 offenses detected

実行結果 compact を設定にします

.rubocop.yml
RedundantReturn:
  AllowMultipleReturnValues: true
Inspecting 1 file
C

Offenses:

redundant_return.rb:2:3: C: Redundant return detected.
  return 'msg'
  ^^^^^^
redundant_return.rb:6:3: C: Redundant return detected.
  return msg
  ^^^^^^

1 file inspected, 2 offenses detected

補足

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

RuboCopまとめ記事

7
7
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
7
7