Sha256: becdf1535c093be38d50d06f3a532938d5cf1694cb887fb786815f69bf5d264e

Contents?: true

Size: 1.29 KB

Versions: 182

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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

182 entries across 175 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/ensure_return.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.65.0 lib/rubocop/cop/lint/ensure_return.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/ensure_return.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/ensure_return.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/ensure_return.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.64.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.63.4 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.63.3 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.63.2 lib/rubocop/cop/lint/ensure_return.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.63.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.63.0 lib/rubocop/cop/lint/ensure_return.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.62.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.62.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.61.0 lib/rubocop/cop/lint/ensure_return.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.60.2 lib/rubocop/cop/lint/ensure_return.rb