Sha256: 5e7538cb0a3fed41fea4d6be2a9ce9c0599774162843cd71a4ccf0d57b9c3d6c

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for the presence of a `return` inside a `begin..end` block
      # in assignment contexts.
      # In this situation, the `return` will result in an exit from the current
      # method, possibly leading to unexpected behavior.
      #
      # @example
      #
      #   # bad
      #   @some_variable ||= begin
      #     return some_value if some_condition_is_met
      #
      #     do_something
      #   end
      #
      #   # good
      #   @some_variable ||= begin
      #     if some_condition_is_met
      #       some_value
      #     else
      #       do_something
      #     end
      #   end
      #
      #   # good
      #   some_variable = if some_condition_is_met
      #                     return if another_condition_is_met
      #
      #                     some_value
      #                   else
      #                     do_something
      #                   end
      #
      class NoReturnInBeginEndBlocks < Base
        MSG = 'Do not `return` in `begin..end` blocks in assignment contexts.'

        def on_lvasgn(node)
          node.each_node(:kwbegin) do |kwbegin_node|
            kwbegin_node.each_node(:return) do |return_node|
              add_offense(return_node)
            end
          end
        end
        alias on_or_asgn on_lvasgn
        alias on_op_asgn on_lvasgn
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb
rubocop-1.69.2 lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb
rubocop-1.69.1 lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb
rubocop-1.69.0 lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb