Class: RuboCop::Cop::Lint::SuppressedExceptionInNumberConversion
- 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.
Constant Summary collapse
- MSG =
'Use `%<prefer>s` instead.'
- EXPECTED_EXCEPTION_CLASSES =
%w[ArgumentError TypeError ::ArgumentError ::TypeError].freeze
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #begin_numeric_constructor_rescue_nil(node) ⇒ Object
- #constructor_receiver?(node) ⇒ Object
- #numeric_constructor_rescue_nil(node) ⇒ Object
- #numeric_method?(node) ⇒ Object
-
#on_rescue(node) ⇒ Object
rubocop:disable Metrics/AbcSize.
Methods included from AutoCorrector
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
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
permalink #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 |
permalink #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 |
permalink #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 |
permalink #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 |
permalink #on_rescue(node) ⇒ Object
rubocop:disable Metrics/AbcSize
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 |