Sha256: 194c7350e1d8a6c1d5528fac14f0b8e6141646c9b434f4cefef917b552a410ac

Contents?: true

Size: 1.86 KB

Versions: 137

Compression:

Stored size: 1.86 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 if !child.method?(:to_s) || child.arguments.any?

            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

137 entries across 136 versions & 14 rubygems

Version Path
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.58.0 lib/rubocop/cop/lint/redundant_string_coercion.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.57.2 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.57.1 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.57.0 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.56.4 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.56.3 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.56.2 lib/rubocop/cop/lint/redundant_string_coercion.rb
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_string_coercion.rb
synctera_ruby_sdk-1.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_string_coercion.rb
synctera_ruby_sdk-1.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_string_coercion.rb
sampero-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.1/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.56.1 lib/rubocop/cop/lint/redundant_string_coercion.rb
tursodb-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_string_coercion.rb