123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Style::RedundantParentheses

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/style/redundant_parentheses.rb

Overview

Checks for redundant parentheses.

Examples:

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

# good
x if y.z.nil?

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns an url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::Parentheses - Included

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Attribute Details

#allow_in_multiline_conditions?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 177

def allow_in_multiline_conditions?
  parentheses_around_condition_config = config.for_cop('Style/ParenthesesAroundCondition')
  return false unless parentheses_around_condition_config['Enabled']

  !!parentheses_around_condition_config['AllowInMultilineConditions']
end

#ternary_parentheses_required?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 93

def ternary_parentheses_required?
  config = @config.for_cop('Style/TernaryParentheses')
  allowed_styles = %w[require_parentheses require_parentheses_when_complex]

  config.fetch('Enabled') && allowed_styles.include?(config['EnforcedStyle'])
end

Instance Method Details

#allowed_ancestor?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 68

def allowed_ancestor?(node)
  # Don't flag `break(1)`, etc
  keyword_ancestor?(node) && parens_required?(node)
end

#allowed_expression?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 60

def allowed_expression?(node)
  allowed_ancestor?(node) ||
    allowed_method_call?(node) ||
    allowed_multiple_expression?(node) ||
    allowed_ternary?(node) ||
    node.parent&.range_type?
end

#allowed_method_call?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 73

def allowed_method_call?(node)
  # Don't flag `method (arg) { }`
  arg_in_call_with_block?(node) && !parentheses?(node.parent)
end

#allowed_multiple_expression?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 78

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

#allowed_pin_operator?(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 32

def_node_matcher :allowed_pin_operator?, '^(pin (begin !{lvar ivar cvar gvar}))'

#allowed_ternary?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 87

def allowed_ternary?(node)
  return false unless node&.parent&.if_type?

  node.parent.ternary? && ternary_parentheses_required?
end

#arg_in_call_with_block?(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 35

def_node_matcher :arg_in_call_with_block?, '^^(block (send _ _ equal?(%0) ...) ...)'

#call_chain_starts_with_int?(begin_node, send_node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 283

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) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 136

def check(begin_node)
  node = begin_node.children.first

  if (message = find_offense_message(begin_node, node))
    return offense(begin_node, message)
  end

  check_send(begin_node, node) if node.call_type?
end

#check_send(begin_node, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 184

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) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 193

def check_unary(begin_node, node)
  return if begin_node.chained?

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

  return if node.send_type? && !method_call_with_redundant_parentheses?(node)

  offense(begin_node, 'a unary operation')
end

#disallowed_literal?(begin_node, node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 217

def disallowed_literal?(begin_node, node)
  node.literal? && !node.range_type? && !raised_to_power_negative_numeric?(begin_node, node)
end

#empty_parentheses?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 114

def empty_parentheses?(node)
  # Don't flag `()`
  node.children.empty?
end

#find_offense_message(begin_node, node) (private)

Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 147

def find_offense_message(begin_node, node)
  return 'a keyword' if keyword_with_redundant_parentheses?(node)
  return 'a literal' if disallowed_literal?(begin_node, node)
  return 'a variable' if node.variable?
  return 'a constant' if node.const_type?
  if node.lambda_or_proc? && (node.braces? || node.send_node.lambda_literal?)
    return 'an expression'
  end
  return 'an interpolated expression' if interpolation?(begin_node)

  return if begin_node.chained?

  if node.and_type? || node.or_type?
    return if node.semantic_operator? && begin_node.parent
    return if node.multiline? && allow_in_multiline_conditions?
    return if ALLOWED_NODE_TYPES.include?(begin_node.parent&.type)
    return if begin_node.parent&.if_type? && begin_node.parent&.ternary?

    'a logical expression'
  elsif node.respond_to?(:comparison_method?) && node.comparison_method?
    return unless begin_node.parent.nil?

    'a comparison expression'
  end
end

#first_arg_begins_with_hash_literal?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 119

