class RuboCop::Cop::Style::RedundantParentheses

This cop checks for redundant parentheses.

@example

# bad
(x) if ((y.z).nil?)

# good
x if y.z.nil?

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 35
def autocorrect(node)
  ParenthesesCorrector.correct(node)
end
on_begin(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 27
def on_begin(node)
  return if !parentheses?(node) || parens_allowed?(node)
  return if node.parent && (node.parent.while_post_type? ||
                            node.parent.until_post_type?)

  check(node)
end

Private Instance Methods

allowed_ancestor?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 55
def allowed_ancestor?(node)
  # Don't flag `break(1)`, etc
  keyword_ancestor?(node) && parens_required?(node)
end
allowed_array_or_hash_element?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 132
def allowed_array_or_hash_element?(node)
  # Don't flag
  # ```
  # { a: (1
  #      ), }
  # ```
  (hash_element?(node) || array_element?(node)) &&
    only_closing_paren_before_comma?(node)
end
allowed_expression?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 48
def allowed_expression?(node)
  allowed_ancestor?(node) ||
    allowed_method_call?(node) ||
    allowed_array_or_hash_element?(node) ||
    allowed_multiple_expression?(node)
end
allowed_method_call?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 60
def allowed_method_call?(node)
  # Don't flag `method (arg) { }`
  arg_in_call_with_block?(node) && !parentheses?(node.parent)
end
allowed_multiple_expression?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 65
def allowed_multiple_expression?(node)
  return false if node.children.one?

  ancestor = node.ancestors.first
  return false unless ancestor

  !ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
end
array_element?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 146
def array_element?(node)
  node.parent&.array_type?
end
call_chain_starts_with_int?(begin_node, send_node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 214
def call_chain_starts_with_int?(begin_node, send_node)
  recv = first_part_of_call_chain(send_node)
  recv&.int_type? && (parent = begin_node.parent) &&
    parent.send_type? && (parent.method?(:-@) || parent.method?(:+@))
end
check(begin_node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 85
def check(begin_node)
  node = begin_node.children.first
  if keyword_with_redundant_parentheses?(node)
    return offense(begin_node, 'a keyword')
  end
  if disallowed_literal?(begin_node, node)
    return offense(begin_node, 'a literal')
  end
  return offense(begin_node, 'a variable') if node.variable?
  return offense(begin_node, 'a constant') if node.const_type?

  check_send(begin_node, node) if node.send_type?
end
check_send(begin_node, node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 99
def check_send(begin_node, node)
  return check_unary(begin_node, node) if node.unary_operation?

  return unless method_call_with_redundant_parentheses?(node)
  return if call_chain_starts_with_int?(begin_node, node)

  offense(begin_node, 'a method call')
end
check_unary(begin_node, node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 108
def check_unary(begin_node, node)
  return if begin_node.chained?

  node = node.children.first while suspect_unary?(node)

  if node.send_type?
    return unless method_call_with_redundant_parentheses?(node)
  end

  offense(begin_node, 'an unary operation')
end
disallowed_literal?(begin_node, node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 157
def disallowed_literal?(begin_node, node)
  node.literal? &&
    !node.range_type? &&
    !raised_to_power_negative_numeric?(begin_node, node)
end
empty_parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 74
def empty_parentheses?(node)
  # Don't flag `()`
  node.children.empty?
end
first_argument?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 202
def first_argument?(node)
  first_send_argument?(node) || first_super_argument?(node)
end
hash_element?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 142
def hash_element?(node)
  node.parent&.pair_type?
end
hash_literal_as_first_arg?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 79
def hash_literal_as_first_arg?(node)
  # Don't flag `method ({key: value})`
  node.children.first.hash_type? && first_argument?(node) &&
    !parentheses?(node.parent)
end
keyword_ancestor?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 128
def keyword_ancestor?(node)
  node.parent&.keyword?
end
keyword_with_redundant_parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 175
def keyword_with_redundant_parentheses?(node)
  return false unless node.keyword?
  return true if node.special_keyword?

  args = *node

  if only_begin_arg?(args)
    parentheses?(args.first)
  else
    args.empty? || parentheses?(node)
  end
end
method_call_with_redundant_parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 188
def method_call_with_redundant_parentheses?(node)
  return false unless node.send_type?
  return false if node.prefix_not?
  return false if range_end?(node)

  send_node, args = method_node_and_args(node)

  args.empty? || parentheses?(send_node) || square_brackets?(send_node)
end
offense(node, msg) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 120
def offense(node, msg)
  add_offense(node, message: "Don't use parentheses around #{msg}.")
end
only_begin_arg?(args) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 198
def only_begin_arg?(args)
  args.one? && args.first.begin_type?
end
only_closing_paren_before_comma?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 150
def only_closing_paren_before_comma?(node)
  source_buffer = node.source_range.source_buffer
  line_range = source_buffer.line_range(node.loc.end.line)

  line_range.source =~ /^\s*\)\s*,/
end
parens_allowed?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 41
def parens_allowed?(node)
  empty_parentheses?(node) ||
    hash_literal_as_first_arg?(node) ||
    rescue?(node) ||
    allowed_expression?(node)
end
raised_to_power_negative_numeric?(begin_node, node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 163
def raised_to_power_negative_numeric?(begin_node, node)
  return false unless node.numeric_type?

  siblings = begin_node.parent&.children
  return false if siblings.nil?

  next_sibling = siblings[begin_node.sibling_index + 1]
  base_value = node.children.first

  base_value.negative? && next_sibling == :**
end
suspect_unary?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_parentheses.rb, line 124
def suspect_unary?(node)
  node.send_type? && node.unary_operation? && !node.prefix_not?
end