Class: RuboCop::Cop::Performance::RedundantBlockCall

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/performance/redundant_block_call.rb

Overview

Identifies the use of a ‘&block` parameter and `block.call` where `yield` would do just as well.

Examples:

# 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

Constant Summary collapse

MSG =
'Use `yield` instead of `%<argname>s.call`.'
YIELD =
'yield'
OPEN_PAREN =
'('
CLOSE_PAREN =
')'
SPACE =
' '

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object Also known as: on_defs



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 47

def on_def(node)
  blockarg_def(node) do |argname, body|
    next unless body

    calls_to_report(argname, body).each do |blockcall|
      next if blockcall.block_literal?

      add_offense(blockcall, message: format(MSG, argname: argname)) do |corrector|
        autocorrect(corrector, blockcall)
      end
    end
  end
end