Class: RuboCop::Cop::Style::RedundantFormat

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/redundant_format.rb

Overview

Checks for calls to ‘Kernel#format` or `Kernel#sprintf` that are redundant.

Calling format with only a single string or constant argument is redundant, as it can be replaced by the string or constant itself.

Also looks for format calls where the arguments are literals that can be inlined into a string easily. This applies to the ‘%s`, `%d`, `%i`, `%u`, and `%f` format specifiers.

Examples:


# bad
format('the quick brown fox jumps over the lazy dog.')
sprintf('the quick brown fox jumps over the lazy dog.')

# good
'the quick brown fox jumps over the lazy dog.'

# bad
format(MESSAGE)
sprintf(MESSAGE)

# good
MESSAGE

# bad
format('%s %s', 'foo', 'bar')
sprintf('%s %s', 'foo', 'bar')

# good
'foo bar'

Constant Summary collapse

MSG =
'Use `%<prefer>s` directly instead of `%<method_name>s`.'
RESTRICT_ON_SEND =
i[format sprintf].to_set.freeze
ACCEPTABLE_LITERAL_TYPES =
i[str dstr sym dsym numeric boolean nil].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

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?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #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

#complex_number?(node) ⇒ Object



73
74
75
# File 'lib/rubocop/cop/style/redundant_format.rb', line 73

def_node_matcher :complex_number?, "{complex (send int :+ complex) (begin complex) (begin (send int :+ complex))}\n"

#find_hash_value_node(node, name) ⇒ Object



78
79
80
# File 'lib/rubocop/cop/style/redundant_format.rb', line 78

def_node_search  :find_hash_value_node, "(pair (sym %1) $_)\n"

#format_without_additional_args?(node) ⇒ Object



63
64
65
# File 'lib/rubocop/cop/style/redundant_format.rb', line 63

def_node_matcher :format_without_additional_args?, "(send {(const {nil? cbase} :Kernel) nil?} %RESTRICT_ON_SEND ${str dstr const})\n"

#on_send(node) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rubocop/cop/style/redundant_format.rb', line 90

def on_send(node)
  format_without_additional_args?(node) do |value|
    replacement = escape_control_chars(value.source)

    add_offense(node, message: message(node, replacement)) do |corrector|
      corrector.replace(node, replacement)
    end
    return
  end

  detect_unnecessary_fields(node)
end

#rational_number?(node) ⇒ Object



68
69
70
# File 'lib/rubocop/cop/style/redundant_format.rb', line 68

def_node_matcher :rational_number?, "{rational (send int :/ rational) (begin rational) (begin (send int :/ rational))}\n"

#splatted_arguments?(node) ⇒ Object



83
84
85
86
87
88
# File 'lib/rubocop/cop/style/redundant_format.rb', line 83

def_node_matcher :splatted_arguments?, "(send _ %RESTRICT_ON_SEND <{\n  splat\n  (hash <kwsplat ...>)\n} ...>)\n"