class RuboCop::Cop::Style::ParenthesesAroundCondition

This cop checks for the presence of superfluous parentheses around the condition of if/unless/while/until.

@example

# bad
x += 1 while (x < 10)
foo unless (bar || baz)

if (x > 10)
elsif (x < 3)
end

# good
x += 1 while x < 10
foo unless bar || baz

if x > 10
elsif x < 3
end

@example AllowInMultilineConditions: false (default)

# bad
if (x > 10 &&
   y > 10)
end

# good
 if x > 10 &&
    y > 10
 end

@example AllowInMultilineConditions: true

# good
if (x > 10 &&
   y > 10)
end

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 57
def autocorrect(node)
  ParenthesesCorrector.correct(node)
end
on_if(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 46
def on_if(node)
  return if node.ternary?

  process_control_op(node)
end
on_until(node)
Alias for: on_while
on_while(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 52
def on_while(node)
  process_control_op(node)
end
Also aliased as: on_until

Private Instance Methods

allow_multiline_conditions?() click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 97
def allow_multiline_conditions?
  cop_config['AllowInMultilineConditions']
end
message(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 85
def message(node)
  kw = node.parent.keyword
  article = kw == 'while' ? 'a' : 'an'
  "Don't use parentheses around the condition of #{article} `#{kw}`."
end
modifier_op?(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 78
def modifier_op?(node)
  return false if node.if_type? && node.ternary?
  return true if node.rescue_type?

  node.basic_conditional? && node.modifier_form?
end
parens_allowed?(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 91
def parens_allowed?(node)
  parens_required?(node) ||
    (safe_assignment?(node) && safe_assignment_allowed?) ||
    (node.multiline? && allow_multiline_conditions?)
end
process_control_op(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 67
def process_control_op(node)
  cond = node.condition

  control_op_condition(cond) do |first_child|
    return if modifier_op?(first_child)
    return if parens_allowed?(cond)

    add_offense(cond)
  end
end