Sha256: 75c2ff249e95daa38c0fb54901a8b211085315b32fff404aa237be242db962fa
Contents?: true
Size: 1.99 KB
Versions: 16
Compression:
Stored size: 1.99 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Style # This cop checks for uses of rescue in its modifier form. # # The cop to check `rescue` in its modifier form is added for following # reasons: # # * The syntax of modifier form `rescue` can be misleading because it # might led us to believe that `rescue` handles the given exception # but it actually rescue all exceptions to return the given rescue # block. In this case, value returned by handle_error or # SomeException. # # * Modifier form `rescue` would rescue all the exceptions. It would # silently skip all exception or errors and handle the error. # Example: If `NoMethodError` is raised, modifier form rescue would # handle the exception. # # @example # # bad # some_method rescue handle_error # # # bad # some_method rescue SomeException # # # good # begin # some_method # rescue # handle_error # end # # # good # begin # some_method # rescue SomeException # handle_error # end class RescueModifier < Cop include Alignment include RescueNode MSG = 'Avoid using `rescue` in its modifier form.' 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, correction) end end end end end end
Version data entries
16 entries across 16 versions & 3 rubygems