Class: RuboCop::Cop::Style::RedundantRegexpArgument

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

Overview

Identifies places where argument can be replaced from a deterministic regexp to a string.

Examples:

# bad
'foo'.byteindex(/f/)
'foo'.byterindex(/f/)
'foo'.gsub(/f/, 'x')
'foo'.gsub!(/f/, 'x')
'foo'.partition(/f/)
'foo'.rpartition(/f/)
'foo'.scan(/f/)
'foo'.split(/f/)
'foo'.start_with?(/f/)
'foo'.sub(/f/, 'x')
'foo'.sub!(/f/, 'x')

# good
'foo'.byteindex('f')
'foo'.byterindex('f')
'foo'.gsub('f', 'x')
'foo'.gsub!('f', 'x')
'foo'.partition('f')
'foo'.rpartition('f')
'foo'.scan('f')
'foo'.split('f')
'foo'.start_with?('f')
'foo'.sub('f', 'x')
'foo'.sub!('f', 'x')

Constant Summary collapse

MSG =
'Use string `%<prefer>s` as argument instead of regexp `%<current>s`.'
RESTRICT_ON_SEND =
%i[
  byteindex byterindex gsub gsub! partition rpartition scan split start_with? sub sub!
].freeze
DETERMINISTIC_REGEX =
/\A(?:#{LITERAL_REGEX})+\Z/.freeze
STR_SPECIAL_CHARS =
%w[
  \a \c \C \e \f \M \n \" \' \\\\ \t \b \f \r \u \v \x \0 \1 \2 \3 \4 \5 \6 \7
].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, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #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, #ready, #relevant_file?, 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_send(node) ⇒ Object Also known as: on_csend



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/style/redundant_regexp_argument.rb', line 47

def on_send(node)
  return unless (regexp_node = node.first_argument)
  return unless regexp_node.regexp_type?
  return if !regexp_node.regopt.children.empty? || regexp_node.content == ' '
  return unless determinist_regexp?(regexp_node)

  prefer = preferred_argument(regexp_node)
  message = format(MSG, prefer: prefer, current: regexp_node.source)

  add_offense(regexp_node, message: message) do |corrector|
    corrector.replace(regexp_node, prefer)
  end
end