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.'

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

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

Instance Method Details

#check_rescue(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/style/rescue_modifier.rb', line 31

def check_rescue(node)
  return unless node

  case node.type
  when :rescue
    ignore_node(node)
  when :ensure
    first_child = node.children.first
    if first_child && first_child.type == :rescue
      ignore_node(first_child)
    end
  end
end

#on_def(node) ⇒ Object



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

def on_def(node)
  _method_name, _args, body = *node
  check_rescue(body)
end

#on_defs(node) ⇒ Object



26
27
28
29
# File 'lib/rubocop/cop/style/rescue_modifier.rb', line 26

def on_defs(node)
  _receiver, _method_name, _args, body = *node
  check_rescue(body)
end

#on_kwbegin(node) ⇒ Object



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

def on_kwbegin(node)
  body, *_ = *node
  check_rescue(body)
end

#on_rescue(node) ⇒ Object



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

def on_rescue(node)
  return if ignored_node?(node)

  convention(node, :expression)
end