5
5

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

Last updated at Posted at 2014-07-17

RuboCop | Style/ParenthesesAroundCondition

概要

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

if や while などの条件文を括弧で囲っていると警告を出します。

設定値一覧

設定対象 設定値 内容 デフォルト
AllowSafeAssignment true イコールで終わるメソッド(=,==,===)の場合には警告を出さない
AllowSafeAssignment false イコールで終わるメソッド(=,==,===)の場合も警告を出す --

ParenthesesAroundCondition

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

検証プログラム

parentheses_around_condition
msg = 'hoge'
if (msg > 'hoge')
  puts 'hoge!'
  puts 'hoge!'
end

if (msg = 'hige')
  puts 'hoge!'
  puts 'hoge!'
end

if msg == 'hoge'
  puts 'hige!'
  puts 'hige!'
end

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

.rubocop.yml
ParenthesesAroundCondition:
  AllowSafeAssignment: true
$ rubocop parentheses_around_condition.rb
Inspecting 1 file
C

Offenses:

parentheses_around_condition.rb:2:4: C: Don't use parentheses around the condition of an if.
if (msg > 'hoge')
   ^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

実行結果 false に設定します

.rubocop.yml
ParenthesesAroundCondition:
  AllowSafeAssignment: false
$ rubocop parentheses_around_condition.rb
Inspecting 1 file
C

Offenses:

parentheses_around_condition.rb:2:4: C: Don't use parentheses around the condition of an if.
if (msg > 'hoge')
   ^^^^^^^^^^^^^^
parentheses_around_condition.rb:7:4: C: Don't use parentheses around the condition of an if.
if (msg = 'hige')
   ^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

補足

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

RuboCopまとめ記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?