def first_arg_begins_with_hash_literal?(node)
  # Don't flag `method ({key: value})` or `method ({key: value}.method)`
  hash_literal = method_chain_begins_with_hash_literal(node.children.first)
  if (root_method = node.each_ancestor(:send).to_a.last)
    parenthesized = root_method.parenthesized_call?
  end
  hash_literal && first_argument?(node) && !parentheses?(hash_literal) && !parenthesized
end

#first_argument?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 258

def first_argument?(node)
  if first_send_argument?(node) ||
     first_super_argument?(node) ||
     first_yield_argument?(node)
    return true
  end

  node.each_ancestor.any? { |ancestor| first_argument?(ancestor) }
end

#first_send_argument?(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 269

def_node_matcher :first_send_argument?, <<~PATTERN
  ^(send _ _ equal?(%0) ...)
PATTERN

#first_super_argument?(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 274

def_node_matcher :first_super_argument?, <<~PATTERN
  ^(super equal?(%0) ...)
PATTERN

#first_yield_argument?(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 279

def_node_matcher :first_yield_argument?, <<~PATTERN
  ^(yield equal?(%0) ...)
PATTERN

#ignore_syntax?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 53

def ignore_syntax?(node)
  return false unless (parent = node.parent)

  parent.while_post_type? || parent.until_post_type? || parent.match_with_lvasgn_type? ||
    like_method_argument_parentheses?(parent) || multiline_control_flow_statements?(node)
end

#interpolation?(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 175

def_node_matcher :interpolation?, '[^begin ^^dstr]'

#keyword_ancestor?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 213

def keyword_ancestor?(node)
  node.parent&.keyword?
end

#keyword_with_redundant_parentheses?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 232

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

#like_method_argument_parentheses?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 100

def like_method_argument_parentheses?(node)
  return false if !node.send_type? && !node.super_type? && !node.yield_type?

  node.arguments.one? && !node.parenthesized? &&
    !node.arithmetic_operation? && node.first_argument.begin_type?
end

#method_call_with_redundant_parentheses?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 245

def method_call_with_redundant_parentheses?(node)
  return false unless node.call_type?
  return false if node.prefix_not?

  send_node, args = method_node_and_args(node)

  args.empty? || parentheses?(send_node) || square_brackets?(send_node)
end

#method_chain_begins_with_hash_literal(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 128

def method_chain_begins_with_hash_literal(node)
  return if node.nil?
  return node if node.hash_type?
  return unless node.send_type?

  method_chain_begins_with_hash_literal(node.children.first)
end

#method_node_and_args(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 26

def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'

#multiline_control_flow_statements?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 107

def multiline_control_flow_statements?(node)
  return false unless (parent = node.parent)
  return false if parent.single_line?

  parent.return_type? || parent.next_type? || parent.break_type?
end

#offense(node, msg) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 203

def offense(node, msg)
  add_offense(node, message: "Don't use parentheses around #{msg}.") do |corrector|
    ParenthesesCorrector.correct(corrector, node)
  end
end

#on_begin(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 37

def on_begin(node)
  return if !parentheses?(node) || parens_allowed?(node) || ignore_syntax?(node)

  check(node)
end

#only_begin_arg?(args) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 254

def only_begin_arg?(args)
  args.one? && args.first.begin_type?
end

#parens_allowed?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 45

def parens_allowed?(node)
  empty_parentheses?(node) ||
    first_arg_begins_with_hash_literal?(node) ||
    rescue?(node) ||
    allowed_pin_operator?(node) ||
    allowed_expression?(node)
end

#raised_to_power_negative_numeric?(begin_node, node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 221

def raised_to_power_negative_numeric?(begin_node, node)
  return false unless node.numeric_type?

  next_sibling = begin_node.right_sibling
  return false unless next_sibling

  base_value = node.children.first

  base_value.negative? && next_sibling == :**
end

#rescue?(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 29

def_node_matcher :rescue?, '{^resbody ^^resbody}'

#square_brackets?(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 23

def_node_matcher :square_brackets?, '(send {(send _recv _msg) str array hash} :[] ...)'

#suspect_unary?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/redundant_parentheses.rb', line 209

def suspect_unary?(node)
  node.send_type? && node.unary_operation? && !node.prefix_not?
end