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
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/ensure_return.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.29.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.29.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.28.2 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.28.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.28.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.27.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.26.1 lib/rubocop/cop/lint/ensure_return.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.26.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.25.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.25.0 lib/rubocop/cop/lint/ensure_return.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.24.1 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.24.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.23.0 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.22.3 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.22.2 lib/rubocop/cop/lint/ensure_return.rb
rubocop-1.22.1 lib/rubocop/cop/lint/ensure_return.rb