Sha256: 75c2ff249e95daa38c0fb54901a8b211085315b32fff404aa237be242db962fa

Contents?: true

Size: 1.99 KB

Versions: 16

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of rescue in its modifier form.
      #
      # The cop to check `rescue` in its modifier form is added for following
      # reasons:
      #
      # * The syntax of modifier form `rescue` can be misleading because it
      #   might led us to believe that `rescue` handles the given exception
      #   but it actually rescue all exceptions to return the given rescue
      #   block. In this case, value returned by handle_error or
      #   SomeException.
      #
      # * Modifier form `rescue` would rescue all the exceptions. It would
      #   silently skip all exception or errors and handle the error.
      #   Example: If `NoMethodError` is raised, modifier form rescue would
      #   handle the exception.
      #
      # @example
      #   # bad
      #   some_method rescue handle_error
      #
      #   # bad
      #   some_method rescue SomeException
      #
      #   # good
      #   begin
      #     some_method
      #   rescue
      #     handle_error
      #   end
      #
      #   # good
      #   begin
      #     some_method
      #   rescue SomeException
      #     handle_error
      #   end
      class RescueModifier < Cop
        include Alignment
        include RescueNode

        MSG = 'Avoid using `rescue` in its modifier form.'

        def on_resbody(node)
          return unless rescue_modifier?(node)

          add_offense(node.parent)
        end

        def autocorrect(node)
          operation, rescue_modifier, = *node
          *_, rescue_args = *rescue_modifier

          indent = indentation(node)
          correction =
            "begin\n" \
            "#{operation.source.gsub(/^/, indent)}" \
            "\n#{offset(node)}rescue\n" \
            "#{rescue_args.source.gsub(/^/, indent)}" \
            "\n#{offset(node)}end"

          lambda do |corrector|
            corrector.replace(node, correction)
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.89.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.88.0 lib/rubocop/cop/style/rescue_modifier.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.87.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.87.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.86.0 lib/rubocop/cop/style/rescue_modifier.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/rescue_modifier.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/rescue_modifier.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.85.1 lib/rubocop/cop/style/rescue_modifier.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.85.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.84.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.83.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.82.0 lib/rubocop/cop/style/rescue_modifier.rb