class RuboCop::Cop::Expert::RedundantParenthesesForMethodCall

This cop checks for all omittable parentheses in method calls.

@example

# bad
foo()
foo(0, 1)
foo(0 + 1)
foo(a = 1)
foo(0, *a)
foo(0, a: 1, b: 2)
foo bar(0, 1)
foo(bar 0, 1)
foo 0, (bar(1))
foo(0) do end
foo(/a/)
foo(%w[a b c])
foo(<<~STR)
  heredoc
STR
for a in foo(0, 1); end

# good
foo
foo 0, 1
foo 0 + 1
foo a = 1
foo 0, *a
foo 0, a: 1, b: 2
foo bar 0, 1
foo 0, (bar 1)
foo 0 do end
foo /a/
foo %w[a b c]
foo <<~STR
  heredoc
STR
for a in foo 0, 1; end

# good
# Parentheses are required.
-foo(0)
foo(0) + 1
foo(0) ? bar(1) : baz(2)
foo(0).bar
foo(0){}
foo 0, bar(1)
foo bar(0), 1
a = 0, foo(1) # multiple assignment
foo bar(0) do end
[foo(0)]
{ foo: bar(0) }
case 0; when foo(1); end
def foo a = bar(0), b: baz(1); end
return 0, foo(1) # return multiple value

# good
# Parentheses are not part of the method call.
# Use the default cop Style/RedundantParentheses.
foo (0)
foo (0 + 1)
foo (bar 0), 1

@example AllowInMultilineCall: never

# bad
foo(0, 1,
  a: 2,
  b: 3,
)
foo(
  0,
  a: 1,
  b: 2,
)

# good
foo 0, 1,
  a: 1,
  b: 2
foo \
  0,
  a: 1,
  b: 2

@example AllowInMultilineCall: before_newline (default)

# bad
foo(0, 1,
  a: 2,
  b: 3,
)

# good
foo(
  0,
  a: 1,
  b: 2,
)
foo 0, 1,
  a: 1,
  b: 2
foo \
  0,
  a: 1,
  b: 2

@example AllowInMultilineCall: always

# good
foo(0, 1,
  a: 2,
  b: 3,
)
foo(
  0,
  a: 1,
  b: 2,
)
foo 0, 1,
  a: 1,
  b: 2
foo \
  0,
  a: 1,
  b: 2

Constants

HIGH_OPERATORS

docs.ruby-lang.org/ja/latest/doc/spec=2foperator.html

MSG
MULTILINE_CONFIG_NAME
MULTILINE_CONFIG_VALUES
PARENS_ALLOW_CHECKERS

Public Instance Methods

on_csend(node)
Alias for: on_send
on_send(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 147
def on_send node
  return unless node.parenthesized_call?
  return if parens_allowed? node
  return if allowed_by_multiline_config? node
  add_offense node, location: :begin
end
Also aliased as: on_csend

Private Instance Methods

allowed_by_multiline_config?(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 172
        def allowed_by_multiline_config? node
  fn_ln = node.loc.selector&.line
  first_arg_ln = node.arguments[0]&.loc&.line
  par_end_ln = node.loc.end.line

  fn_ln && fn_ln != par_end_ln &&
    case multiline_config
    when :never
      false
    when :before_newline
      first_arg_ln && fn_ln != first_arg_ln
    when :always
      true
    end
end
explicit_receiver?(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 252
        def explicit_receiver? node
  bracket_receiver?(node) || dot_receiver?(node)
end
high_method_operand?(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 271
        def high_method_operand? node
  high_method_operator_receiver?(node) || high_method_operator_arg?(node)
end
high_operand?(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 296
        def high_operand? node
  high_method_operand?(node) || high_special_operand?(node) ||
    ternary_operand?(node)
end
high_operator?(op) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 301
        def high_operator? op
  HIGH_OPERATORS.include? op.to_sym
end
high_special_operand?(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 305
        def high_special_operand? node
  op =
    case
    when node.parent&.irange_type? then '..'
    when node.parent&.erange_type? then '...'
    when special_operand?(node) then node.parent.operator
    end
  op && high_operator?(op)
end
implicit_call?(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 315
        def implicit_call? node
  node.implicit_call?
end
multiline_config() click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 319
        def multiline_config
  @multiline_config ||=
    (cop_config[MULTILINE_CONFIG_NAME] || :before_newline).to_sym.tap do |a|
      unless MULTILINE_CONFIG_VALUES.include? a
        raise "Unknown option: #{MULTILINE_CONFIG_NAME}: #{a}"
      end
    end
end
parens_allowed?(node) click to toggle source
# File lib/rubocop/cop/expert/redundant_parentheses_for_method_call.rb, line 328
        def parens_allowed? node
  arg_s?(node) &&
    PARENS_ALLOW_CHECKERS.any? do |sym|
      __send__ sym, node
    end
end