Sha256: fde216f8f67113dc382006405f42e239be6024c113a2549a2cb13eee7ed1e0da

Contents?: true

Size: 1.88 KB

Versions: 62

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for redundant `begin` blocks.
      #
      # Currently it checks for code like this:
      #
      # @example
      #
      #   # bad
      #   def redundant
      #     begin
      #       ala
      #       bala
      #     rescue StandardError => e
      #       something
      #     end
      #   end
      #
      #   # good
      #   def preferred
      #     ala
      #     bala
      #   rescue StandardError => e
      #     something
      #   end
      #
      #   # bad
      #   # When using Ruby 2.5 or later.
      #   do_something do
      #     begin
      #       something
      #     rescue => ex
      #       anything
      #     end
      #   end
      #
      #   # good
      #   # In Ruby 2.5 or later, you can omit `begin` in `do-end` block.
      #   do_something do
      #     something
      #   rescue => ex
      #     anything
      #   end
      #
      #   # good
      #   # Stabby lambdas don't support implicit `begin` in `do-end` blocks.
      #   -> do
      #     begin
      #       foo
      #     rescue Bar
      #       baz
      #     end
      #   end
      class RedundantBegin < Cop
        MSG = 'Redundant `begin` block detected.'

        def on_def(node)
          check(node)
        end
        alias on_defs on_def

        def on_block(node)
          return if target_ruby_version < 2.5

          return if node.send_node.lambda_literal?
          return if node.braces?

          check(node)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.remove(node.loc.begin)
            corrector.remove(node.loc.end)
          end
        end

        private

        def check(node)
          return unless node.body&.kwbegin_type?

          add_offense(node.body, location: :begin)
        end
      end
    end
  end
end

Version data entries

62 entries across 43 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.89.1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.89.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.88.0 lib/rubocop/cop/style/redundant_begin.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.87.1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.87.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.86.0 lib/rubocop/cop/style/redundant_begin.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/redundant_begin.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/redundant_begin.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.85.1 lib/rubocop/cop/style/redundant_begin.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.85.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.84.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.83.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.82.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.81.0 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.80.1 lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.80.0 lib/rubocop/cop/style/redundant_begin.rb