class RuboCop::Cop::Style::ParenthesesAroundCondition

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

Public Instance Methods

on_if(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 12
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 18
def on_while(node)
  process_control_op(node)
end
Also aliased as: on_until

Private Instance Methods

message(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 49
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 41
def modifier_op?(node)
  return false if node.if_type? && node.ternary?
  return true if node.rescue_type?

  MODIFIER_NODES.include?(node.type) &&
    node.modifier_form?
end
process_control_op(node) click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 29
def process_control_op(node)
  cond = node.condition

  control_op_condition(cond) do |first_child|
    return if modifier_op?(first_child)
    return if parens_required?(node.children.first)
    return if safe_assignment?(cond) && safe_assignment_allowed?

    add_offense(cond)
  end
end