class RuboCop::Cop::Performance::RedundantBlockCall
end
yield 1, 2, 3
def another
end
yield
def method
# good
end
func.call 1, 2, 3
def another(&func)
end
block.call
def method(&block)
# bad
@example
where ‘yield` would do just as well.
This cop identifies the use of a `&block` parameter and `block.call`
def args_include_block_pass?(blockcall)
def args_include_block_pass?(blockcall) _receiver, _call, *args = *blockcall args.any?(&:block_pass_type?) end
def autocorrect(node)
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) && !args.empty? ->(corrector) { corrector.replace(node.source_range, new_source) } end
def calls_to_report(argname, body)
def calls_to_report(argname, body) return [] if blockarg_assigned?(body, argname) calls = to_enum(:blockarg_calls, body, argname) return [] if calls.any? { |call| args_include_block_pass?(call) } calls end
def on_def(node)
def on_def(node) blockarg_def(node) do |argname, body| next unless body calls_to_report(argname, body).each do |blockcall| add_offense(blockcall, message: format(MSG, argname: argname)) end end end