Sha256: 1197376b518ada03acb0e3e9b5d9264f6bf2a63d33a4adc9ac905def6823d9ae

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for redundant `begin` blocks.
      #
      # Currently it checks for code like this:
      #
      # @example
      #
      #   def redundant
      #     begin
      #       ala
      #       bala
      #     rescue StandardError => e
      #       something
      #     end
      #   end
      #
      #   def preferred
      #     ala
      #     bala
      #   rescue StandardError => e
      #     something
      #   end
      class RedundantBegin < Cop
        include OnMethodDef

        MSG = 'Redundant `begin` block detected.'

        def on_method_def(_node, _method_name, _args, body)
          return unless body && body.type == :kwbegin

          add_offense(body, :begin)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            child = node.children.first

            begin_indent = node.loc.column
            child_indent = child.loc.column

            indent_diff = child_indent - begin_indent

            corrector.replace(
              range_with_surrounding_space(node.loc.expression),
              range_with_surrounding_space(
                child.loc.expression
              ).source.gsub(/^[ \t]{#{indent_diff}}/, '')
            )
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.30.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.29.1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.29.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.28.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.27.1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.27.0 lib/rubocop/cop/style/redundant_begin.rb