Sha256: 9eb02bc6d4ca7939f4ae4e2d96cb4f4a9e2323ba72287a3c00ba4fa8651f6200

Contents?: true

Size: 1.14 KB

Versions: 6823

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of rescue in its modifier form.
      #
      # @example
      #   # bad
      #   some_method rescue handle_error
      #
      #   # good
      #   begin
      #     some_method
      #   rescue
      #     handle_error
      #   end
      class RescueModifier < Cop
        include Alignment
        include RescueNode

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

        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.source_range, correction)
          end
        end
      end
    end
  end
end

Version data entries

6,823 entries across 6,817 versions & 25 rubygems

Version Path
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.68.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.68.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.67.2 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.67.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.67.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.66.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.65.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.64.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.63.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.63.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.62.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.61.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.61.0 lib/rubocop/cop/style/rescue_modifier.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/rescue_modifier.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/rescue_modifier.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/rescue_modifier.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.60.0 lib/rubocop/cop/style/rescue_modifier.rb