Class: Gitlab::Styles::Rubocop::Cop::AvoidReturnFromBlocks

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Defined in:
lib/gitlab/styles/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
WHITELISTED_METHODS =
%i[each each_filename times loop].freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/styles/rubocop/cop/avoid_return_from_blocks.rb', line 30

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| whitelisted?(block) }

    add_offense(return_node)
  end
end