Class: RuboCop::Cop::Lint::NumberConversion

Inherits:
Base
  • Object
show all
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`.

Examples:


# bad

'10'.to_i
'10.2'.to_f
'10'.to_c
['1', '2', '3'].map(&:to_i)
foo.try(:to_f)
bar.send(:to_c)

# good

Integer('10', 10)
Float('10.2')
Complex('10')
['1', '2', '3'].map { |i| Integer(i, 10) }
foo.try { |i| Float(i) }
bar.send { |i| Complex(i) }

IgnoredMethods: [minutes]


# good
10.minutes.to_i

IgnoredClasses: [Time, DateTime] (default)


# good
Time.now.to_datetime.to_i

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.'
METHODS =
CONVERSION_METHOD_CLASS_MAPPING.keys.map(&:inspect).join(' ')

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 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, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

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

silence_warnings

Constructor Details

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

Instance Method Details

#on_send(node) ⇒ Object



76
77
78
79
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 76

def on_send(node)
  handle_conversion_method(node)
  handle_as_symbol(node)
end

#to_method(node) ⇒ Object



66
67
68
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 66

def_node_matcher :to_method, <<~PATTERN
  (send $_ ${#{METHODS}})
PATTERN

#to_method_symbol(node) ⇒ Object



71
72
73
74
# File 'lib/rubocop/cop/lint/number_conversion.rb', line 71

def_node_matcher :to_method_symbol, <<~PATTERN
  {(send _ $_ ${(sym ${#{METHODS}})} ...)
   (send _ $_ ${(block_pass (sym ${#{METHODS}}))} ...)}
PATTERN