class RuboCop::Cop::Lint::RedundantStringCoercion

Checks for string conversion in string interpolation, which is redundant.

@example

# bad

"result is #{something.to_s}"

@example

# good

"result is #{something}"

Constants

MSG_DEFAULT
MSG_SELF

Public Instance Methods

on_interpolation(begin_node) click to toggle source
# File lib/rubocop/cop/lint/redundant_string_coercion.rb, line 30
def on_interpolation(begin_node)
  final_node = begin_node.children.last

  return unless to_s_without_args?(final_node)

  message = final_node.receiver ? MSG_DEFAULT : MSG_SELF

  add_offense(final_node.loc.selector, message: message) do |corrector|
    receiver = final_node.receiver
    corrector.replace(
      final_node,
      if receiver
        receiver.source
      else
        'self'
      end
    )
  end
end