Class: RuboCop::Cop::Lint::NumberConversion
- Extended by:
- AutoCorrector
- Includes:
- IgnoredMethods
- Defined in:
- lib/rubocop/cop/lint/number_conversion.rb
Overview
This cop 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 ignored with `IgnoredMethods`.
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)" }.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 to_i to_f to_c].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
Methods included from AutoCorrector
Methods included from IgnoredMethods
#ignored_method?, #ignored_methods, included
Methods inherited from Base
#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #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
82 83 84 85 |
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 82 def on_send(node) handle_conversion_method(node) handle_as_symbol(node) end |
#to_method(node) ⇒ Object
72 73 74 |
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 72 def_node_matcher :to_method, "(send $_ ${\#{METHODS}})\n" |
#to_method_symbol(node) ⇒ Object
77 78 79 80 |
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 77 def_node_matcher :to_method_symbol, "{(send _ $_ ${(sym ${\#{METHODS}})} ...)\n (send _ $_ ${(block_pass (sym ${\#{METHODS}}))} ...)}\n" |