Class: Rubocop::Cop::AvoidReturnFromBlocks

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/rubocop/cop/avoid_return_from_blocks.rb

Overview

Checks for return inside blocks. For more information see: gitlab.com/gitlab-org/gitlab-foss/issues/42889

Examples:

# bad
call do
  return if something

  do_something_else
end

# good
call do
  break if something

  do_something_else
end

Constant Summary collapse

MSG =
'Do not return from a block, use next or break instead.'
DEF_METHODS =
%i[define_method lambda].freeze
ALLOWED_METHODS =
%i[each each_filename times loop].freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/avoid_return_from_blocks.rb', line 28

def on_block(node)
  block_body = node.body

  return unless block_body
  return unless top_block?(node)

  block_body.each_node(:return) do |return_node|
    next if parent_blocks(node, return_node).all? { |block| allowed?(block) }

    add_offense(return_node)
  end
end