Class: Rubocop::Cop::Style::RescueModifier

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/rescue_modifier.rb

Overview

This cop checks for uses of rescue in its modifier form.

Constant Summary collapse

MSG =
'Avoid using rescue in its modifier form.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, #autocorrect_action, cop_name, #do_autocorrect, #ignore_node, inherited, #initialize, #inspect, #name, rails?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#normal_rescue?(node) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/style/rescue_modifier.rb', line 28

def normal_rescue?(node)
  return false unless node

  case node.type
  when :rescue
    # Skip only the rescue node and continue processing its children.
    process_regular_node(node)
    true
  when :ensure
    first_child = node.children.first
    if first_child && first_child.type == :rescue
      process_regular_node(first_child)
      true
    else
      false
    end
  else
    false
  end
end

#on_def(node) ⇒ Object



16
17
18
19
20
# File 'lib/rubocop/cop/style/rescue_modifier.rb', line 16

def on_def(node)
  _method_name, _args, body = *node
  return if normal_rescue?(body)
  super
end

#on_defs(node) ⇒ Object



22
23
24
25
26
# File 'lib/rubocop/cop/style/rescue_modifier.rb', line 22

def on_defs(node)
  _receiver, _method_name, _args, body = *node
  return if normal_rescue?(body)
  super
end

#on_kwbegin(node) ⇒ Object



10
11
12
13
14
# File 'lib/rubocop/cop/style/rescue_modifier.rb', line 10

def on_kwbegin(node)
  body, *_ = *node
  return if normal_rescue?(body)
  super
end

#on_rescue(node) ⇒ Object



49
50
51
# File 'lib/rubocop/cop/style/rescue_modifier.rb', line 49

def on_rescue(node)
  add_offence(:convention, node.loc.expression, MSG)
end