class RuboCop::Cop::Style::SymbolProc


something.map { |s| s.upcase }
# good
@example AllowedPatterns: [‘map’] (default)
something.map { |s| s.upcase }
# bad
@example AllowedPatterns: [] (default)
define_method(:foo) { |foo| foo.bar }
# good
@example AllowedMethods: [define_method] (default)
end
# some comment
s.upcase # some comment
# some comment
something.do_something do |s| # some comment
# good - if there are comment in either position
@example AllowComments: true
end
# some comment
s.upcase # some comment
# some comment
something.do_something do |s| # some comment
# bad
@example AllowComments: false (default)
something.do_something(foo) { |o| o.bar }
# good
@example AllowMethodsWithArguments: true
something.do_something(foo, &:bar)
# good
something.do_something(foo) { |o| o.bar }
# bad
@example AllowMethodsWithArguments: false (default)
something.map(&:upcase)
# good
something.map { _1.upcase }
something.map { |s| s.upcase }
# bad
@example
—-
# ArgumentError: wrong number of arguments (given 1, expected 0)
call(&:bar)
#=> :bar
call { |x| x.bar }
end
block.call(Foo.new, options)
def call(options = {}, &block)
end
end
:bar
def bar
class Foo
—-
[source,ruby]

For example:
number of arguments is wrong, but a non-lambda ‘Proc` will not.
For example, a lambda will raise an `ArgumentError` if the
a `Proc` generated from a block does not.
generated from `Symbol#to_proc` behaves as a lambda, while
This cop is unsafe because there is a difference that a `Proc`
@safety
These are customizable with `AllowedMethods` option.
`define_method?` methods are allowed by default.
please set `true` to `AllowMethodsWithArguments`.
If you prefer a style that allows block for method with arguments,
Use symbols as procs when possible.

def self.autocorrect_incompatible_with

def self.autocorrect_incompatible_with
  [Layout::SpaceBeforeBlockBraces]
end

def allow_comments?

def allow_comments?
  cop_config.fetch('AllowComments', false)
end

def allow_if_method_has_argument?(send_node)

def allow_if_method_has_argument?(send_node)
  !!cop_config.fetch('AllowMethodsWithArguments', false) && !send_node.arguments.count.zero?
end

def allowed_method_name?(name)

def allowed_method_name?(name)
  allowed_method?(name) || matches_allowed_pattern?(name)
end

def autocorrect(corrector, node)

def autocorrect(corrector, node)
  if node.send_node.arguments?
    autocorrect_with_args(corrector, node, node.send_node.arguments, node.body.method_name)
  else
    autocorrect_without_args(corrector, node)
  end
end

def autocorrect_with_args(corrector, node, args, method_name)

def autocorrect_with_args(corrector, node, args, method_name)
  arg_range = args.last.source_range
  arg_range = range_with_surrounding_comma(arg_range, :right)
  replacement = " &:#{method_name}"
  replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
  corrector.insert_after(arg_range, replacement)
  corrector.remove(block_range_with_space(node))
end

def autocorrect_without_args(corrector, node)

def autocorrect_without_args(corrector, node)
  corrector.replace(block_range_with_space(node), "(&:#{node.body.method_name})")
end

def begin_pos_for_replacement(node)

def begin_pos_for_replacement(node)
  expr = node.send_node.source_range
  if (paren_pos = (expr.source =~ /\(\s*\)$/))
    expr.begin_pos + paren_pos
  else
    node.loc.begin.begin_pos
  end
end

def block_range_with_space(node)

def block_range_with_space(node)
  block_range = range_between(begin_pos_for_replacement(node), node.loc.end.end_pos)
  range_with_surrounding_space(block_range, side: :left)
end

def destructuring_block_argument?(argument_node)

def destructuring_block_argument?(argument_node)
  argument_node.one? && argument_node.source.include?(',')
end

def on_block(node)

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def on_block(node)
  symbol_proc?(node) do |dispatch_node, arguments_node, method_name|
    # TODO: Rails-specific handling that we should probably make
    # configurable - https://github.com/rubocop/rubocop/issues/1485
    # we should allow lambdas & procs
    return if proc_node?(dispatch_node)
    return if unsafe_hash_usage?(dispatch_node)
    return if unsafe_array_usage?(dispatch_node)
    return if %i[lambda proc].include?(dispatch_node.method_name)
    return if allowed_method_name?(dispatch_node.method_name)
    return if allow_if_method_has_argument?(node.send_node)
    return if node.block_type? && destructuring_block_argument?(arguments_node)
    return if allow_comments? && contains_comments?(node)
    register_offense(node, method_name, dispatch_node.method_name)
  end
end

def register_offense(node, method_name, block_method_name)

def register_offense(node, method_name, block_method_name)
  block_start = node.loc.begin.begin_pos
  block_end = node.loc.end.end_pos
  range = range_between(block_start, block_end)
  message = format(MSG, method: method_name, block_method: block_method_name)
  add_offense(range, message: message) { |corrector| autocorrect(corrector, node) }
end

def unsafe_array_usage?(node)

def unsafe_array_usage?(node)
  node.receiver&.array_type? && %i[min max].include?(node.method_name)
end

def unsafe_hash_usage?(node)

See: https://github.com/rubocop/rubocop/issues/10864
def unsafe_hash_usage?(node)
  node.receiver&.hash_type? && %i[reject select].include?(node.method_name)
end