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
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/redundant_string_coercion.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.65.0 lib/rubocop/cop/lint/redundant_string_coercion.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.64.1 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.63.4 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.63.3 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.63.2 lib/rubocop/cop/lint/redundant_string_coercion.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.63.1 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.63.0 lib/rubocop/cop/lint/redundant_string_coercion.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.62.1 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.62.0 lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.61.0 lib/rubocop/cop/lint/redundant_string_coercion.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/redundant_string_coercion.rb
rubocop-1.60.2 lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/redundant_string_coercion.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/redundant_string_coercion.rb