Sha256: 34ee1e07090e4ccaa336bfc3a786453bd2c68c2d2581510ccc809adbdaa326f2

Contents?: true

Size: 1.64 KB

Versions: 6833

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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.
      #
      # @example
      #
      #   # bad
      #
      #   '10'.to_i
      #   '10.2'.to_f
      #   '10'.to_c
      #
      #   # good
      #
      #   Integer('10', 10)
      #   Float('10.2')
      #   Complex('10')
      class NumberConversion < Cop
        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 '\
              '%<number_object>s.%<to_method>s, use stricter '\
              '%<corrected_method>s.'.freeze

        def_node_matcher :to_method, <<-PATTERN
          (send $_ ${:to_i :to_f :to_c})
        PATTERN

        def on_send(node)
          to_method(node) do |receiver, to_method|
            message = format(
              MSG,
              number_object: receiver.source,
              to_method: to_method,
              corrected_method: correct_method(node, receiver)
            )
            add_offense(node, message: message)
          end
        end

        private

        def correct_method(node, receiver)
          format(CONVERSION_METHOD_CLASS_MAPPING[node.method_name],
                 number_object: receiver.source)
        end
      end
    end
  end
end

Version data entries

6,833 entries across 6,827 versions & 26 rubygems

Version Path
ory-kratos-client-0.7.6.alpha7 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
ory-kratos-client-0.7.6.alpha6 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
ory-kratos-client-0.7.6.alpha5 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
ory-kratos-client-0.7.6.alpha4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
ory-kratos-client-0.7.6.alpha3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
ory-kratos-client-0.7.6.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
ory-kratos-client-0.7.5.alpha2 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
mx-platform-ruby-0.4.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
mx-platform-ruby-0.3.2 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
mx-platform-ruby-0.3.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
passbase-1.3.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
mfk_openapi_ruby_client-1.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
passbase-1.2.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
passbase-1.1.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
passbase-1.0.3 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
passbase-1.0.2 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
passbase-1.0.1 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
passbase-1.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/number_conversion.rb