Class: RuboCop::Cop::Lint::NumberConversion
- Extended by:
- AutoCorrector
- Includes:
- AllowedMethods, AllowedPattern
- Defined in:
- lib/rubocop/cop/lint/number_conversion.rb
Overview
Warns the usage of unsafe number conversions. Unsafe number conversion can cause unexpected error if auto type conversion fails. Cop prefer parsing with number class instead.
Conversion with Integer, Float, etc. will raise an ArgumentError if given input that is not numeric (eg. an empty string), whereas to_i, etc. will try to convert regardless of input (“”.to_i => 0“). As such, this cop is disabled by default because it’s not necessarily always correct to raise if a value is not numeric.
NOTE: Some values cannot be converted properly using one of the Kernel method (for instance, Time and DateTime values are allowed by this cop by default). Similarly, Rails’ duration methods do not work well with ‘Integer()` and can be allowed with AllowedMethods. By default, there are no methods to allowed.
Constant Summary collapse
- CONVERSION_METHOD_CLASS_MAPPING =
{ to_i: "#{Integer.name}(%<number_object>s, 10)", to_f: "#{Float.name}(%<number_object>s)", to_c: "#{Complex.name}(%<number_object>s)", to_r: "#{Rational.name}(%<number_object>s)" }.freeze
- MSG =
'Replace unsafe number conversion with number ' \ 'class parsing, instead of using ' \ '`%<current>s`, use stricter ' \ '`%<corrected_method>s`.'
- CONVERSION_METHODS =
i[Integer Float Complex Rational to_i to_f to_c to_r].freeze
- METHODS =
CONVERSION_METHOD_CLASS_MAPPING.keys.map(&:inspect).join(' ')
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #on_send(node) ⇒ Object (also: #on_csend)
- #to_method(node) ⇒ Object
- #to_method_symbol(node) ⇒ Object
Methods included from AutoCorrector
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
#on_send(node) ⇒ Object Also known as: on_csend
106 107 108 109 |
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 106 def on_send(node) handle_conversion_method(node) handle_as_symbol(node) end |
#to_method(node) ⇒ Object
92 93 94 |
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 92 def_node_matcher :to_method, "(call $_ ${\#{METHODS}})\n" |
#to_method_symbol(node) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 97 def_node_matcher :to_method_symbol, "(call _ $_ ${\n {\n (sym ${\#{METHODS}})\n (block_pass (sym ${\#{METHODS}}))\n }\n} ...)\n" |