Class: RuboCop::Cop::Lint::SuppressedExceptionInNumberConversion

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Defined in:
lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb

Overview

Checks for cases where exceptions unrelated to the numeric constructors ‘Integer()`, `Float()`, `BigDecimal()`, `Complex()`, and `Rational()` may be unintentionally swallowed.

Examples:


# bad
Integer(arg) rescue nil

# bad
begin
  Integer(arg)
rescue
  nil
end

# bad
begin
  Integer(arg)
rescue
end

# good
Integer(arg, exception: false)

Constant Summary collapse

MSG =
'Use `%<prefer>s` instead.'
EXPECTED_EXCEPTION_CLASSES =
%w[ArgumentError TypeError ::ArgumentError ::TypeError].freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from TargetRubyVersion

maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#begin_numeric_constructor_rescue_nil(node) ⇒ Object

[View source]

56
57
58
59
60
61
62
# File 'lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb', line 56

def_node_matcher :begin_numeric_constructor_rescue_nil, <<~PATTERN
  (kwbegin
    (rescue
      $#numeric_method?
      (resbody $_? nil?
        {(nil) nil?}) nil?))
PATTERN

#constructor_receiver?(node) ⇒ Object

[View source]

75
76
77
# File 'lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb', line 75

def_node_matcher :constructor_receiver?, <<~PATTERN
  {nil? (const {nil? cbase} :Kernel)}
PATTERN

#numeric_constructor_rescue_nil(node) ⇒ Object

[View source]

49
50
51
52
53
# File 'lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb', line 49

def_node_matcher :numeric_constructor_rescue_nil, <<~PATTERN
  (rescue
    $#numeric_method?
    (resbody nil? nil? (nil)) nil?)
PATTERN

#numeric_method?(node) ⇒ Object

[View source]

65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb', line 65

def_node_matcher :numeric_method?, <<~PATTERN
  {
    (call #constructor_receiver? {:Integer :BigDecimal :Complex :Rational}
      _ _?)
    (call #constructor_receiver? :Float
      _)
  }
PATTERN

#on_rescue(node) ⇒ Object

rubocop:disable Metrics/AbcSize

[View source]

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb', line 82

def on_rescue(node)
  if (method, exception_classes = begin_numeric_constructor_rescue_nil(node.parent))
    return unless expected_exception_classes_only?(exception_classes)

    node = node.parent
  else
    return unless (method = numeric_constructor_rescue_nil(node))
  end

  arguments = method.arguments.map(&:source) << 'exception: false'
  prefer = "#{method.method_name}(#{arguments.join(', ')})"
  prefer = "#{method.receiver.source}#{method.loc.dot.source}#{prefer}" if method.receiver

  add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
    corrector.replace(node, prefer)
  end
end