Sha256: 0b012d53624ea7c2ca56ff27a812517014e0bde19ca6a75aaf4f1b62ae665b16

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for string conversion in string interpolation, `print`, `puts`, and `warn` arguments,
      # which is redundant.
      #
      # @example
      #
      #   # bad
      #
      #   "result is #{something.to_s}"
      #   print something.to_s
      #   puts something.to_s
      #   warn something.to_s
      #
      # @example
      #
      #   # good
      #
      #   "result is #{something}"
      #   print something
      #   puts something
      #   warn something
      #
      class RedundantStringCoercion < Base
        include Interpolation
        extend AutoCorrector

        MSG_DEFAULT = 'Redundant use of `Object#to_s` in %<context>s.'
        MSG_SELF = 'Use `self` instead of `Object#to_s` in %<context>s.'
        RESTRICT_ON_SEND = %i[print puts warn].freeze

        # @!method to_s_without_args?(node)
        def_node_matcher :to_s_without_args?, '(send _ :to_s)'

        def on_interpolation(begin_node)
          final_node = begin_node.children.last

          return unless to_s_without_args?(final_node)

          register_offense(final_node, 'interpolation')
        end

        def on_send(node)
          return if node.receiver

          node.each_child_node(:send) do |child|
            next unless child.method?(:to_s)

            register_offense(child, "`#{node.method_name}`")
          end
        end

        private

        def register_offense(node, context)
          receiver = node.receiver
          template = receiver ? MSG_DEFAULT : MSG_SELF
          message = format(template, context: context)

          add_offense(node.loc.selector, message: message) do |corrector|
            replacement = receiver ? receiver.source : 'self'

            corrector.replace(node, replacement)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.50.2 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.50.1 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.50.0 lib/rubocop/cop/lint/redundant_string_coercion.rb