Sha256: 8ffd31498b902586ff8beb54f822668ee582d6e43b3a9e5eab64dce62a67eda8

Contents?: true

Size: 1.3 KB

Versions: 38

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for `return` from an `ensure` block.
      # `return` from an ensure block is a dangerous code smell as it
      # will take precedence over any exception being raised,
      # and the exception will be silently thrown away as if it were rescued.
      #
      # If you want to rescue some (or all) exceptions, best to do it explicitly
      #
      # @example
      #
      #   # bad
      #
      #   def foo
      #     do_something
      #   ensure
      #     cleanup
      #     return self
      #   end
      #
      # @example
      #
      #   # good
      #
      #   def foo
      #     do_something
      #     self
      #   ensure
      #     cleanup
      #   end
      #
      #   # also good
      #
      #   def foo
      #     begin
      #       do_something
      #     rescue SomeException
      #       # Let's ignore this exception
      #     end
      #     self
      #   ensure
      #     cleanup
      #   end
      class EnsureReturn < Base
        extend AutoCorrector
        include RangeHelp

        MSG = 'Do not return from an `ensure` block.'

        def on_ensure(node)
          node.body&.each_node(:return) { |return_node| add_offense(return_node) }
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
rubocop-1.22.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.21.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.20.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.19.1 lib/rubocop/cop/lint/ensure_return.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.19.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.18.4 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.18.3 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.18.2 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.18.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.18.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.17.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.16.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.16.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.15.0 lib/rubocop/cop/lint/ensure_return.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.14.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.13.0 lib/rubocop/cop/lint/ensure_return.rb