# 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 < Base extend AutoCorrector 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 private def check(node) return unless node.body&.kwbegin_type? add_offense(node.body.loc.begin) do |corrector| corrector.remove(node.body.loc.begin) corrector.remove(node.body.loc.end) end end end end end end