Sha256: 9eb02bc6d4ca7939f4ae4e2d96cb4f4a9e2323ba72287a3c00ba4fa8651f6200

Contents?: true

Size: 1.14 KB

Versions: 6497

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,497 entries across 6,491 versions & 24 rubygems

Version Path
cybrid_api_id_ruby-0.122.57 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_bank_ruby-0.122.57 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_organization_ruby-0.122.57 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_id_ruby-0.122.54 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_bank_ruby-0.122.54 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_organization_ruby-0.122.54 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_bank_ruby-0.122.53 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_organization_ruby-0.122.53 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_id_ruby-0.122.53 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_id_ruby-0.122.52 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_bank_ruby-0.122.52 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_organization_ruby-0.122.52 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_bank_ruby-0.122.51 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_organization_ruby-0.122.51 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_id_ruby-0.122.51 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_bank_ruby-0.122.49 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_id_ruby-0.122.49 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_organization_ruby-0.122.49 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_bank_ruby-0.122.48 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
cybrid_api_id_ruby-0.122.48 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb