Class: RuboCop::Cop::Lint::RedundantStringCoercion

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Interpolation
Defined in:
lib/rubocop/cop/lint/redundant_string_coercion.rb

Overview

Checks for string conversion in string interpolation, ‘print`, `puts`, and `warn` arguments, which is redundant.

Examples:


# bad

"result is #{something.to_s}"
print something.to_s
puts something.to_s
warn something.to_s

# good

"result is #{something}"
print something
puts something
warn something

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from Interpolation

#on_dstr, #on_node_with_interpolations

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_interpolation(begin_node) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rubocop/cop/lint/redundant_string_coercion.rb', line 38

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

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/lint/redundant_string_coercion.rb', line 46

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

#to_s_without_args?(node) ⇒ Object



36
# File 'lib/rubocop/cop/lint/redundant_string_coercion.rb', line 36

def_node_matcher :to_s_without_args?, '(send _ :to_s)'