class RuboCop::Cop::Performance::RedundantBlockCall

This cop identifies the use of a `&block` parameter and `block.call` where `yield` would do just as well.

@example

@bad
def method(&block)
  block.call
end
def another(&func)
  func.call 1, 2, 3
end

@good
def method
  yield
end
def another
  yield 1, 2, 3
end

Constants

CLOSE_PAREN
MSG
OPEN_PAREN
SPACE
YIELD

Public Instance Methods

autocorrect(node) click to toggle source

offenses are registered on the `block.call` nodes

# File lib/rubocop/cop/performance/redundant_block_call.rb, line 51
def autocorrect(node)
  _receiver, _method, *args = *node
  new_source = String.new(YIELD)
  unless args.empty?
    new_source += if parentheses?(node)
                    OPEN_PAREN
                  else
                    SPACE
                  end

    new_source << args.map(&:source).join(', ')
  end

  new_source << CLOSE_PAREN if parentheses?(node)
  ->(corrector) { corrector.replace(node.source_range, new_source) }
end
on_def(node) click to toggle source
# File lib/rubocop/cop/performance/redundant_block_call.rb, line 41
def on_def(node)
  blockarg_def(node) do |argname, body|
    next unless body
    blockarg_calls(body, argname) do |blockcall|
      add_offense(blockcall, :expression, format(MSG, argname))
    end
  end
end